-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtry_argparser.sh
More file actions
executable file
·58 lines (51 loc) · 3.9 KB
/
Copy pathtry_argparser.sh
File metadata and controls
executable file
·58 lines (51 loc) · 3.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#!/usr/bin/env bash
###############################################################################
# #
# Copyright 2025 Simon Brandt #
# #
# Licensed under the Apache License, Version 2.0 (the "License"); #
# you may not use this file except in compliance with the License. #
# You may obtain a copy of the License at #
# #
# http://www.apache.org/licenses/LICENSE-2.0 #
# #
# Unless required by applicable law or agreed to in writing, software #
# distributed under the License is distributed on an "AS IS" BASIS, #
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. #
# See the License for the specific language governing permissions and #
# limitations under the License. #
# #
###############################################################################
# Author: Simon Brandt
# E-Mail: simon.brandt@uni-greifswald.de
# Last Modification: 2025-09-01
# Usage: Run this script with "bash try_argparser.sh".
# Purpose: Demonstrate the basic functionality of the Argparser.
# Define the arguments.
# shellcheck disable=SC2190 # Indexed, not associative array.
args=(
"id | short_opts | long_opts | val_names | defaults | choices | type | arg_no | arg_group | notes | help "
"pos_1 | | | pos_1 | 2 | 1,2 | int | 1 | Positional arguments | | one positional argument with default and choice "
"pos_2 | | | pos_2 | | | int | 2 | Positional arguments | | two positional arguments without default or choice"
"var_1 | a,A | var-1,var-a | VAL_1 | | | uint | 1 | Mandatory options | | one value without default or choice "
"var_2 | b,B | var-2,var-b | VAL_2 | | | int | + | Mandatory options | | at least one value without default or choice "
"var_3 | c,C | var-3,var-c | VAL_3 | | A,B | char | + | Mandatory options | | at least one value with choice "
"var_4 | d,D | var-4,var-d | VAL_4 | A | A-C | char | 1 | Optional options | | one value with default and choice "
"var_5 | e,E | var-5,var-e | VAL_5 | E | | str | 1 | Optional options | | one value with default "
"var_6 | f,F | var-6,var-f | VAL_6 | false | | bool | 0 | Optional options | | no value (flag) with default "
"var_7 | g,G | var-7,var-g | VAL_7 | true | | bool | 0 | Optional options | deprecated | no value (flag) with default "
)
source argparser -- "$@"
# The arguments can now be accessed as keys and values of the
# associative array "args". Further, they are set as variables to the
# environment, from which they are expanded by globbing.
for arg in "${!var@}"; do
printf 'The keyword argument "%s" is set to "%s".\n' \
"${arg}" "${args[${arg}]}"
done | sort
index=1
for arg in "${!pos@}"; do
printf 'The positional argument "%s" on index %s is set to "%s".\n' \
"${arg}" "${index}" "${args[${arg}]}"
(( index++ ))
done