mirror of
https://github.com/kvz/bash3boilerplate.git
synced 2024-12-24 08:36:37 +00:00
get rid of awk, sed & egrep usage
This commit is contained in:
parent
b38310e915
commit
b8bdaf371a
25
main.sh
25
main.sh
@ -144,13 +144,20 @@ EOF
|
|||||||
|
|
||||||
# Translate usage string -> getopts arguments, and set $arg_<flag> defaults
|
# Translate usage string -> getopts arguments, and set $arg_<flag> defaults
|
||||||
while read -r __b3bp_tmp_line; do
|
while read -r __b3bp_tmp_line; do
|
||||||
if echo "${__b3bp_tmp_line}" |egrep '^-' >/dev/null 2>&1; then
|
if [[ "${__b3bp_tmp_line}" =~ ^- ]]; then
|
||||||
# fetch single character version of option string
|
# fetch single character version of option string
|
||||||
__b3bp_tmp_opt="$(echo "${__b3bp_tmp_line}" |awk '{print $1}' |sed -e 's#^-##')"
|
__b3bp_tmp_opt="${__b3bp_tmp_line%% *}"
|
||||||
|
__b3bp_tmp_opt="${__b3bp_tmp_opt:1}"
|
||||||
|
|
||||||
# fetch long version if present
|
# fetch long version if present
|
||||||
__b3bp_tmp_long_opt="$(echo "${__b3bp_tmp_line}" |awk '/\-\-/ {print $2}' |sed -e 's#^--##')"
|
__b3bp_tmp_long_opt=""
|
||||||
__b3bp_tmp_long_opt_mangled="$(sed 's#-#_#g' <<< "$__b3bp_tmp_long_opt")"
|
|
||||||
|
if [[ "${__b3bp_tmp_line}" == *"--"* ]]; then
|
||||||
|
__b3bp_tmp_long_opt="${__b3bp_tmp_line#*--}"
|
||||||
|
__b3bp_tmp_long_opt="${__b3bp_tmp_long_opt%% *}"
|
||||||
|
fi
|
||||||
|
|
||||||
|
__b3bp_tmp_long_opt_mangled="${__b3bp_tmp_long_opt//-/_}"
|
||||||
|
|
||||||
# map long name back to short name
|
# map long name back to short name
|
||||||
__b3bp_tmp_varname="__b3bp_tmp_short_opt_${__b3bp_tmp_long_opt_mangled}"
|
__b3bp_tmp_varname="__b3bp_tmp_short_opt_${__b3bp_tmp_long_opt_mangled}"
|
||||||
@ -158,7 +165,7 @@ while read -r __b3bp_tmp_line; do
|
|||||||
|
|
||||||
# check if option takes an argument
|
# check if option takes an argument
|
||||||
__b3bp_tmp_varname="__b3bp_tmp_has_arg_${__b3bp_tmp_opt}"
|
__b3bp_tmp_varname="__b3bp_tmp_has_arg_${__b3bp_tmp_opt}"
|
||||||
if ! echo "${__b3bp_tmp_line}" |egrep '\[.*\]' >/dev/null 2>&1; then
|
if [[ ! "${__b3bp_tmp_line}" =~ \[.*\] ]]; then
|
||||||
__b3bp_tmp_init="0" # it's a flag. init with 0
|
__b3bp_tmp_init="0" # it's a flag. init with 0
|
||||||
eval "${__b3bp_tmp_varname}=0"
|
eval "${__b3bp_tmp_varname}=0"
|
||||||
else
|
else
|
||||||
@ -171,12 +178,12 @@ while read -r __b3bp_tmp_line; do
|
|||||||
|
|
||||||
[ -z "${__b3bp_tmp_opt:-}" ] && continue
|
[ -z "${__b3bp_tmp_opt:-}" ] && continue
|
||||||
|
|
||||||
if echo "${__b3bp_tmp_line}" |egrep '\. Default=' >/dev/null 2>&1; then
|
if [[ "${__b3bp_tmp_line}" =~ \.\ Default ]]; then
|
||||||
# ignore default value if option does not have an argument
|
# ignore default value if option does not have an argument
|
||||||
__b3bp_tmp_varname="__b3bp_tmp_has_arg_${__b3bp_tmp_opt:0:1}"
|
__b3bp_tmp_varname="__b3bp_tmp_has_arg_${__b3bp_tmp_opt:0:1}"
|
||||||
|
|
||||||
if [ "${!__b3bp_tmp_varname}" = "1" ]; then
|
if [ "${!__b3bp_tmp_varname}" = "1" ]; then
|
||||||
__b3bp_tmp_init="$(echo "${__b3bp_tmp_line}" |sed 's#^.*Default=\(\)#\1#g')"
|
__b3bp_tmp_init="${__b3bp_tmp_line##*Default=}"
|
||||||
fi
|
fi
|
||||||
fi
|
fi
|
||||||
|
|
||||||
@ -204,14 +211,14 @@ if [ -n "${__b3bp_tmp_opts:-}" ]; then
|
|||||||
if [[ "${OPTARG}" =~ .*=.* ]]; then
|
if [[ "${OPTARG}" =~ .*=.* ]]; then
|
||||||
# --key=value format
|
# --key=value format
|
||||||
__b3bp_tmp_long_opt=${OPTARG/=*/}
|
__b3bp_tmp_long_opt=${OPTARG/=*/}
|
||||||
__b3bp_tmp_long_opt_mangled="$(sed 's#-#_#g' <<< "$__b3bp_tmp_long_opt")"
|
__b3bp_tmp_long_opt_mangled="${__b3bp_tmp_long_opt//-/_}"
|
||||||
# Set opt to the short option corresponding to the long option
|
# Set opt to the short option corresponding to the long option
|
||||||
eval "__b3bp_tmp_opt=\"\${__b3bp_tmp_short_opt_${__b3bp_tmp_long_opt_mangled}}\""
|
eval "__b3bp_tmp_opt=\"\${__b3bp_tmp_short_opt_${__b3bp_tmp_long_opt_mangled}}\""
|
||||||
OPTARG=${OPTARG#*=}
|
OPTARG=${OPTARG#*=}
|
||||||
else
|
else
|
||||||
# --key value format
|
# --key value format
|
||||||
# Map long name to short version of option
|
# Map long name to short version of option
|
||||||
__b3bp_tmp_long_opt_mangled="$(sed 's#-#_#g' <<< "$OPTARG")"
|
__b3bp_tmp_long_opt_mangled="${OPTARG//-/_}"
|
||||||
eval "__b3bp_tmp_opt=\"\${__b3bp_tmp_short_opt_${__b3bp_tmp_long_opt_mangled}}\""
|
eval "__b3bp_tmp_opt=\"\${__b3bp_tmp_short_opt_${__b3bp_tmp_long_opt_mangled}}\""
|
||||||
# Only assign OPTARG if option takes an argument
|
# Only assign OPTARG if option takes an argument
|
||||||
eval "OPTARG=\"\${@:OPTIND:\${__b3bp_tmp_has_arg_${__b3bp_tmp_opt}}}\""
|
eval "OPTARG=\"\${@:OPTIND:\${__b3bp_tmp_has_arg_${__b3bp_tmp_opt}}}\""
|
||||||
|
Loading…
Reference in New Issue
Block a user