Skip to content

Commit 712935f

Browse files
author
Damien Nadé
committed
argsparse.sh: __argsparse_parse_options_no_usage: refactored short to long conversion to new argsparse_short_to_long function
1 parent a83c7f3 commit 712935f

1 file changed

Lines changed: 16 additions & 5 deletions

File tree

argsparse.sh

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1093,6 +1093,18 @@ __argsparse_parse_options_check_exclusions() {
10931093
return 1
10941094
}
10951095

1096+
## @fn argsparse_short_to_long()
1097+
## @brief
1098+
## @param char a short option letter
1099+
## @return if found, the long option name matching given short option
1100+
## letter.
1101+
argsparse_short_to_long() {
1102+
[[ $# -eq 1 ]] || return 1
1103+
local char=$1
1104+
local long=${__argsparse_short_options[$char]}
1105+
[[ -n "$long" ]] && printf %s "$long"
1106+
}
1107+
10961108
## @fn __argsparse_set_option()
10971109
## @private
10981110
## @brief Resolv option-specific setter function and invoke it.
@@ -1136,7 +1148,7 @@ __argsparse_parse_options_no_usage() {
11361148
# Be careful, the function is (too) big.
11371149

11381150
local long short getopt_temp next_param set_hook option_type
1139-
local next_param_identifier exclude value
1151+
local next_param_identifier exclude value char
11401152
local -a longs_array
11411153
local -A exclusions
11421154
# The getopt parameters.
@@ -1232,18 +1244,17 @@ __argsparse_parse_options_no_usage() {
12321244
# matching long name.
12331245
if [[ "$next_param" = -[!-] ]]
12341246
then
1235-
next_param=${next_param#-}
1236-
if [[ -z "${__argsparse_short_options[$next_param]}" ]]
1247+
char=${next_param#-}
1248+
if ! next_param=$(argsparse_short_to_long "$char")
12371249
then
12381250
# Short option without equivalent long. According to
12391251
# current implementation, this should be considered as
12401252
# a bug.
12411253
printf >&2 \
12421254
"%s: -%s: option doesnt have any matching long option." \
1243-
"$argsparse_pgm" "$next_param"
1255+
"$argsparse_pgm" "$char"
12441256
return 1
12451257
fi
1246-
next_param=${__argsparse_short_options[$next_param]}
12471258
else
12481259
# Wasnt a short option. Just strip the leading dash.
12491260
next_param=${next_param#--}

0 commit comments

Comments
 (0)