Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to denote that a command line argument is optional when printing usage

People also ask

What makes an argument optional in command line?

Conventions for command-line arguments Optional command-line arguments have a dash ( - ) before them. In general, if an argument doesn't have a dash in front of it, it's not optional unless it's an argument to another command-line argument (see below).

Which command is used to print the total no of arguments passed at command line?

We are using the $# command to get the total number of arguments passed to the script file.

Which command can be used to print its arguments on the terminal?

The echo command is one of the most basic and frequently used commands in Linux. The arguments passed to echo are printed to the standard output.


I suppose this is as much a standard as anything.

The Open Group Base Specifications Issue 7

IEEE Std 1003.1, 2013 Edition

Copyright © 2001-2013 The IEEE and The Open Group

Ch. 12 - Utility Conventions

Although it doesn't seem to mention many things I have commonly seen over the years used to denote various meanings:

  • square brackets [optional option]
  • angle brackets <required argument>
  • curly braces {default values}
  • parenthesis (miscellaneous info)

Edit: I should add, that these are just conventions. The important thing is to pick a convention which is sensible, clearly state your convention, and stick to it consistently. Be flexible and create conventions which seem to be most frequently encountered on your target platform(s). They will be the easiest for users to adapt to.


I personally have not seen a 'standard' that denotes that a switch is optional (like how there's a standard that defines how certain languages are written for example), as it really is personal choice, but according to IBM's docs and the Wiki, along with numerous shell scripts I've personally seen (and command line options from various programs), and the IEEE, the 'defacto' is to treat square bracketed ([]) parameters as optional parameters. Example from Linux:

ping (output trimmed...)

usage: ping [-c count] [-t ttl] host

where [-c count] and [-t ttl] are optional parameters but host is not (as defined in the help).

I personally follow the defacto as well by using [] to mean they are optional parameters and make sure to note that in the usage of that script/program.

I should note that a computer standard should define how something happens and its failure paths (either true fail or undefined behavior). Something along the lines of the command line interpreter _shall_ treat arguments as optional when enclosed in square brackets, and _shall_ treat X as Y when Z, etc.. Much like the ISO C standard says how a function shall be formed for it to be valid (otherwise it fails). Given that there are no command line interpreters, from ASH to ZSH and everything in between, that fail a script for treating [] as anything but optional, one could say there is not a true standard.


Yes, the square brackets indicate optional arguments in Unix man pages.

From "man man":

[-abc] any or all arguments within [ ] are optional.