NBash
57 строк · 1.9 Кб
1#!/usr/bin/env bash
2#
3
4PATH="..:$PATH"
5
6# Load argsparse library.
7. argsparse.sh
8
9# We declare an option accepting a value by giving it the "value"
10# property. By default, if an option with the "value" property is
11# repeated on the command line, only the last value is kept in
12# program_options. See other tutorials to learn how to have such
13# cumulative options.
14argsparse_use_option option1 "An option." value
15
16# Declaring an option accepting a value and not having a
17# single-char equivalent. (Alternative syntax)
18# Ending the option name by ':' is equivalent
19argsparse_use_option option2: "Another option."
20
21# Of course, options can accept multiple properties.
22argsparse_use_option option3 "A 3rd option." short:o value
23
24# Alternative syntaxes for the same thing.
25argsparse_use_option option4: "A 4th option." short:p
26argsparse_use_option op=tion5: "A 5th option."
27
28# Another option accepting a value, with a single-char equivalent.
29# Setting the "default" property to an option make it have a default
30# value. An option with a default value is considered as always set.
31argsparse_use_option option6 "A 6th option." short:i value default:some_value
32# Note that 'default' does not imply 'value'.
33
34# Declaring the option...
35argsparse_use_option option7 "A 7th option."
36# ...and setting properties after.
37argsparse_set_option_property value option7
38argsparse_set_option_property short:n option7
39argsparse_set_option_property default:default_option7_value option7
40
41#
42printf -v argsparse_usage_description "%s\n" \
43"A example script with options accepting values." \
44"Try command lines such as:" \
45" $0 --help" \
46" $0 --option1" \
47" $0 --option1 my_value" \
48" $0 -o some_other_value --option2 asdf" \
49" $0 --option7 not_default_anymore"
50
51# Command line parsing is done here.
52argsparse_parse_options "$@"
53
54printf "Options reporting:\n"
55# Simple reporting function.
56argsparse_report
57printf "End of argsparse report.\n\n"
58