From 484040ecbedd2ec71db77bacd03a544dbc9bd582 Mon Sep 17 00:00:00 2001 From: ge Date: Wed, 20 Jul 2022 19:46:38 +0300 Subject: [PATCH] feat: Update optval() --- src/boring-backup | 32 ++++++++++++++++++-------------- 1 file changed, 18 insertions(+), 14 deletions(-) diff --git a/src/boring-backup b/src/boring-backup index a7220c6..c1efdad 100755 --- a/src/boring-backup +++ b/src/boring-backup @@ -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 - else - opt="$1" - if [[ "$1" =~ .+=$ ]]; then opt="${1:0: -1}"; fi - echo "Error: Missing argument for $opt" >&2; exit 1 + opt="${1%%=*}"; val="${1##*=}"; sft=1 + + if [ "$opt" == "$val" ]; then + if [ -n "$2" ] && [ "${2:0:1}" != "-" ]; then + val="$2"; sft=2 + else + unset val + fi + fi + + if [ -z "$val" ]; then + echo "Missing argument for option $opt" >&2; exit 1 fi }