Fix getopts with empty parameters

This commit is contained in:
Maniack Crudelis 2020-04-21 16:24:49 +02:00
parent 7cc04f5171
commit f72be82429

View file

@ -147,26 +147,30 @@ ynh_handle_getopts_args () {
break break
fi fi
else else
# Else, add this value to this option # Ignore empty parameters
# Each value will be separated by ';' if [ -n "${all_args[$i]}" ]
if [ -n "${!option_var}" ] then
then # Else, add this value to this option
# If there's already another value for this option, add a ; before adding the new value # Each value will be separated by ';'
eval ${option_var}+="\;" if [ -n "${!option_var}" ]
fi then
# If there's already another value for this option, add a ; before adding the new value
eval ${option_var}+="\;"
fi
# Remove the \ that escape - at beginning of values. # Remove the \ that escape - at beginning of values.
all_args[i]="${all_args[i]//\\TOBEREMOVED\\/}" all_args[i]="${all_args[i]//\\TOBEREMOVED\\/}"
# For the record. # For the record.
# We're using eval here to get the content of the variable stored itself as simple text in $option_var... # We're using eval here to get the content of the variable stored itself as simple text in $option_var...
# Other ways to get that content would be to use either ${!option_var} or declare -g ${option_var} # Other ways to get that content would be to use either ${!option_var} or declare -g ${option_var}
# But... ${!option_var} can't be used as left part of an assignation. # But... ${!option_var} can't be used as left part of an assignation.
# declare -g ${option_var} will create a local variable (despite -g !) and will not be available for the helper itself. # declare -g ${option_var} will create a local variable (despite -g !) and will not be available for the helper itself.
# So... Stop fucking arguing each time that eval is evil... Go find an other working solution if you can find one! # So... Stop fucking arguing each time that eval is evil... Go find an other working solution if you can find one!
eval ${option_var}+='"${all_args[$i]}"' eval ${option_var}+='"${all_args[$i]}"'
shift_value=$(( shift_value + 1 )) fi
shift_value=$(( shift_value + 1 ))
fi fi
done done
fi fi