Skip to content

Commit 5fee098

Browse files
committed
Half-way through.
1 parent 903d930 commit 5fee098

File tree

5 files changed

+178
-12
lines changed

5 files changed

+178
-12
lines changed

bin/argbash

+64-5
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,13 @@
2828
# Argbash is FREE SOFTWARE, see https://argbash.io for more info
2929

3030

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
3138
die()
3239
{
3340
local _ret=$2
@@ -61,6 +68,9 @@ type()
6168
}
6269

6370

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.
6474
begins_with_short_option()
6575
{
6676
local first_option all_short_options='otcIhv'
@@ -69,19 +79,24 @@ begins_with_short_option()
6979
}
7080

7181
# 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.
7284
_positionals=()
7385
_arg_input=
7486
# THE DEFAULTS INITIALIZATION - OPTIONALS
7587
_arg_output="-"
7688
_arg_type="bash-script"
7789
_arg_library="off"
7890
_arg_strip="none"
79-
_arg_check_typos="on"
80-
_arg_commented="off"
91+
_arg_check_typos="off"
92+
_arg_commented="on"
8193
_arg_search=(".")
8294
_arg_debug=
8395

8496

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.
85100
print_help()
86101
{
87102
printf '%s\n' "Argbash is an argument parser generator for Bash."
@@ -91,8 +106,8 @@ print_help()
91106
printf '\t%s\n' "-t, --type: Output type to generate (default: 'bash-script')"
92107
printf '\t%s\n' "--library, --no-library: Whether the input file if the pure parsing library (off by default)"
93108
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)"
96111
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:"
97112
printf " '%s'" "."
98113
printf ')\n'
@@ -102,55 +117,81 @@ print_help()
102117
}
103118

104119

120+
# The parsing of the command-line
105121
parse_commandline()
106122
{
107123
_positionals_count=0
108124
while test $# -gt 0
109125
do
110126
_key="$1"
111127
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.
112133
-o|--output)
113134
test $# -lt 2 && die "Missing value for the optional argument '$_key'." 1
114135
_arg_output="$2"
115136
shift
116137
;;
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
117142
--output=*)
118143
_arg_output="${_key##--output=}"
119144
;;
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.
120148
-o*)
121149
_arg_output="${_key##-o}"
122150
;;
151+
# See the comment of option '--output' to see what's going on here - principle is the same.
123152
-t|--type)
124153
test $# -lt 2 && die "Missing value for the optional argument '$_key'." 1
125154
_arg_type="$(type "$2" "type")" || exit 1
126155
shift
127156
;;
157+
# See the comment of option '--output=' to see what's going on here - principle is the same.
128158
--type=*)
129159
_arg_type="$(type "${_key##--type=}" "type")" || exit 1
130160
;;
161+
# See the comment of option '-o' to see what's going on here - principle is the same.
131162
-t*)
132163
_arg_type="$(type "${_key##-t}" "type")" || exit 1
133164
;;
165+
# The library argurment doesn't accept a value,
166+
# we expect the --library, so we watch for it.
134167
--no-library|--library)
135168
_arg_library="on"
136169
test "${1:0:5}" = "--no-" && _arg_library="off"
137170
;;
171+
# See the comment of option '--output' to see what's going on here - principle is the same.
138172
--strip)
139173
test $# -lt 2 && die "Missing value for the optional argument '$_key'." 1
140174
_arg_strip="$(content "$2" "strip")" || exit 1
141175
shift
142176
;;
177+
# See the comment of option '--output=' to see what's going on here - principle is the same.
143178
--strip=*)
144179
_arg_strip="$(content "${_key##--strip=}" "strip")" || exit 1
145180
;;
181+
# See the comment of option '--library' to see what's going on here - principle is the same.
146182
--no-check-typos|--check-typos)
147183
_arg_check_typos="on"
148184
test "${1:0:5}" = "--no-" && _arg_check_typos="off"
149185
;;
186+
# See the comment of option '--library' to see what's going on here - principle is the same.
150187
-c|--no-commented|--commented)
151188
_arg_commented="on"
152189
test "${1:0:5}" = "--no-" && _arg_commented="off"
153190
;;
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.
154195
-c*)
155196
_arg_commented="on"
156197
_next="${_key##-c}"
@@ -159,37 +200,46 @@ parse_commandline()
159200
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."
160201
fi
161202
;;
203+
# See the comment of option '--output' to see what's going on here - principle is the same.
162204
-I|--search)
163205
test $# -lt 2 && die "Missing value for the optional argument '$_key'." 1
164206
_arg_search+=("$2")
165207
shift
166208
;;
209+
# See the comment of option '--output=' to see what's going on here - principle is the same.
167210
--search=*)
168211
_arg_search+=("${_key##--search=}")
169212
;;
213+
# See the comment of option '-o' to see what's going on here - principle is the same.
170214
-I*)
171215
_arg_search+=("${_key##-I}")
172216
;;
217+
# See the comment of option '--output' to see what's going on here - principle is the same.
173218
--debug)
174219
test $# -lt 2 && die "Missing value for the optional argument '$_key'." 1
175220
_arg_debug="$2"
176221
shift
177222
;;
223+
# See the comment of option '--output=' to see what's going on here - principle is the same.
178224
--debug=*)
179225
_arg_debug="${_key##--debug=}"
180226
;;
227+
# See the comment of option '--library' to see what's going on here - principle is the same.
181228
-h|--help)
182229
print_help
183230
exit 0
184231
;;
232+
# See the comment of option '-c' to see what's going on here - principle is the same.
185233
-h*)
186234
print_help
187235
exit 0
188236
;;
237+
# See the comment of option '--library' to see what's going on here - principle is the same.
189238
-v|--version)
190239
printf '%s %s\n\n%s\n' "argbash" "2.8.0" 'Argbash is an argument parser generator for Bash.'
191240
exit 0
192241
;;
242+
# See the comment of option '-c' to see what's going on here - principle is the same.
193243
-v*)
194244
printf '%s %s\n\n%s\n' "argbash" "2.8.0" 'Argbash is an argument parser generator for Bash.'
195245
exit 0
@@ -205,6 +255,9 @@ parse_commandline()
205255
}
206256

207257

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
208261
handle_passed_args_count()
209262
{
210263
local _required_args_string="'input'"
@@ -213,9 +266,14 @@ handle_passed_args_count()
213266
}
214267

215268

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.
216271
assign_positional_args()
217272
{
218273
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.
219277
_positional_names="_arg_input "
220278

221279
shift "$_shift_for"
@@ -227,6 +285,7 @@ assign_positional_args()
227285
done
228286
}
229287

288+
# Now call all the functions defined above that are needed to get the job done
230289
parse_commandline "$@"
231290
handle_passed_args_count
232291
assign_positional_args 1 "${_positionals[@]}"
@@ -460,7 +519,7 @@ then
460519
# match against suspicious, then inverse match against correct stuff:
461520
# #<optional whitespace>\(allowed\|another allowed\|...\)<optional whitespace><opening bracket <or> end of line>
462521
# Then, extract all matches (assumed to be alnum chars + '_') from grep and put them in the error msg.
463-
grep_output="$(printf "%s" "$output" | grep '^#\s*\(ARG_\|ARGBASH\)' | grep -v '^#\s*\(ARGBASH_SET_INDENT\|ARG_OPTIONAL_SINGLE\|ARG_VERSION\|ARG_VERSION_AUTO\|ARG_HELP\|ARG_OPTIONAL_INCREMENTAL\|ARG_OPTIONAL_REPEATED\|ARG_VERBOSE\|ARG_OPTIONAL_BOOLEAN\|ARG_OPTIONAL_ACTION\|ARG_POSITIONAL_SINGLE\|ARG_POSITIONAL_INF\|ARG_POSITIONAL_MULTI\|ARG_POSITIONAL_DOUBLEDASH\|ARG_OPTION_STACKING\|ARG_RESTRICT_VALUES\|ARG_DEFAULTS_POS\|ARG_LEFTOVERS\|ARGBASH_WRAP\|INCLUDE_PARSING_CODE\|DEFINE_SCRIPT_DIR\|ARGBASH_SET_DELIM\|ARGBASH_GO\|ARGBASH_PREPARE\|ARG_TYPE_GROUP\|ARG_TYPE_GROUP_SET\|ARG_USE_ENV\|ARG_USE_PROG\)\s*\((\|$\)' | sed -e 's/#\s*\([[:alnum:]_]*\).*/\1 /' | tr -d '\n\r')"
522+
grep_output="$(printf "%s" "$output" | grep '^#\s*\(ARG_\|ARGBASH\)' | grep -v '^#\s*\(ARGBASH_SET_INDENT\|ARG_OPTIONAL_SINGLE\|ARG_VERSION\|ARG_VERSION_AUTO\|ARG_HELP\|ARG_OPTIONAL_INCREMENTAL\|ARG_OPTIONAL_REPEATED\|ARG_VERBOSE\|ARG_OPTIONAL_BOOLEAN\|ARG_OPTIONAL_SWITCH_ON\|ARG_OPTIONAL_SWITCH_OFF\|ARG_OPTIONAL_ACTION\|ARG_POSITIONAL_SINGLE\|ARG_POSITIONAL_INF\|ARG_POSITIONAL_MULTI\|ARG_POSITIONAL_DOUBLEDASH\|ARG_OPTION_STACKING\|ARG_RESTRICT_VALUES\|ARG_DEFAULTS_POS\|ARG_LEFTOVERS\|ARGBASH_WRAP\|INCLUDE_PARSING_CODE\|DEFINE_SCRIPT_DIR\|ARGBASH_SET_DELIM\|ARGBASH_GO\|ARGBASH_PREPARE\|ARG_TYPE_GROUP\|ARG_TYPE_GROUP_SET\|ARG_USE_ENV\|ARG_USE_PROG\)\s*\((\|$\)' | sed -e 's/#\s*\([[:alnum:]_]*\).*/\1 /' | tr -d '\n\r')"
464523
test -n "$grep_output" && die "Your script contains possible misspelled Argbash macros: $grep_output" 1
465524
fi
466525
if test "$outfname" != '-'

bin/argbash-1to2

+50
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,13 @@ version=2.8.0
1616
# Argbash is FREE SOFTWARE, see https://argbash.io for more info
1717

1818

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
1926
die()
2027
{
2128
local _ret=$2
@@ -26,6 +33,9 @@ die()
2633
}
2734

2835

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.
2939
begins_with_short_option()
3040
{
3141
local first_option all_short_options='ovh'
@@ -34,12 +44,17 @@ begins_with_short_option()
3444
}
3545

3646
# 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.
3749
_positionals=()
3850
_arg_input=('' )
3951
# THE DEFAULTS INITIALIZATION - OPTIONALS
4052
_arg_output=""
4153

4254

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.
4358
print_help()
4459
{
4560
printf '%s\n' "Convert a template for argbash>=1,<2 to argbash>=2,<3"
@@ -51,36 +66,57 @@ print_help()
5166
}
5267

5368

69+
# The parsing of the command-line
5470
parse_commandline()
5571
{
5672
_positionals_count=0
5773
while test $# -gt 0
5874
do
5975
_key="$1"
6076
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.
6182
-o|--output)
6283
test $# -lt 2 && die "Missing value for the optional argument '$_key'." 1
6384
_arg_output="$2"
6485
shift
6586
;;
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
6691
--output=*)
6792
_arg_output="${_key##--output=}"
6893
;;
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.
6997
-o*)
7098
_arg_output="${_key##-o}"
7199
;;
100+
# The version argurment doesn't accept a value,
101+
# we expect the --version or -v, so we watch for them.
72102
-v|--version)
73103
echo "argbash-1to2 v$version"
74104
exit 0
75105
;;
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.
76110
-v*)
77111
echo "argbash-1to2 v$version"
78112
exit 0
79113
;;
114+
# See the comment of option '--version' to see what's going on here - principle is the same.
80115
-h|--help)
81116
print_help
82117
exit 0
83118
;;
119+
# See the comment of option '-v' to see what's going on here - principle is the same.
84120
-h*)
85121
print_help
86122
exit 0
@@ -96,17 +132,30 @@ parse_commandline()
96132
}
97133

98134

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
99138
handle_passed_args_count()
100139
{
101140
local _required_args_string="'input'"
102141
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.
103144
}
104145

105146

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.
106149
assign_positional_args()
107150
{
108151
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.
109155
_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.
110159
_our_args=$((${#_positionals[@]} - 1))
111160
for ((ii = 0; ii < _our_args; ii++))
112161
do
@@ -122,6 +171,7 @@ assign_positional_args()
122171
done
123172
}
124173

174+
# Now call all the functions defined above that are needed to get the job done
125175
parse_commandline "$@"
126176
handle_passed_args_count
127177
assign_positional_args 1 "${_positionals[@]}"

0 commit comments

Comments
 (0)