#parser #arguments-parser #bash #argument #cli #cli-parser

bin+lib spongecrab

Bringing powerful argument parsing to bash scripts

1 unstable release

0.2.3 Apr 27, 2024

#52 in #argument

Unlicense

14KB
245 lines

Spongecrab - bringing powerful argument parsing to bash scripts

See '--example' and '--generate' for reference.

Usage: spongecrab [OPTIONS] [-- <INPUT>...]

Arguments:
  [INPUT]...  Raw text to parse

Options:
      --name <NAME>              Application name [default: myscript]
      --about <ABOUT>            Application about text
  -p, --positional <POSITIONAL>  Add a (required) positional argument
  -o, --optional <OPTIONAL>      Add an optional positional argument
  -O, --option <OPTION>          Add an optional argument
  -f, --flag <FLAG>              Add a flag argument
  -c, --collect <COLLECT>        Collect remaining positional arguments
  -C, --collect+ <COLLECT>       Collect (required) remaining positional arguments
  -e, --extra <EXTRA>            Collect extra optional arguments after '--'
  -P, --prefix <PREFIX>          Prefix for parsed variable names
  -G, --generate                 Generate script boilerplate (see also '--example')
      --example                  Generate example script
  -h, --help                     Print help
  -V, --version                  Print version

Generated code (using --generate):


# Command line interface (based on `spongecrab --generate`)
APP_NAME=$(basename "$0")
ABOUT="program description"
# Argument syntax: "<arg_name>;<help_text>;<default_value>;<short_name>"
# -o, -c, -C are mutually exclusive
CLI=(
    -p "arg1;Positional argument"
    -o "arg2;Optional positional argument;<default value>"
    -O "option;Optional argument;;o"
    -f "flag;Optional flag argument;;f"
    # -c "collect_any;Optional remaining positional arguments"
    # -C "collect_some;Required remaining positional arguments"
    -e "extra;Optional extra arguments after '--'"
)
CLI=$(spongecrab --name "$APP_NAME" --about "$ABOUT" "${CLI[@]}" -- "$@") || exit 1
eval "$CLI" || exit 1

See the example script.

Dependencies

~1.2–1.8MB
~34K SLoC