Skip to content

Latest commit

 

History

History
92 lines (74 loc) · 4.47 KB

File metadata and controls

92 lines (74 loc) · 4.47 KB

6.2.5. Argparser

Contents of argparser_wrapper.sh
#!/usr/bin/env bash

# Parse the arguments.
args=(
    "id       | short_opts | long_opts | val_names   | defaults | choices | type | arg_no | arg_group            | help                                                            "
    "in_file  |            |           | source      |          |         | file | 1      | Positional arguments | the template HTML file to fill in                               "
    "out_file |            |           | destination |          |         | file | 1      | Positional arguments | the output HTML file                                            "
    "name     | n          | name      | NAME        |          |         | str  | 1      | Mandatory options    | the name of the homepage's owner                                "
    "age      | a          | age       | AGE         |          |         | uint | 1      | Mandatory options    | the current age of the homepage's owner                         "
    "role     | r          | role      | ROLE        |          | u,m,b   | char | 1      | Mandatory options    | the role of the homepage's owner (u: user, m: moderator, b: bot)"
    "verbose  | v          | verbose   |             | false    |         | bool | 0      | Optional options     | output verbose information                                      "
    "exit     | e          | exit      |             | false    |         | bool | 0      | Optional options     | exit directly after parsing, for runtime assessment             "
)
source argparser -- "$@"

# Set the role to its long form.
case "${role}" in
    u) role="User" ;;
    m) role="Moderator" ;;
    b) role="Bot" ;;
esac

# Possibly, exit prematurely.
[[ "${exit}" == true ]] && exit

# Run the HTML processor.
if [[ "$0" == */* ]]; then
    cd "${0%/*.sh}" || exit 1
fi
source process_html_template.sh

Example calls:

# Long options.
bash argparser_wrapper.sh --verbose --name="A. R. G. Parser" --age=2 --role=b template.html argparser.html

# Short options.
bash argparser_wrapper.sh -v -n "A. R. G. Parser" -a 2 -r b template.html argparser.html

# Leading positional arguments.
bash argparser_wrapper.sh template.html argparser.html -v -n "A. R. G. Parser" -a 2 -r b

# Positional arguments delimiter "--".
bash argparser_wrapper.sh --verbose --name="A. R. G. Parser" --age=2 --role=b -- template.html argparser.html

# Positional arguments delimiter "++".
bash argparser_wrapper.sh -v -n "A. R. G. Parser" -- template.html argparser.html ++ -a 2 -r b

# Intermixed positional arguments.
bash argparser_wrapper.sh --verbose --name="A. R. G. Parser" template.html --age=2 argparser.html --role=b

# Help, usage, and version messages.
bash argparser_wrapper.sh --help
bash argparser_wrapper.sh --usage
bash argparser_wrapper.sh --version

🡄 6.2.4. docopts           6.3. Runtime comparison 🡆