[![Actions Status](https://github.com/tecolicom/getoptlong/actions/workflows/test.yml/badge.svg?branch=main)](https://github.com/tecolicom/getoptlong/actions?workflow=test) [![MetaCPAN Release](https://badge.fury.io/pl/Getopt-Long-Bash.svg)](https://metacpan.org/release/Getopt-Long-Bash) # NAME getoptlong - Option parsing that does what you mean, for Bash # SYNOPSIS **Option definition:** declare -A OPTS=( [ &USAGE ]="command [options] file..." [ flag | f # Flag ]= [ counter | c + # Counter ]=0 [ required | r : # Required ]=/dev/stdout [ optional | o ? # Optional ]= [ array | A @ # Array ]= [ hash | H % # Hash ]= [ integer | i :=i # Integer ]=1 [ pattern | p :=(^(fast|slow)$) # Regex ]=fast ) **One-liner:** . getoptlong.sh OPTS "$@" **Multi-step:** . getoptlong.sh - getoptlong init OPTS getoptlong parse "$@" && eval "$(getoptlong set)" **Or:** eval "$(getoptlong OPTS)" # VERSION 0.7.0 # DESCRIPTION **getoptlong.sh** is a Bash library providing Perl's [Getopt::Long](https://metacpan.org/pod/Getopt%3A%3ALong)-style option parsing. Options are defined in a Bash associative array: the key specifies the option name, aliases, type, and other attributes; the value sets the default. The library parses command-line arguments, sets variables, and leaves non-option arguments in `$@`. Two usage modes are available: **one-liner** for simple scripts (source with array name and arguments), and **multi-step** for advanced control (separate init, parse, and set calls). Supports short (`-v`) and long (`--verbose`) options with bundling (`-vvv`). **Option types**: _flag_, _required argument_, _optional argument_, _array_, _hash_. **Modifiers**: _callback_, _pass-through_. **Validation**: _integer_, _float_, _regex_. **Help message** generation. **Multiple invocations** for subcommand support. For a gentle introduction, see [Getopt::Long::Bash::Tutorial](https://metacpan.org/pod/Getopt%3A%3ALong%3A%3ABash%3A%3ATutorial). # INSTALLATION cpanm -n Getopt::Long::Bash # USAGE ## One-liner Source with array name and arguments to parse in one step: . getoptlong.sh OPTS "$@" Configuration parameters must be included in the options array (e.g., `[&PREFIX]=OPT_`). Callback registration is not available in this mode; use `!` modifier for automatic callback instead. ## Multi-step Source the library first, then call init, parse, and set separately: . getoptlong.sh - getoptlong init OPTS getoptlong parse "$@" && eval "$(getoptlong set)" This mode allows callback registration between init and parse. **Note:** When sourcing without arguments (`. getoptlong.sh`), the current shell's positional parameters are passed to the library. If the first argument happens to match an existing associative array name, it may cause unexpected behavior. Use `. getoptlong.sh -` to safely source without side effects. # OPTION DEFINITION Options are defined as elements of an associative array. Each key specifies the option's name, type, and modifiers, while the value provides the default. Whitespace is allowed anywhere in the definition for readability. Configuration parameters can also be included with `&` prefix (e.g., `[&PREFIX]=OPT_`); see ["CONFIGURATION"](#configuration). The key format is: [NAME[|ALIAS...][TYPE[MOD]][DEST][=VALIDATE] # DESC]=DEFAULT ## COMPONENTS - **NAME** Long option name (`--name`). Hyphens become underscores in variables (`--dry-run` → `$dry_run`). - **ALIAS** Additional names separated by `|` (e.g., `verbose|v|V`). - **TYPE** Argument type specifier: (none) or + Flag (counter) : Required argument ? Optional argument @ Array (multiple values) % Hash (key=value pairs) - **MOD (MODIFIER)** Special behavior flags (can be combined): ! Callback - calls function when option is parsed > Pass-through - collects option and value into array - **DEST** Custom variable name (e.g., `[opt|o:MYVAR]` stores in `$MYVAR`). - **VALIDATE** Value validation: `=i` (integer), `=f` (float), `=`. See ["VALIDATION"](#validation). - **DESC (DESCRIPTION)** Help message text (everything after `#`). # OPTION TYPES Each option type determines how arguments are handled and stored. ## COUNTER FLAG (`+` or none) A flag takes no argument. First use sets to `1`, subsequent uses increment (useful for verbosity levels). Use `--no-` to reset to empty string. Bundling supported: `-vvv` equals `-v -v -v`. [verbose|v]= # $verbose: 1 when specified [debug|d+]=0 # $debug: increments (-d -d -d or -ddd) Numeric initial value (like `0`) enables counter display in help. There is no pure boolean type; all flags are counters. Use empty string test for boolean evaluation: `[[ $verbose ]]` is true when non-empty, false when empty. ## REQUIRED ARGUMENT (`:`) The option requires an argument; error if missing. Use `--no-` to reset to empty string (useful for disabling defaults). [output|o:]= # --output=file, --output file, -ofile, -o file Short form `-o=value` is **not** supported (use `-ovalue` or `-o value`). ## OPTIONAL ARGUMENT (`?`) The argument is optional. The variable has three possible states: a value (`--config=file`), empty string (`--config` without value), or unset (option not specified). Use `[[ -v config ]]` to check if the option was specified. [config|c?]= # --config=file or --config (sets to "") `--config=value` sets to `value`, `--config` without value sets to empty string. Short form `-c` sets to empty string; `-cvalue` form is **not** supported. ## ARRAY (`@`) Collects multiple values into an array. Multiple specifications accumulate. A single option can contain delimited values (default: space, tab, comma; see [DELIM](#configuration)). Access with `"${include[@]}"`. [include|I@]= # --include a --include b or --include a,b To reset existing values: use `--no-include` on the command line (e.g., `--no-include --include /new/path`), or use `callback --before` to automatically reset before each new value. ## HASH (`%`) Collects `key=value` pairs into an associative array. Key without value is treated as `key=1`. Multiple pairs can be specified: `--define A=1,B=2` (see [DELIM](#configuration)). Access with `${define[KEY]}`, keys with `${!define[@]}`. [define|D%]= # --define KEY=VAL or --define KEY (KEY=1) To reset existing values: use `--no-define` on the command line (e.g., `--no-define --define KEY=val`), or use `callback --before` to automatically reset before each new value. # CALLBACKS Callback functions are called when an option is parsed. The value is stored in the variable as usual, and the callback is invoked for additional processing such as validation or side effects. Callbacks work the same way with pass-through options. Calls a function when the option is parsed. Default function name is the option name with hyphens converted to underscores; use `getoptlong callback` to specify a custom function. Can combine with any type (`+!`, `:!`, `?!`, `@!`, `%!`). [action|a!]= # Calls action() when specified [file|f:!]= # Calls file() with argument ## REGISTRATION Register callbacks with `getoptlong callback`. If function name is omitted or `-`, uses option name (hyphens to underscores). Additional `args` are passed to the callback function after the option name and value. getoptlong callback