mirror of
https://github.com/YunoHost/yunohost.git
synced 2024-09-03 20:06:10 +02:00
[fix] Update ynh_handle_getopts_args
Fix ynh_handle_getopts_args from https://github.com/YunoHost/yunohost/pull/561 -2094557e1e (diff-a1f538c2eae234f6ba78e0bed0ae54db)
-976f160afb (diff-a1f538c2eae234f6ba78e0bed0ae54db)
-50f3291ea7 (diff-a1f538c2eae234f6ba78e0bed0ae54db)
-540291a7e0 (diff-a1f538c2eae234f6ba78e0bed0ae54db)
This commit is contained in:
parent
0bc8300b88
commit
8ffd7e87f5
1 changed files with 32 additions and 23 deletions
|
@ -53,33 +53,33 @@ ynh_handle_getopts_args () {
|
||||||
|
|
||||||
# For each option in the array, reduce to short options for getopts (e.g. for [u]=user, --user will be -u)
|
# For each option in the array, reduce to short options for getopts (e.g. for [u]=user, --user will be -u)
|
||||||
# And built parameters string for getopts
|
# And built parameters string for getopts
|
||||||
# ${!args_array[@]} is the list of all keys in the array (A key is 'u' in [u]=user, user is a value)
|
# ${!args_array[@]} is the list of all option_flags in the array (An option_flag is 'u' in [u]=user, user is a value)
|
||||||
local getopts_parameters=""
|
local getopts_parameters=""
|
||||||
local key=""
|
local option_flag=""
|
||||||
for key in "${!args_array[@]}"
|
for option_flag in "${!args_array[@]}"
|
||||||
do
|
do
|
||||||
# Concatenate each keys of the array to build the string of arguments for getopts
|
# Concatenate each option_flags of the array to build the string of arguments for getopts
|
||||||
# Will looks like 'abcd' for -a -b -c -d
|
# Will looks like 'abcd' for -a -b -c -d
|
||||||
# If the value of a key finish by =, it's an option with additionnal values. (e.g. --user bob or -u bob)
|
# If the value of an option_flag finish by =, it's an option with additionnal values. (e.g. --user bob or -u bob)
|
||||||
# Check the last character of the value associate to the key
|
# Check the last character of the value associate to the option_flag
|
||||||
if [ "${args_array[$key]: -1}" = "=" ]
|
if [ "${args_array[$option_flag]: -1}" = "=" ]
|
||||||
then
|
then
|
||||||
# For an option with additionnal values, add a ':' after the letter for getopts.
|
# For an option with additionnal values, add a ':' after the letter for getopts.
|
||||||
getopts_parameters="${getopts_parameters}${key}:"
|
getopts_parameters="${getopts_parameters}${option_flag}:"
|
||||||
else
|
else
|
||||||
getopts_parameters="${getopts_parameters}${key}"
|
getopts_parameters="${getopts_parameters}${option_flag}"
|
||||||
fi
|
fi
|
||||||
# Check each argument given to the function
|
# Check each argument given to the function
|
||||||
local arg=""
|
local arg=""
|
||||||
# ${#arguments[@]} is the size of the array
|
# ${#arguments[@]} is the size of the array
|
||||||
for arg in `seq 0 $(( ${#arguments[@]} - 1 ))`
|
for arg in `seq 0 $(( ${#arguments[@]} - 1 ))`
|
||||||
do
|
do
|
||||||
# And replace long option (value of the key) by the short option, the key itself
|
# And replace long option (value of the option_flag) by the short option, the option_flag itself
|
||||||
# (e.g. for [u]=user, --user will be -u)
|
# (e.g. for [u]=user, --user will be -u)
|
||||||
# Replace long option with =
|
# Replace long option with =
|
||||||
arguments[arg]="${arguments[arg]//--${args_array[$key]}/-${key} }"
|
arguments[arg]="${arguments[arg]//--${args_array[$option_flag]}/-${option_flag} }"
|
||||||
# And long option without =
|
# And long option without =
|
||||||
arguments[arg]="${arguments[arg]//--${args_array[$key]%=}/-${key}}"
|
arguments[arg]="${arguments[arg]//--${args_array[$option_flag]%=}/-${option_flag}}"
|
||||||
done
|
done
|
||||||
done
|
done
|
||||||
|
|
||||||
|
@ -98,10 +98,10 @@ ynh_handle_getopts_args () {
|
||||||
|
|
||||||
if [ "$parameter" = "?" ]
|
if [ "$parameter" = "?" ]
|
||||||
then
|
then
|
||||||
ynh_die "Invalid argument: -${OPTARG:-}"
|
ynh_die --message="Invalid argument: -${OPTARG:-}"
|
||||||
elif [ "$parameter" = ":" ]
|
elif [ "$parameter" = ":" ]
|
||||||
then
|
then
|
||||||
ynh_die "-$OPTARG parameter requires an argument."
|
ynh_die --message="-$OPTARG parameter requires an argument."
|
||||||
else
|
else
|
||||||
local shift_value=1
|
local shift_value=1
|
||||||
# Use the long option, corresponding to the short option read by getopts, as a variable
|
# Use the long option, corresponding to the short option read by getopts, as a variable
|
||||||
|
@ -132,10 +132,11 @@ ynh_handle_getopts_args () {
|
||||||
# Declare the content of option_var as a variable.
|
# Declare the content of option_var as a variable.
|
||||||
eval ${option_var}=""
|
eval ${option_var}=""
|
||||||
# Then read the array value per value
|
# Then read the array value per value
|
||||||
|
local i
|
||||||
for i in `seq 0 $(( ${#all_args[@]} - 1 ))`
|
for i in `seq 0 $(( ${#all_args[@]} - 1 ))`
|
||||||
do
|
do
|
||||||
# If this argument is an option, end here.
|
# If this argument is an option, end here.
|
||||||
if [ "${all_args[$i]:0:1}" == "-" ] || [ -z "${all_args[$i]}" ]
|
if [ "${all_args[$i]:0:1}" == "-" ]
|
||||||
then
|
then
|
||||||
# Ignore the first value of the array, which is the option itself
|
# Ignore the first value of the array, which is the option itself
|
||||||
if [ "$i" -ne 0 ]; then
|
if [ "$i" -ne 0 ]; then
|
||||||
|
@ -165,25 +166,33 @@ ynh_handle_getopts_args () {
|
||||||
# Check if there's getopts arguments
|
# Check if there's getopts arguments
|
||||||
if [ "${arguments[0]:0:1}" != "-" ]
|
if [ "${arguments[0]:0:1}" != "-" ]
|
||||||
then
|
then
|
||||||
# If not, enter in legacy mode and manage the arguments as positionnal ones.
|
# If not, enter in legacy mode and manage the arguments as positionnal ones..
|
||||||
echo "! Helper used in legacy mode !"
|
# Dot not echo, to prevent to go through a helper output. But print only in the log.
|
||||||
|
set -x; echo "! Helper used in legacy mode !" > /dev/null; set +x
|
||||||
|
local i
|
||||||
for i in `seq 0 $(( ${#arguments[@]} -1 ))`
|
for i in `seq 0 $(( ${#arguments[@]} -1 ))`
|
||||||
do
|
do
|
||||||
# Use getopts_parameters as a list of key of the array args_array
|
# Try to use legacy_args as a list of option_flag of the array args_array
|
||||||
|
# Otherwise, fallback to getopts_parameters to get the option_flag. But an associative arrays isn't always sorted in the correct order...
|
||||||
# Remove all ':' in getopts_parameters
|
# Remove all ':' in getopts_parameters
|
||||||
getopts_parameters=${getopts_parameters//:}
|
getopts_parameters=${legacy_args:-${getopts_parameters//:}}
|
||||||
# Get the key from getopts_parameters, by using the key according to the position of the argument.
|
# Get the option_flag from getopts_parameters, by using the option_flag according to the position of the argument.
|
||||||
key=${getopts_parameters:$i:1}
|
option_flag=${getopts_parameters:$i:1}
|
||||||
# Use the long option, corresponding to the key, as a variable
|
if [ -z "$option_flag" ]; then
|
||||||
|
ynh_print_warn --message="Too many arguments ! \"${arguments[$i]}\" will be ignored."
|
||||||
|
continue
|
||||||
|
fi
|
||||||
|
# Use the long option, corresponding to the option_flag, as a variable
|
||||||
# (e.g. for [u]=user, 'user' will be used as a variable)
|
# (e.g. for [u]=user, 'user' will be used as a variable)
|
||||||
# Also, remove '=' at the end of the long option
|
# Also, remove '=' at the end of the long option
|
||||||
# The variable name will be stored in 'option_var'
|
# The variable name will be stored in 'option_var'
|
||||||
local option_var="${args_array[$key]%=}"
|
local option_var="${args_array[$option_flag]%=}"
|
||||||
|
|
||||||
# Store each value given as argument in the corresponding variable
|
# Store each value given as argument in the corresponding variable
|
||||||
# The values will be stored in the same order than $args_array
|
# The values will be stored in the same order than $args_array
|
||||||
eval ${option_var}+=\"${arguments[$i]}\"
|
eval ${option_var}+=\"${arguments[$i]}\"
|
||||||
done
|
done
|
||||||
|
unset legacy_args
|
||||||
else
|
else
|
||||||
# END LEGACY MODE
|
# END LEGACY MODE
|
||||||
# Call parse_arg and pass the modified list of args as an array of arguments.
|
# Call parse_arg and pass the modified list of args as an array of arguments.
|
||||||
|
|
Loading…
Add table
Reference in a new issue