arg-parser fixes
This commit is contained in:
@@ -50,15 +50,14 @@ grep_match() {
|
||||
#usage To echo 1 if flag exists: arg f
|
||||
arg() {
|
||||
local need=${1:0:1} # argument to find (only first letter)
|
||||
[ $need ] || {
|
||||
[ "$need" ] || {
|
||||
echo "Argument is not specified!" >&2
|
||||
exit 1
|
||||
}
|
||||
local isflag=$2 || 0 # should we find the value or just the presence of the $need?
|
||||
local retvar=$3 || 0 # var to return value into (if 0 then value will be echo'ed in stdout)
|
||||
local args=(${__MAIN_ARGS[0]}) # args we need are stored in 1st element of __MAIN_ARGS
|
||||
for ((idx=0; idx<${#args[@]}; ++idx)) do # going through args
|
||||
local arg=${args[$idx]} # current argument
|
||||
for ((idx=0; idx<${#__MAIN_ARGS__[@]}; ++idx)) do # going through args
|
||||
local arg=${__MAIN_ARGS__[$idx]} # current argument
|
||||
# skip $arg if it starts with '--', letter or digit
|
||||
grep_match "$arg" "^(\w{1}|-{2})" && continue
|
||||
# clear $arg from special and duplicate characters
|
||||
@@ -67,15 +66,15 @@ arg() {
|
||||
# now we can check if $need is one of $chars
|
||||
if grep_match "-$need" "^-[$chars]$"; then # if it is
|
||||
if [[ $isflag = 1 ]]; then # and we expect it as a flag
|
||||
# then return '1' back into $3 (if exists) or echo in stdout
|
||||
[ $retvar ] && eval "$retvar='1'" || echo "1"
|
||||
# then return '1' back into $retvar (if exists) or echo in stdout
|
||||
[ "$retvar" ] && eval "$retvar='1'" || echo "1"
|
||||
else # but if $arg is not a flag
|
||||
# then get next argument as value of current one
|
||||
local value="${args[$idx+1]}"
|
||||
local value="${__MAIN_ARGS__[$idx+1]}"
|
||||
# check if it is valid value
|
||||
if grep_match "$value" "^\w+$"; then
|
||||
# and return it back back into $3 (if exists) or echo in stdout
|
||||
[ $retvar ] && eval "$retvar='$value'" || echo "$value"
|
||||
if grep_match "$value" "^[[:graph:]]+$"; then
|
||||
# and return it back back into $retvar (if exists) or echo in stdout
|
||||
[ "$retvar" ] && eval "$retvar='$value'" || echo "$value"
|
||||
break
|
||||
else # otherwise throw error message into stderr (just in case)
|
||||
echo "Argument '$arg' must have a correct value!" >&2
|
||||
@@ -98,28 +97,27 @@ arg() {
|
||||
#usage To echo 1 if flag exists: arg f
|
||||
argl() {
|
||||
local need=$1 # argument to find
|
||||
[ $need ] || {
|
||||
[ "$need" ] || {
|
||||
echo "Argument is not specified!" >&2
|
||||
exit 1
|
||||
}
|
||||
local isflag=$2 || 0 # should we find the value or just the presence of the $need?
|
||||
local retvar=$3 || 0 # var to return value into (if 0 then value will be echo'ed in stdout)
|
||||
local args=(${__MAIN_ARGS[0]}) # args we need are stored in 1st element of __MAIN_ARGS
|
||||
for ((idx=0; idx<${#args[@]}; ++idx)) do
|
||||
local arg=${args[$idx]} # current argument
|
||||
for ((idx=0; idx<${#__MAIN_ARGS__[@]}; ++idx)) do # going through args
|
||||
local arg=${__MAIN_ARGS__[$idx]} # current argument
|
||||
# if we expect $arg as a flag
|
||||
if [[ $isflag = 1 ]]; then
|
||||
# and if $arg has correct format (like '--flag')
|
||||
if grep_match "$arg" "^--$need"; then
|
||||
# then return '1' back into $3 (if exists) or echo in stdout
|
||||
[ ! $retvar = 0 ] && eval "$retvar=1" || echo "1"
|
||||
# then return '1' back into $retvar (if exists) or echo in stdout
|
||||
[ "$retvar" = 0 ] && echo "1" || eval "$retvar=1"
|
||||
break
|
||||
fi
|
||||
else # but if $arg is not a flag
|
||||
# check if $arg has correct format (like '--foo=bar')
|
||||
if grep_match "$arg" "^--$need=.+$"; then # if it is
|
||||
# then return part from '=' to arg's end as value back into $3 (if exists) or echo in stdout
|
||||
[ ! $retvar = 0 ] && eval "$retvar=${arg#*=}" || echo "${arg#*=}"
|
||||
# then return part from '=' to arg's end as value back into $retvar (if exists) or echo in stdout
|
||||
[ "$retvar" = 0 ] && echo "${arg#*=}" || eval "$retvar=${arg#*=}"
|
||||
break
|
||||
fi
|
||||
fi
|
||||
@@ -170,7 +168,7 @@ argn() {
|
||||
|
||||
# first we must save the original arguments passed
|
||||
# to the script when it was called:
|
||||
__MAIN_ARGS=$@
|
||||
__MAIN_ARGS__=($@)
|
||||
|
||||
echo -e "\n1. Short args (vars):"
|
||||
arg a 1 a # -a
|
||||
|
||||
Reference in New Issue
Block a user