diff --git a/main.sh b/main.sh index 461093d..85e9b76 100755 --- a/main.sh +++ b/main.sh @@ -131,36 +131,44 @@ EOF # Translate usage string -> getopts arguments, and set $arg_ defaults while read __b3bp_tmp_line; do - # fetch single character version of option string - __b3bp_tmp_opt="$(echo "${__b3bp_tmp_line}" |awk '{print $1}' |sed -e 's#^-##')" + if echo "${__b3bp_tmp_line}" |egrep '^-' >/dev/null 2>&1; then + # fetch single character version of option string + __b3bp_tmp_opt="$(echo "${__b3bp_tmp_line}" |awk '{print $1}' |sed -e 's#^-##')" - # fetch long version if present - __b3bp_tmp_long_opt="$(echo "${__b3bp_tmp_line}" |awk '/\-\-/ {print $2}' |sed -e 's#^--##')" - __b3bp_tmp_long_opt_mangled="$(sed 's#-#_#g' <<< $__b3bp_tmp_long_opt)" + # fetch long version if present + __b3bp_tmp_long_opt="$(echo "${__b3bp_tmp_line}" |awk '/\-\-/ {print $2}' |sed -e 's#^--##')" + __b3bp_tmp_long_opt_mangled="$(sed 's#-#_#g' <<< $__b3bp_tmp_long_opt)" - # map long name back to short name - __b3bp_tmp_varname="__b3bp_tmp_short_opt_${__b3bp_tmp_long_opt_mangled}" - eval "${__b3bp_tmp_varname}=\"${__b3bp_tmp_opt}\"" + # map long name back to short name + __b3bp_tmp_varname="__b3bp_tmp_short_opt_${__b3bp_tmp_long_opt_mangled}" + eval "${__b3bp_tmp_varname}=\"${__b3bp_tmp_opt}\"" - # check if option takes an argument - __b3bp_tmp_varname="__b3bp_tmp_has_arg_${__b3bp_tmp_opt}" - if ! echo "${__b3bp_tmp_line}" |egrep '\[.*\]' >/dev/null 2>&1; then - __b3bp_tmp_init="0" # it's a flag. init with 0 - eval "${__b3bp_tmp_varname}=0" - else - __b3bp_tmp_opt="${__b3bp_tmp_opt}:" # add : if opt has arg - __b3bp_tmp_init="" # it has an arg. init with "" - eval "${__b3bp_tmp_varname}=1" + # check if option takes an argument + __b3bp_tmp_varname="__b3bp_tmp_has_arg_${__b3bp_tmp_opt}" + if ! echo "${__b3bp_tmp_line}" |egrep '\[.*\]' >/dev/null 2>&1; then + __b3bp_tmp_init="0" # it's a flag. init with 0 + eval "${__b3bp_tmp_varname}=0" + else + __b3bp_tmp_opt="${__b3bp_tmp_opt}:" # add : if opt has arg + __b3bp_tmp_init="" # it has an arg. init with "" + eval "${__b3bp_tmp_varname}=1" + fi + __b3bp_tmp_opts="${__b3bp_tmp_opts:-}${__b3bp_tmp_opt}" + fi + + [ -z "${__b3bp_tmp_opt:-}" ] && continue + + if echo "${__b3bp_tmp_line}" |egrep '\. Default=' >/dev/null 2>&1; then + # ignore default value if option does not have an argument + __b3bp_tmp_varname="__b3bp_tmp_has_arg_${__b3bp_tmp_opt:0:1}" + + if [ "${!__b3bp_tmp_varname}" = "1" ]; then + __b3bp_tmp_init="$(echo "${__b3bp_tmp_line}" |sed 's#^.*Default=\(\)#\1#g')" + fi fi - __b3bp_tmp_opts="${__b3bp_tmp_opts:-}${__b3bp_tmp_opt}" __b3bp_tmp_varname="arg_${__b3bp_tmp_opt:0:1}" - if ! echo "${__b3bp_tmp_line}" |egrep '\. Default=' >/dev/null 2>&1; then - eval "${__b3bp_tmp_varname}=\"${__b3bp_tmp_init}\"" - else - __b3bp_tmp_match="$(echo "${__b3bp_tmp_line}" |sed 's#^.*Default=\(\)#\1#g')" - eval "${__b3bp_tmp_varname}=\"${__b3bp_tmp_match}\"" - fi + eval "${__b3bp_tmp_varname}=\"${__b3bp_tmp_init}\"" done <<< "${__usage:-}" # run getopts only if options were specified in __usage diff --git a/test/fixture/main-usage-defaults.exitcode b/test/fixture/main-usage-defaults.exitcode new file mode 100644 index 0000000..573541a --- /dev/null +++ b/test/fixture/main-usage-defaults.exitcode @@ -0,0 +1 @@ +0 diff --git a/test/fixture/main-usage-defaults.stdio b/test/fixture/main-usage-defaults.stdio new file mode 100644 index 0000000..380792a --- /dev/null +++ b/test/fixture/main-usage-defaults.stdio @@ -0,0 +1,7 @@ +ACCPTST:STDIO_REPLACE_DATETIMES +{datetime} UTC [ info] arg_1: 0 +{datetime} UTC [ info] arg_2: 0 +{datetime} UTC [ info] arg_3: THREE +{datetime} UTC [ info] arg_4: FOUR +{datetime} UTC [ info] arg_5: OOOPS +{datetime} UTC [ info] arg_6: diff --git a/test/scenario/main-usage-defaults/run.sh b/test/scenario/main-usage-defaults/run.sh new file mode 100755 index 0000000..0b37c8c --- /dev/null +++ b/test/scenario/main-usage-defaults/run.sh @@ -0,0 +1,33 @@ +#!/usr/bin/env bash +set -o pipefail +set -o errexit +set -o nounset +# set -o xtrace + +# Set magic variables for current FILE & DIR +__dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" +__file="${__dir}/$(basename "${BASH_SOURCE[0]}")" +__base="$(basename ${__file} .sh)" +__root="$(cd "$(dirname $(dirname $(dirname "${__dir}")))" && pwd)" + +# Set __usage and source main.sh +read -r -d '' __usage <<-'EOF' || true # exits non-zero when EOF encountered + -1 --one Do one thing. Default="ONE" + More description. + -2 --two Do two things. + More description. Default="TWO" + -3 --three [arg] Do three things. Default="THREE" + More description. + -4 --four [arg] Do four things. + More description. Default="FOUR" + -5 --five [arg] Do five things. Default="FIVE" + More description. Default="OOOPS" + -6 --six [arg] Do six things. + More description. +EOF + +echo "ACCPTST:STDIO_REPLACE_DATETIMES" + +source "${__root}/main.sh" + +for argument in ${!arg_*}; do info "${argument}: ${!argument}"; done