feat: Update optval()

This commit is contained in:
ge 2022-07-20 19:46:38 +03:00
parent 8c8a6504e7
commit 484040ecbe

View File

@ -63,21 +63,25 @@ EOF
optval() { optval() {
# GNU-style CLI options parser. # GNU-style CLI options parser.
# #
# Parse --opt VAL and --opt=VAL options. # Parse `--opt VAL` and `--opt=VAL` options.
# Requires 2 arguments: $1, $2. # Usage: optval "$1" "$2"
# Set variables: # Sets variables:
# opt option name. # opt - option name
# val option value. # val - option's value
# sft value for shift. # sft - value for shift
if [[ "$1" =~ .+=.+ ]]; then opt="${1%%=*}"; val="${1##*=}"; sft=1
opt="${1%%=*}"; val="${1#*=}"; sft=1
elif [[ ! "$1" =~ .+=$ ]] && [ "$2" ] && [ "${2:0:1}" != "-" ]; then if [ "$opt" == "$val" ]; then
opt="$1"; val="$2"; sft=2 if [ -n "$2" ] && [ "${2:0:1}" != "-" ]; then
val="$2"; sft=2
else else
opt="$1" unset val
if [[ "$1" =~ .+=$ ]]; then opt="${1:0: -1}"; fi fi
echo "Error: Missing argument for $opt" >&2; exit 1 fi
if [ -z "$val" ]; then
echo "Missing argument for option $opt" >&2; exit 1
fi fi
} }