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

Assume that I have a script that can be run in either of the following ways.

./foo arg1 arg2
./foo

Is there a generally accepted way to denote that arg1 and arg2 aren't mandatory arguments when printing the correct usage of the command?

I've sometimes noticed usage printed with the arguments wrapped in brackets like in the following usage printout.

Usage: ./foo [arg1] [arg2]

Do these brackets mean that the argument is optional or is there another generally accepted way to denote that an argument is optional?


ANSWERS:


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) 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.

I personally follow the defacto as well by using [] to mean optional parameter.


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

From "man man":

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


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.



 MORE:


 ? Shell script with options and argument - Update
 ? Running C Executable With Arguments As C Executable From Shell Script
 ? UNIX Shell scripting/Menu
 ? Find and replace: \'
 ? Find and replace: \'
 ? Find and replace: \'
 ? replace with sed on Mac OS X Leopard doesn't do what expected
 ? Change find and replace every iteration
 ? Using sed to replace text with spaces with a defined variable with slashs
 ? Grep - find and replace