https://github.com/mickaelblet/bash_args
Parse and store arguments/options from argv natively on bash
https://github.com/mickaelblet/bash_args
argparse argv bash bash-library bash-only
Last synced: 3 months ago
JSON representation
Parse and store arguments/options from argv natively on bash
- Host: GitHub
- URL: https://github.com/mickaelblet/bash_args
- Owner: MickaelBlet
- License: mit
- Created: 2024-03-26T20:48:11.000Z (about 1 year ago)
- Default Branch: master
- Last Pushed: 2025-01-06T02:18:20.000Z (5 months ago)
- Last Synced: 2025-01-06T03:22:05.501Z (5 months ago)
- Topics: argparse, argv, bash, bash-library, bash-only
- Language: Shell
- Homepage:
- Size: 238 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Args
Parse and store arguments/options from `argv` natively on **bash**.
Inspired by the Python library [argparse](https://python.readthedocs.io/en/latest/library/argparse.html).
Compatible with **bash** version >= **4.2.0**.
Documentations available at [documentations](#documentations).## Quikstart
```bash
#!/bin/bashset -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:-false}; 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] -- ARG1example of description
positional arguments:
ARG1 take the first argumentoptional arguments:
-h, --help print this help message
-p, --print-hello print hello
--option VALUE help of optionexample 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
### Functions
|Function|Description|
|---|---|
|[args_add_argument](docs/functions.md#args_add_argument)|Add a argument.|
|[args_parse_arguments](docs/functions.md#args_parse_arguments)|Convert argument strings to objects and assign them as attributes on the ARGS map.|
|[args_clean](docs/functions.md#args_clean)|Clean all map and array for recalled.|
|[args_count](docs/functions.md#args_count)|Count the number of occurence of argument after parsed.|
|[args_debug_values](docs/functions.md#args_debug_values)|Show all values of arguments and options.|
|[args_isexists](docs/functions.md#args_isexists)|Check if argument is exists after parsed.|
|[args_set_description](docs/functions.md#args_set_description)|Set a usage description.|
|[args_set_epilog](docs/functions.md#args_set_epilog)|Set a epilog description.|
|[args_set_program_name](docs/functions.md#args_set_program_name)|Set the program name for usage message.|
|[args_set_usage_width](docs/functions.md#args_set_usage_width)|Set the widths of usage message.|
|[args_set_usage](docs/functions.md#args_set_usage)|Set a full usage message.|
|[args_usage](docs/functions.md#args_usage)|Show/Generate usage message.|### Global variables
|Name|Description|
|---|---|
|__ARGS|Assossiative array for use internaly in args script.|
|ARGS|Assossiative array for store after [args_parse_arguments](../README.md#args_parse_arguments).|