You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: bin/argbash
+64-5
Original file line number
Diff line number
Diff line change
@@ -28,6 +28,13 @@
28
28
# Argbash is FREE SOFTWARE, see https://argbash.io for more info
29
29
30
30
31
+
# # When called, the process ends.
32
+
# Args:
33
+
# $1: The exit message (print to stderr)
34
+
# $2: The exit code (default is 1)
35
+
# if env var _PRINT_HELP is set to 'yes', the usage is print to stderr (prior to $1)
36
+
# Example:
37
+
# test -f "$_arg_infile" || _PRINT_HELP=yes die "Can't continue, have to supply file as an argument, got '$_arg_infile'" 4
31
38
die()
32
39
{
33
40
local _ret=$2
@@ -61,6 +68,9 @@ type()
61
68
}
62
69
63
70
71
+
# Function that evaluates whether a value passed to it begins by a character
72
+
# that is a short option of an argument the script knows about.
73
+
# This is required in order to support getopts-like short options grouping.
64
74
begins_with_short_option()
65
75
{
66
76
local first_option all_short_options='otcIhv'
@@ -69,19 +79,24 @@ begins_with_short_option()
69
79
}
70
80
71
81
# THE DEFAULTS INITIALIZATION - POSITIONALS
82
+
# The positional args array has to be reset before the parsing, because it may already be defined
83
+
# - for example if this script is sourced by an argbash-powered script.
72
84
_positionals=()
73
85
_arg_input=
74
86
# THE DEFAULTS INITIALIZATION - OPTIONALS
75
87
_arg_output="-"
76
88
_arg_type="bash-script"
77
89
_arg_library="off"
78
90
_arg_strip="none"
79
-
_arg_check_typos="on"
80
-
_arg_commented="off"
91
+
_arg_check_typos="off"
92
+
_arg_commented="on"
81
93
_arg_search=(".")
82
94
_arg_debug=
83
95
84
96
97
+
# Function that prints general usage of the script.
98
+
# This is useful if users asks for it, or if there is an argument parsing error (unexpected / spurious arguments)
99
+
# and it makes sense to remind the user how the script is supposed to be called.
85
100
print_help()
86
101
{
87
102
printf'%s\n'"Argbash is an argument parser generator for Bash."
@@ -91,8 +106,8 @@ print_help()
91
106
printf'\t%s\n'"-t, --type: Output type to generate (default: 'bash-script')"
92
107
printf'\t%s\n'"--library, --no-library: Whether the input file if the pure parsing library (off by default)"
93
108
printf'\t%s\n'"--strip: Determines what to have in the output (default: 'none')"
94
-
printf'\t%s\n'"--check-typos, --no-check-typos: Whether to check for possible argbash macro typos (on by default)"
95
-
printf'\t%s\n'"-c, --commented, --no-commented: Commented mode - include explanatory comments with the parsing code (off by default)"
109
+
printf'\t%s\n'"--check-typos, --no-check-typos: Whether to check for possible argbash macro typos (off by default)"
110
+
printf'\t%s\n'"-c, --commented, --no-commented: Commented mode - include explanatory comments with the parsing code (on by default)"
96
111
printf'\t%s'"-I, --search: Directories to search for the wrapped scripts (directory of the template will be added to the end of the list) (default array elements:"
97
112
printf" '%s'""."
98
113
printf')\n'
@@ -102,55 +117,81 @@ print_help()
102
117
}
103
118
104
119
120
+
# The parsing of the command-line
105
121
parse_commandline()
106
122
{
107
123
_positionals_count=0
108
124
whiletest$# -gt 0
109
125
do
110
126
_key="$1"
111
127
case"$_key"in
128
+
# We support whitespace as a delimiter between option argument and its value.
129
+
# Therefore, we expect the --output or -o value.
130
+
# so we watch for --output and -o.
131
+
# Since we know that we got the long or short option,
132
+
# we just reach out for the next argument to get the value.
112
133
-o|--output)
113
134
test$# -lt 2 && die "Missing value for the optional argument '$_key'." 1
114
135
_arg_output="$2"
115
136
shift
116
137
;;
138
+
# We support the = as a delimiter between option argument and its value.
139
+
# Therefore, we expect --output=value, so we watch for --output=*
140
+
# For whatever we get, we strip '--output=' using the ${var##--output=} notation
141
+
# to get the argument value
117
142
--output=*)
118
143
_arg_output="${_key##--output=}"
119
144
;;
145
+
# We support getopts-style short arguments grouping,
146
+
# so as -o accepts value, we allow it to be appended to it, so we watch for -o*
147
+
# and we strip the leading -o from the argument string using the ${var##-o} notation.
120
148
-o*)
121
149
_arg_output="${_key##-o}"
122
150
;;
151
+
# See the comment of option '--output' to see what's going on here - principle is the same.
123
152
-t|--type)
124
153
test$# -lt 2 && die "Missing value for the optional argument '$_key'." 1
125
154
_arg_type="$(type "$2""type")"||exit 1
126
155
shift
127
156
;;
157
+
# See the comment of option '--output=' to see what's going on here - principle is the same.
# See the comment of option '--library' to see what's going on here - principle is the same.
146
182
--no-check-typos|--check-typos)
147
183
_arg_check_typos="on"
148
184
test"${1:0:5}" = "--no-"&& _arg_check_typos="off"
149
185
;;
186
+
# See the comment of option '--library' to see what's going on here - principle is the same.
150
187
-c|--no-commented|--commented)
151
188
_arg_commented="on"
152
189
test"${1:0:5}" = "--no-"&& _arg_commented="off"
153
190
;;
191
+
# We support getopts-style short arguments clustering,
192
+
# so as -c doesn't accept value, other short options may be appended to it, so we watch for -c*.
193
+
# After stripping the leading -c from the argument, we have to make sure
194
+
# that the first character that follows coresponds to a short option.
154
195
-c*)
155
196
_arg_commented="on"
156
197
_next="${_key##-c}"
@@ -159,37 +200,46 @@ parse_commandline()
159
200
begins_with_short_option "$_next"&&shift&&set -- "-c""-${_next}""$@"|| die "The short option '$_key' can't be decomposed to ${_key:0:2} and -${_key:2}, because ${_key:0:2} doesn't accept value and '-${_key:2:1}' doesn't correspond to a short option."
160
201
fi
161
202
;;
203
+
# See the comment of option '--output' to see what's going on here - principle is the same.
162
204
-I|--search)
163
205
test$# -lt 2 && die "Missing value for the optional argument '$_key'." 1
164
206
_arg_search+=("$2")
165
207
shift
166
208
;;
209
+
# See the comment of option '--output=' to see what's going on here - principle is the same.
167
210
--search=*)
168
211
_arg_search+=("${_key##--search=}")
169
212
;;
213
+
# See the comment of option '-o' to see what's going on here - principle is the same.
170
214
-I*)
171
215
_arg_search+=("${_key##-I}")
172
216
;;
217
+
# See the comment of option '--output' to see what's going on here - principle is the same.
173
218
--debug)
174
219
test$# -lt 2 && die "Missing value for the optional argument '$_key'." 1
175
220
_arg_debug="$2"
176
221
shift
177
222
;;
223
+
# See the comment of option '--output=' to see what's going on here - principle is the same.
178
224
--debug=*)
179
225
_arg_debug="${_key##--debug=}"
180
226
;;
227
+
# See the comment of option '--library' to see what's going on here - principle is the same.
181
228
-h|--help)
182
229
print_help
183
230
exit 0
184
231
;;
232
+
# See the comment of option '-c' to see what's going on here - principle is the same.
185
233
-h*)
186
234
print_help
187
235
exit 0
188
236
;;
237
+
# See the comment of option '--library' to see what's going on here - principle is the same.
189
238
-v|--version)
190
239
printf'%s %s\n\n%s\n'"argbash""2.8.0"'Argbash is an argument parser generator for Bash.'
191
240
exit 0
192
241
;;
242
+
# See the comment of option '-c' to see what's going on here - principle is the same.
193
243
-v*)
194
244
printf'%s %s\n\n%s\n'"argbash""2.8.0"'Argbash is an argument parser generator for Bash.'
195
245
exit 0
@@ -205,6 +255,9 @@ parse_commandline()
205
255
}
206
256
207
257
258
+
# Check that we receive expected amount positional arguments.
259
+
# Return 0 if everything is OK, 1 if we have too little arguments
260
+
# and 2 if we have too much arguments
208
261
handle_passed_args_count()
209
262
{
210
263
local _required_args_string="'input'"
@@ -213,9 +266,14 @@ handle_passed_args_count()
213
266
}
214
267
215
268
269
+
# Take arguments that we have received, and save them in variables of given names.
270
+
# The 'eval' command is needed as the name of target variable is saved into another variable.
216
271
assign_positional_args()
217
272
{
218
273
local _positional_name _shift_for=$1
274
+
# We have an array of variables to which we want to save positional args values.
275
+
# This array is able to hold array elements as targets.
276
+
# As variables don't contain spaces, they may be held in space-separated string.
219
277
_positional_names="_arg_input "
220
278
221
279
shift"$_shift_for"
@@ -227,6 +285,7 @@ assign_positional_args()
227
285
done
228
286
}
229
287
288
+
# Now call all the functions defined above that are needed to get the job done
230
289
parse_commandline "$@"
231
290
handle_passed_args_count
232
291
assign_positional_args 1 "${_positionals[@]}"
@@ -460,7 +519,7 @@ then
460
519
# match against suspicious, then inverse match against correct stuff:
461
520
# #<optional whitespace>\(allowed\|another allowed\|...\)<optional whitespace><opening bracket <or> end of line>
462
521
# Then, extract all matches (assumed to be alnum chars + '_') from grep and put them in the error msg.
Copy file name to clipboardExpand all lines: bin/argbash-1to2
+50
Original file line number
Diff line number
Diff line change
@@ -16,6 +16,13 @@ version=2.8.0
16
16
# Argbash is FREE SOFTWARE, see https://argbash.io for more info
17
17
18
18
19
+
# # When called, the process ends.
20
+
# Args:
21
+
# $1: The exit message (print to stderr)
22
+
# $2: The exit code (default is 1)
23
+
# if env var _PRINT_HELP is set to 'yes', the usage is print to stderr (prior to $1)
24
+
# Example:
25
+
# test -f "$_arg_infile" || _PRINT_HELP=yes die "Can't continue, have to supply file as an argument, got '$_arg_infile'" 4
19
26
die()
20
27
{
21
28
local _ret=$2
@@ -26,6 +33,9 @@ die()
26
33
}
27
34
28
35
36
+
# Function that evaluates whether a value passed to it begins by a character
37
+
# that is a short option of an argument the script knows about.
38
+
# This is required in order to support getopts-like short options grouping.
29
39
begins_with_short_option()
30
40
{
31
41
local first_option all_short_options='ovh'
@@ -34,12 +44,17 @@ begins_with_short_option()
34
44
}
35
45
36
46
# THE DEFAULTS INITIALIZATION - POSITIONALS
47
+
# The positional args array has to be reset before the parsing, because it may already be defined
48
+
# - for example if this script is sourced by an argbash-powered script.
37
49
_positionals=()
38
50
_arg_input=('' )
39
51
# THE DEFAULTS INITIALIZATION - OPTIONALS
40
52
_arg_output=""
41
53
42
54
55
+
# Function that prints general usage of the script.
56
+
# This is useful if users asks for it, or if there is an argument parsing error (unexpected / spurious arguments)
57
+
# and it makes sense to remind the user how the script is supposed to be called.
43
58
print_help()
44
59
{
45
60
printf'%s\n'"Convert a template for argbash>=1,<2 to argbash>=2,<3"
@@ -51,36 +66,57 @@ print_help()
51
66
}
52
67
53
68
69
+
# The parsing of the command-line
54
70
parse_commandline()
55
71
{
56
72
_positionals_count=0
57
73
whiletest$# -gt 0
58
74
do
59
75
_key="$1"
60
76
case"$_key"in
77
+
# We support whitespace as a delimiter between option argument and its value.
78
+
# Therefore, we expect the --output or -o value.
79
+
# so we watch for --output and -o.
80
+
# Since we know that we got the long or short option,
81
+
# we just reach out for the next argument to get the value.
61
82
-o|--output)
62
83
test$# -lt 2 && die "Missing value for the optional argument '$_key'." 1
63
84
_arg_output="$2"
64
85
shift
65
86
;;
87
+
# We support the = as a delimiter between option argument and its value.
88
+
# Therefore, we expect --output=value, so we watch for --output=*
89
+
# For whatever we get, we strip '--output=' using the ${var##--output=} notation
90
+
# to get the argument value
66
91
--output=*)
67
92
_arg_output="${_key##--output=}"
68
93
;;
94
+
# We support getopts-style short arguments grouping,
95
+
# so as -o accepts value, we allow it to be appended to it, so we watch for -o*
96
+
# and we strip the leading -o from the argument string using the ${var##-o} notation.
69
97
-o*)
70
98
_arg_output="${_key##-o}"
71
99
;;
100
+
# The version argurment doesn't accept a value,
101
+
# we expect the --version or -v, so we watch for them.
72
102
-v|--version)
73
103
echo"argbash-1to2 v$version"
74
104
exit 0
75
105
;;
106
+
# We support getopts-style short arguments clustering,
107
+
# so as -v doesn't accept value, other short options may be appended to it, so we watch for -v*.
108
+
# After stripping the leading -v from the argument, we have to make sure
109
+
# that the first character that follows coresponds to a short option.
76
110
-v*)
77
111
echo"argbash-1to2 v$version"
78
112
exit 0
79
113
;;
114
+
# See the comment of option '--version' to see what's going on here - principle is the same.
80
115
-h|--help)
81
116
print_help
82
117
exit 0
83
118
;;
119
+
# See the comment of option '-v' to see what's going on here - principle is the same.
84
120
-h*)
85
121
print_help
86
122
exit 0
@@ -96,17 +132,30 @@ parse_commandline()
96
132
}
97
133
98
134
135
+
# Check that we receive expected amount positional arguments.
136
+
# Return 0 if everything is OK, 1 if we have too little arguments
137
+
# and 2 if we have too much arguments
99
138
handle_passed_args_count()
100
139
{
101
140
local _required_args_string="'input'"
102
141
test"${_positionals_count}" -ge 1 || _PRINT_HELP=yes die "FATAL ERROR: Not enough positional arguments - we require at least 1 (namely: $_required_args_string), but got only ${_positionals_count}." 1
142
+
# We accept up to inifinitely many positional values, so
143
+
# there is no need to check for spurious positional arguments.
103
144
}
104
145
105
146
147
+
# Take arguments that we have received, and save them in variables of given names.
148
+
# The 'eval' command is needed as the name of target variable is saved into another variable.
106
149
assign_positional_args()
107
150
{
108
151
local _positional_name _shift_for=$1
152
+
# We have an array of variables to which we want to save positional args values.
153
+
# This array is able to hold array elements as targets.
154
+
# As variables don't contain spaces, they may be held in space-separated string.
109
155
_positional_names="_arg_input "
156
+
# If we allow up to infinitely many args, we calculate how many of values
157
+
# were actually passed, and we extend the target array accordingly.
158
+
# We also know that we have _pos_names_count known positional arguments.
110
159
_our_args=$((${#_positionals[@]}-1))
111
160
for((ii =0; ii < _our_args; ii++))
112
161
do
@@ -122,6 +171,7 @@ assign_positional_args()
122
171
done
123
172
}
124
173
174
+
# Now call all the functions defined above that are needed to get the job done
0 commit comments