|
1 | | -# args_bash |
2 | | -Parse and stock arguments/options from argv |
| 1 | +# Args |
| 2 | + |
| 3 | +Parse and stock arguments/options from `argv`. |
| 4 | +Inspired by the Python library [argparse](https://python.readthedocs.io/en/latest/library/argparse.html). |
| 5 | +Documentations available at [documentations](#documentations). |
| 6 | + |
| 7 | +Use [getopt](https://www.man7.org/linux/man-pages/man1/getopt.1.html) for parse arguments/options. |
| 8 | + |
| 9 | +## Quikstart |
| 10 | + |
| 11 | +```bash |
| 12 | +#!/bin/bash |
| 13 | + |
| 14 | +set -euo pipefail |
| 15 | + |
| 16 | +source "args.sh" |
| 17 | + |
| 18 | +args_set_description "example of description" |
| 19 | +args_set_epilog "example of epilog" |
| 20 | + |
| 21 | +args_add_argument \ |
| 22 | + --name="ARG1" \ |
| 23 | + --help="take the first argument" \ |
| 24 | + --dest="ARG1" \ |
| 25 | + --required="true" |
| 26 | +args_add_bool_option \ |
| 27 | + --short="c" \ |
| 28 | + --long="clear" \ |
| 29 | + --help="clear the test directory" \ |
| 30 | + --dest="OPT_CLEAR" |
| 31 | +args_add_option \ |
| 32 | + --long="option" \ |
| 33 | + --help="help of option" \ |
| 34 | + --metavar="VALUE" \ |
| 35 | + --default="24" |
| 36 | + |
| 37 | +args_parse_arguments "$@" |
| 38 | + |
| 39 | +echo "'ARG1' argument from dest $ARG1" |
| 40 | +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]}" |
| 43 | +echo "'--option' option from map ${ARGS[option]}" |
| 44 | +``` |
| 45 | + |
| 46 | +``` |
| 47 | +$ ./example/quickstart.sh |
| 48 | +./example/quickstart.sh: argument 'ARG1' is required |
| 49 | +$ ./example/quickstart.sh -h |
| 50 | +usage: quickstart.sh [-c] [-h] [--option VALUE] -- ARG1 |
| 51 | +
|
| 52 | +example of description |
| 53 | +
|
| 54 | +positional arguments: |
| 55 | + ARG1 take the first argument |
| 56 | +
|
| 57 | +optional arguments: |
| 58 | + -c, --clear clear the test directory |
| 59 | + -h, --help print this help message |
| 60 | + --option VALUE help of option |
| 61 | +
|
| 62 | +example of epilog |
| 63 | +$ ./example/quickstart.sh 42 |
| 64 | +'ARG1' argument from dest 42 |
| 65 | +'ARG1' argument from map 42 |
| 66 | +'-c/--clear' option from dest false |
| 67 | +'-c/--clear' option from map false/false |
| 68 | +'--option' option from map 24 |
| 69 | +``` |
| 70 | + |
| 71 | +## Functions |
| 72 | + |
| 73 | +### args_add_argument |
| 74 | + |
| 75 | +Add a positional argument |
| 76 | + |
| 77 | +|option|description| |
| 78 | +|---|---| |
| 79 | +|--default|Default value| |
| 80 | +|--dest|Destination variable| |
| 81 | +|--help|Usage helper| |
| 82 | +|--name|Name of argument| |
| 83 | +|--required|Is required if true| |
| 84 | + |
| 85 | +#### Example |
| 86 | + |
| 87 | +```bash |
| 88 | +args_add_argument --name="FOO" --help="help of FOO" --dest="FOO" --required="true" |
| 89 | +``` |
| 90 | + |
| 91 | +### args_add_bool_option |
| 92 | + |
| 93 | +Add a boolean option |
| 94 | + |
| 95 | +|option|description| |
| 96 | +|---|---| |
| 97 | +|--dest|Destination variable| |
| 98 | +|--help|Usage helper| |
| 99 | +|--long|Long option name| |
| 100 | +|--short|Short option name| |
| 101 | + |
| 102 | +#### Example |
| 103 | + |
| 104 | +```bash |
| 105 | +args_add_bool_option --short="f" --long="foo" --help="help of foo" --dest="FOO" |
| 106 | +``` |
| 107 | + |
| 108 | +### args_add_reverse_bool_option |
| 109 | + |
| 110 | +Add a reverse boolean option |
| 111 | + |
| 112 | +|option|description| |
| 113 | +|---|---| |
| 114 | +|--dest|Destination variable| |
| 115 | +|--help|Usage helper| |
| 116 | +|--long|Long option name| |
| 117 | +|--short|Short option name| |
| 118 | + |
| 119 | +#### Example |
| 120 | + |
| 121 | +```bash |
| 122 | +args_add_reverse_bool_option --short="f" --long="foo" --help="help of foo" --dest="FOO" |
| 123 | +``` |
| 124 | + |
| 125 | +### args_add_option |
| 126 | + |
| 127 | +Add a option who take a argument |
| 128 | + |
| 129 | +|option|description| |
| 130 | +|---|---| |
| 131 | +|--default|Default value of option| |
| 132 | +|--dest|Destination variable| |
| 133 | +|--help|Usage helper| |
| 134 | +|--long|Long option name| |
| 135 | +|--metavar|Usage argument name (if not set use long/short name)| |
| 136 | +|--required|Is required if true| |
| 137 | +|--short|Short option name| |
| 138 | + |
| 139 | +#### Example |
| 140 | + |
| 141 | +```bash |
| 142 | +args_add_option --short="f" --long="foo" --help="help of foo" --dest="FOO" |
| 143 | +``` |
| 144 | + |
| 145 | +### args_parse_arguments |
| 146 | + |
| 147 | +Use after args_add_* functions |
| 148 | +Convert argument strings to objects and assign them as attributes on the ARGS map |
| 149 | +Previous calls to args_add_argument/args_add_bool_option/args_add_reverse_bool_option/args_add_option |
| 150 | +determine exactly what objects are created and how they are assigned |
| 151 | +Execute this with "$@" parameters |
| 152 | + |
| 153 | +#### Example |
| 154 | + |
| 155 | +```bash |
| 156 | +args_parse_arguments "$@" |
| 157 | +``` |
| 158 | + |
| 159 | +## Documentations |
| 160 | + |
| 161 | +[docs/global.md](docs/global.md) |
| 162 | +[docs/setter.md](docs/setter.md) |
0 commit comments