NBash
56 строк · 1.7 Кб
1#!/usr/bin/env bash
2
3PATH="..:$PATH"
4
5# Load argsparse library.
6. argsparse.sh
7
8# Beside the 'type', you can control options user-given values by
9# using other methods.
10
11argsparse_use_option option1 "An enumerated option." value
12
13# *Instead* of declaring a type for the option, you can declare an
14# array named option_<optionname>_values, which will contain the
15# acceptable values for said option.
16option_option1_values=(only accepting those values)
17# Acceptable values are mentioned in argsparse usage.
18
19# And, after either type checking of enumerated values checking, you
20# have the possibility to simply define a function named
21# check_value_of_<optionname>. If the function return with value 0, it
22# means the value is correct. Any other value returned by the function
23# would make the value incorrect.
24argsparse_use_option option2 "An always bad option." value
25check_value_of_option2() {
26# So, this would make all values return an error.
27false
28}
29
30# If an option name contains '-' chars...
31argsparse_use_option long-option "An option with a long name." value
32# ... then the array name...
33option_long_option_values=(long option acceptable values)
34# ... and the function name must have '_' in place of '-'.
35check_value_of_long_option() {
36local value=$1
37[[ "$value" = long ]]
38}
39
40#
41printf -v argsparse_usage_description "%s\n" \
42"A tutorial script teaching advanced value checking." \
43"Try command lines such as:" \
44" $0" \
45" $0 -h" \
46" $0 --option1 only" \
47" $0 --option2 something" \
48" $0 --long-option only"
49
50# Command line parsing is done here.
51argsparse_parse_options "$@"
52
53printf "Options reporting:\n"
54# Simple reporting function.
55argsparse_report
56printf "End of argsparse report.\n\n"
57