LinuxCommandLibrary

getopt

Parse command-line options

TLDR

Parse optional verbose/version flags with shorthands

$ getopt [[-o|--options]] vV [[-l|--longoptions]] verbose,version -- --version --verbose
copy

Add a --file option with a required argument with shorthand -f
$ getopt [[-o|--options]] f: [[-l|--longoptions]] file: -- --file=somefile
copy

Add a --verbose option with an optional argument with shorthand -v, and pass a non-option parameter arg
$ getopt [[-o|--options]] v:: [[-l|--longoptions]] verbose:: -- --verbose arg
copy

Accept a -r and --verbose flag, a --accept option with an optional argument and add a --target with a required argument option with shorthands
$ getopt [[-o|--options]] rv::s::t: [[-l|--longoptions]] verbose,source::,target: -- -v --target target
copy

SYNOPSIS

getopt options -- optstring -- parameters

PARAMETERS

optstring
    A string containing the option characters to be recognized. Each character can be followed by a colon (:) to indicate that it requires an argument or two colons (::) to indicate that it accepts an optional argument.

--longoptions
    A comma-separated list of long option names to be recognized. Each name can be followed by an equals sign (=) to indicate that it requires an argument or two equals signs (==) to indicate that it accepts an optional argument.

-o, --options optstring
    Specifies the short options accepted. Identical to the optstring argument.

-l, --longoptions longoptions
    Specifies the long options accepted. Identical to the --longoptions argument.

-n, --name program-name
    Specifies the program name to use when reporting errors.

-q, --quiet
    Disable error reporting by getopt itself, but the exit code still indicates an error.

-Q, --quietly
    Disable error and warning reporting by getopt itself, regardless of the exit code.

-s, --shell shellname
    Set the shell to be used for quoting (e.g., bash, csh).

-u, --unquoted
    Do not quote the output. Useful for shells that do not handle quoted arguments well.

-T, --test
    Test mode; don't actually rearrange arguments or generate output.

-h, --help
    Display a help message and exit.

-V, --version
    Display version information and exit.

--
    Separates options from non-option arguments.

DESCRIPTION

getopt is a command-line utility used in shell scripts to parse command options and arguments. It provides a more robust and standardized way to handle options compared to manually parsing $@. It rearranges arguments to group options together, allows short and long options, and can validate option arguments. This improves the script's reliability and user-friendliness by offering consistent argument handling. It helps to validate command-line input and ensure correct usage in shell scripts.

getopt is particularly useful when you need to support both short and long options (e.g., -a and --all) and when you want to handle options with mandatory or optional arguments. It generates shell code that simplifies accessing and processing the options and their corresponding arguments within the script. While newer alternatives like argparse (often Python-based) offer more features, getopt remains a relevant tool for POSIX shell scripting due to its wide availability and ease of use.

CAVEATS

getopt has different implementations. The standard implementations might differ in argument order and behavior with unexpected inputs, especially with optional arguments.

EXAMPLES

Example: Parse options -a, -b, and -c with -b requiring an argument:
getopt abc: -- "$@"

Example with long options:
getopt -o a:b::c --long alpha,beta::,gamma -- "$@"

RETURN CODES

getopt returns 0 upon successful parsing. It returns a non-zero exit status if it encounters an invalid option or missing argument.

SEE ALSO

getopts(1), argparse (library)

Copied to clipboard