Usage message

In computer programming, a usage message or help message refers to 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 detailed documentation, such as a man page.

Pattern

On Unix-like platforms, usage messages usually follow the same common pattern:

Examples

Here is an example based on the NetBSD source code style guide:

Usage: program [-aDde] [-f | -g] [-n number] [-b b_arg | -c c_arg] req1 req2 [opt1 [opt2]]

This would indicate that "program" should be called with:

Implementation

For example, if a shell script required at least two parameter to be run, a programmer could create a usage message using something similar to the following:

if [ $# -lt 2 ]; then
    printf 'Usage: %s parameter1 parameter2 ...\n' "$(basename "$0")" >&2
    exit 64
fi

Explanation of the code:

  1. $# is number of parameter passed on the command line to the script
  2. "$(basename "$0")" is the output of basename, to strip any path away from the script's filename
  3. >&2 is used to redirect the output of printf to stderr
  4. /usr/include/sysexits.h defines 64 as the preferable exit code for usage errors.

See also

This article is issued from Wikipedia. The text is licensed under Creative Commons - Attribution - Sharealike. Additional terms may apply for the media files.