Allow for multiline opt description in __usage. Fixes #7

This commit is contained in:
Manuel Streuhofer 2016-11-11 01:13:28 +01:00
parent 0181129aff
commit 539443b3ca
4 changed files with 73 additions and 24 deletions

56
main.sh
View File

@ -131,36 +131,44 @@ EOF
# Translate usage string -> getopts arguments, and set $arg_<flag> 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

View File

@ -0,0 +1 @@
0

View File

@ -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:

View File

@ -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