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