Usage message


In computer programming, a usage message or help message is a brief message displayed by a program that utilizes a command-line interface for execution. This message usually consists of the correct command line usage for the program and includes a list of the correct command-line arguments or options acceptable to said program.
Usage messages are utilized as a quick way for a program to inform the user of proper command syntax, and should not be substituted for proper error messages or for detailed documentation such as a man page.

Pattern

On Unix-like platforms, usage messages usually follow the same common pattern:
  • They often begin with "Usage:", the command, followed by a list of arguments.
  • To indicate optional arguments, square brackets are commonly used, and can also be used to group parameters that must be specified together.
  • To indicate required arguments, angled brackets are commonly used, following the same grouping conventions as square brackets.
  • Exclusive parameters can be indicated by separating them with vertical bars within groups.

Examples

Here is an example based on the NetBSD :

Usage: program req1 req2
  • [options">opt1 [opt2

    This would indicate that "program" should be called with:
    • [options without operands:,,, . Note that in this case some parameters are case-sensitive
    • exclusive options:,
    • options with operands:
    • exclusive options with operands:,
    • required arguments:,
    • optional argument, which may be used with or without
    • optional argument, which requires

    Implementation

    To print a usage statement in a shell script, one might write:

    case "$arg" in
    ...
    h) printf 'Usage: %s parameter1 parameter2...\n' "$"
    exit 0
    ;;
    ...
    esac

    Anti-patterns

    A usage statement is not an error message, and thus it should generally only be printed when specifically requested by the user. It should be written to the standard error; if the user has entered an incorrect command, the program should ideally respond with a succinct error message that describes the exact error made by the user rather than printing the usage statement and requiring the user to figure out what the mistake was. If the user fails to pass the correct number of arguments, for example, a single line stating that an argument is missing is far more useful than several pages of output providing a general usage.