Skip to content

Parse and store arguments/options from argv natively on bash

License

Notifications You must be signed in to change notification settings

MickaelBlet/bash_args

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Args

Parse and store arguments/options from argv natively on bash.
Inspired by the Python library argparse.
Compatible with bash version >= 4.2.0.
Documentations available at documentations.

Quikstart

#!/bin/bash

set -euo pipefail

source "args.sh"

args_set_description "example" "of" "description"
args_set_epilog "example of epilog"

args_add_argument \
    --help "take the first argument" \
    --dest "ARG1" \
    --required \
    -- "ARG1"
args_add_argument \
    --help="print hello" \
    --action="store_true" \
    --dest="DO_HELLO" \
    -- "-p" "--print-hello"
args_add_argument \
    --help="help of option" \
    --metavar="VALUE" \
    --default="24" \
    -- "--option"

args_parse_arguments "$@"

echo "'ARG1' argument from dest ${ARG1:-}"
echo "'ARG1' argument from map  ${ARGS[ARG1]}"
echo "'--option' option from map ${ARGS[option]}"
if $DO_HELLO; then
    echo "Hello world"
fi
$ ./example/quickstart.sh
./example/quickstart.sh: argument 'ARG1' is required
$ ./example/quickstart.sh -h
usage: quickstart.sh [-h] [-p] [--option VALUE] -- ARG1

example of description

positional arguments:
  ARG1                  take the first argument

optional arguments:
  -h, --help            print this help message
  -p, --print-hello     print hello
  --option VALUE        help of option

example of epilog
$ ./example/quickstart.sh 42
'ARG1' argument from dest 42
'ARG1' argument from map  42
'--option' option from map 24
$ ./example/quickstart.sh 42 -p
'ARG1' argument from dest 42
'ARG1' argument from map  42
'--option' option from map 24
Hello world
$ ./example/quickstart.sh 42 -p --option 42
'ARG1' argument from dest 42
'ARG1' argument from map  42
'--option' option from map 42
Hello world

Documentations

Function Description
args_add_argument Add a argument
args_parse_arguments Convert argument strings to objects and assign them as attributes on the ARGS map
args_set_description Set a usage description
args_set_epilog Set a epilog description
args_set_usage_width Set the widths of usage message
args_set_usage Set a full usage message
args_usage Show/Generate usage message
args_clean Clean all map and array for recalled
args_debug_values Show all values of arguments and options
Global
variables

About

Parse and store arguments/options from argv natively on bash

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages