NBash
74 строки · 1.8 Кб
1#!/usr/bin/env bash
2
3PATH="..:$PATH"
4
5# Load argsparse library.
6. argsparse.sh
7
8# Miscellaneous other functions. See below.
9
10argsparse_use_option option1 "An option" value exclude:option2
11argsparse_use_option option2 "Another option"
12
13#
14printf -v argsparse_usage_description "%s\n" \
15"A tutorial script to show other misc argsparse features." \
16"Try command lines such as:" \
17" $0" \
18" $0 -h" \
19" $0 --option1 123a" \
20" $0 --option2"
21
22# You can define your own usage function
23usage() {
24printf "Beginning of usage\n"
25# Unless you want it to be completely removed, you can still call
26# argsparse_usage function to also have auto-generated argsparse
27# usage message.
28argsparse_usage
29printf "End of usage\n"
30# And perform your own exit.
31exit 16
32}
33
34# Allow empty command line
35argsparse_allow_no_argument yes
36
37# Command line parsing is done here.
38argsparse_parse_options "$@"
39
40if [[ $# -eq 0 ]]
41then
42cat <<'information'
43Though this script has been called without any parameter, `usage' has
44not been triggered, due to the "argsparse_allow_no_argument yes"
45executed a few lines above in the script.
46
47To obtain the usage message, explicitly add --help on the command line.
48
49information
50fi
51
52# The argsparse_has_option_property function returns with 0 if a named
53# option has a named property. If the property has a value, it is
54# printed to standard input. So you can do things like that:
55if exclusion=$(argsparse_has_option_property option1 exclude)
56then
57printf "The option %s excludes these other options: %s\n" \
58option1 "$exclusion"
59fi
60
61for option in option1 option2
62do
63if argsparse_has_option_property "$option" value
64then
65printf "%s has the value property.\n" "$option"
66else
67printf "%s does not have the value property.\n" "$option"
68fi
69done
70
71printf "\nOptions reporting:\n"
72# Simple reporting function.
73argsparse_report
74printf "End of argsparse report.\n\n"
75