Skip to content

Commit bef2c42

Browse files
committed
Exit code 64 for help + bad syntax
1 parent 8d47e28 commit bef2c42

File tree

6 files changed

+402
-255
lines changed

6 files changed

+402
-255
lines changed

README.md

+37-59
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
Parse and stock arguments/options from `argv`.
44
Inspired by the Python library [argparse](https://python.readthedocs.io/en/latest/library/argparse.html).
5+
Compatible with **bash** version >= **4.2.0**.
56
Documentations available at [documentations](#documentations).
67

78
Use [getopt](https://www.man7.org/linux/man-pages/man1/getopt.1.html) for parse arguments/options.
@@ -15,7 +16,7 @@ set -euo pipefail
1516

1617
source "args.sh"
1718

18-
args_set_description "example of description"
19+
args_set_description "example" "of" "description"
1920
args_set_epilog "example of epilog"
2021

2122
args_add_argument \
@@ -24,10 +25,10 @@ args_add_argument \
2425
--required \
2526
-- "ARG1"
2627
args_add_argument \
27-
--help="clear the test directory" \
28+
--help="print hello" \
2829
--action="store_true" \
29-
--dest="OPT_CLEAR" \
30-
-- "-c" "--clear"
30+
--dest="DO_HELLO" \
31+
-- "-p" "--print-hello"
3132
args_add_argument \
3233
--help="help of option" \
3334
--metavar="VALUE" \
@@ -36,85 +37,62 @@ args_add_argument \
3637

3738
args_parse_arguments "$@"
3839

39-
echo "'ARG1' argument from dest $ARG1"
40+
echo "'ARG1' argument from dest ${ARG1:-}"
4041
echo "'ARG1' argument from map ${ARGS[ARG1]}"
41-
echo "'-c/--clear' option from dest $OPT_CLEAR"
42-
echo "'-c/--clear' option from map ${ARGS[c]}/${ARGS[clear]}"
4342
echo "'--option' option from map ${ARGS[option]}"
43+
if $DO_HELLO; then
44+
echo "Hello world"
45+
fi
4446
```
4547

4648
```
4749
$ ./example/quickstart.sh
4850
./example/quickstart.sh: argument 'ARG1' is required
4951
$ ./example/quickstart.sh -h
50-
usage: quickstart.sh [-c] [-h] [--option VALUE] -- ARG1
52+
usage: quickstart.sh [-h] [-p] [--option VALUE] -- ARG1
5153
5254
example of description
5355
5456
positional arguments:
5557
ARG1 take the first argument
5658
5759
optional arguments:
58-
-c, --clear clear the test directory
5960
-h, --help print this help message
61+
-p, --print-hello print hello
6062
--option VALUE help of option
6163
6264
example of epilog
6365
$ ./example/quickstart.sh 42
6466
'ARG1' argument from dest 42
6567
'ARG1' argument from map 42
66-
'-c/--clear' option from dest false
67-
'-c/--clear' option from map false/false
6868
'--option' option from map 24
69-
```
70-
71-
## Functions
72-
73-
### args_add_argument
74-
75-
Add a argument
76-
77-
|option|description|
78-
|---|---|
79-
|--action|Action (store,store_true,store_false) (default:store)|
80-
|--default|Default value|
81-
|--dest|Destination variable|
82-
|--help|Usage helper|
83-
|--metavar|Usage argument name (if not set use long/short name)|
84-
|--required|Is required if present|
85-
86-
```bash
87-
args_add_argument [options] -- [name/flags...]
88-
```
89-
90-
#### Examples
91-
92-
```bash
93-
# positional argument
94-
args_add_argument --help="help of FOO" --dest="FOO" --required -- "FOO"
95-
# boolean optional argument
96-
args_add_argument --action="store_true" --help="help of foo" --dest="FOO" -- "-f" "--foo"
97-
# not boolean optional argument
98-
args_add_argument --action="store_false" --help="help of foo" --dest="FOO" -- "-f" "--foo"
99-
# optional argument
100-
args_add_argument --help="help of foo" --dest="FOO" -- "-f" "--foo"
101-
```
102-
103-
### args_parse_arguments
104-
105-
Use after args_add_* functions
106-
Convert argument strings to objects and assign them as attributes on the ARGS map
107-
Previous calls to args_add_argument/args_add_bool_option/args_add_reverse_bool_option/args_add_option
108-
determine exactly what objects are created and how they are assigned
109-
Execute this with "$@" parameters
110-
111-
#### Example
112-
113-
```bash
114-
args_parse_arguments "$@"
69+
$ ./example/quickstart.sh 42 -p
70+
'ARG1' argument from dest 42
71+
'ARG1' argument from map 42
72+
'--option' option from map 24
73+
Hello world
74+
$ ./example/quickstart.sh 42 -p --option 42
75+
'ARG1' argument from dest 42
76+
'ARG1' argument from map 42
77+
'--option' option from map 42
78+
Hello world
11579
```
11680

11781
## Documentations
11882

119-
[docs/global.md](docs/global.md)
120-
[docs/setter.md](docs/setter.md)
83+
|Function|Description|
84+
|---|---|
85+
|[args_add_argument](docs/functions.md#args_add_argument)|Add a argument|
86+
|[args_parse_arguments](docs/functions.md#args_parse_arguments)|Convert argument strings to objects and assign them as attributes on the ARGS map|
87+
|[args_set_description](docs/functions.md#args_set_description)|Set a usage description|
88+
|[args_set_epilog](docs/functions.md#args_set_epilog)|Set a epilog description|
89+
|[args_set_usage_width](docs/functions.md#args_set_usage_width)|Set the widths of usage message|
90+
|[args_set_usage](docs/functions.md#args_set_usage)|Set a full usage message|
91+
|[args_set_alternative](docs/functions.md#args_set_alternative)|Set alternative mode for [getopt](https://www.man7.org/linux/man-pages/man1/getopt.1.html)|
92+
|[args_usage](docs/functions.md#args_usage)|Show/Generate usage message|
93+
|[args_clean](docs/functions.md#args_clean)|Clean all map and array for recalled|
94+
|[args_debug_values](docs/functions.md#args_debug_values)|Show all values of arguments and options|
95+
96+
|Global|
97+
|---|
98+
|[variables](docs/global.md#variables)|

0 commit comments

Comments
 (0)