Close #9 : Ensured all local vars are declared local. Also removed builtin cmdarg_cfg['cfgfile'] loading as it was undocumented and even I forgot it was there.
This commit is contained in:
114
cmdarg.sh
114
cmdarg.sh
@@ -30,8 +30,8 @@ function cmdarg
|
|||||||
# the argument value is invalid. Can be straight bash, but it really
|
# the argument value is invalid. Can be straight bash, but it really
|
||||||
# should be the name of a function. This may be enforced in future versions
|
# should be the name of a function. This may be enforced in future versions
|
||||||
# of the library.
|
# of the library.
|
||||||
shortopt=${1:0:1}
|
local shortopt=${1:0:1}
|
||||||
key="$2"
|
local key="$2"
|
||||||
if [[ "$shortopt" == "h" ]]; then
|
if [[ "$shortopt" == "h" ]]; then
|
||||||
echo "-h is reserved for cmdarg usage" >&2
|
echo "-h is reserved for cmdarg usage" >&2
|
||||||
${CMDARG_ERROR_BEHAVIOR} 1
|
${CMDARG_ERROR_BEHAVIOR} 1
|
||||||
@@ -46,7 +46,7 @@ function cmdarg
|
|||||||
declare -A argtypemap
|
declare -A argtypemap
|
||||||
argtypemap[':']=$CMDARG_FLAG_REQARG
|
argtypemap[':']=$CMDARG_FLAG_REQARG
|
||||||
argtypemap['?']=$CMDARG_FLAG_OPTARG
|
argtypemap['?']=$CMDARG_FLAG_OPTARG
|
||||||
argtype=${1:1:1}
|
local argtype=${1:1:1}
|
||||||
if [[ "$argtype" =~ ^[\[{]$ ]]; then
|
if [[ "$argtype" =~ ^[\[{]$ ]]; then
|
||||||
echo "Flags required [:?] when specifying Hash or Array arguments (${argtype})" >&2
|
echo "Flags required [:?] when specifying Hash or Array arguments (${argtype})" >&2
|
||||||
${CMDARG_ERROR_BEHAVIOR} 1
|
${CMDARG_ERROR_BEHAVIOR} 1
|
||||||
@@ -101,10 +101,10 @@ function cmdarg_info
|
|||||||
#
|
#
|
||||||
# Sets various flags about your script that are printed during cmdarg_usage
|
# Sets various flags about your script that are printed during cmdarg_usage
|
||||||
#
|
#
|
||||||
FLAGS="header|copyright|footer|author"
|
local flags="header|copyright|footer|author"
|
||||||
if [[ "$1" =~ $FLAGS ]]; then
|
if [[ "$1" =~ $flags ]]; then
|
||||||
echo "cmdarg_info <flag> <value>" >&2
|
echo "cmdarg_info <flag> <value>" >&2
|
||||||
echo "Where <flag> is one of $FLAGS" >&2
|
echo "Where <flag> is one of $flags" >&2
|
||||||
${CMDARG_ERROR_BEHAVIOR} 1
|
${CMDARG_ERROR_BEHAVIOR} 1
|
||||||
fi
|
fi
|
||||||
CMDARG_INFO["$1"]=$2
|
CMDARG_INFO["$1"]=$2
|
||||||
@@ -112,14 +112,13 @@ function cmdarg_info
|
|||||||
|
|
||||||
function cmdarg_describe
|
function cmdarg_describe
|
||||||
{
|
{
|
||||||
local longopt opt argtype default description flags validator
|
local longopt=${CMDARG[$1]}
|
||||||
longopt=${CMDARG[$1]}
|
local opt=$1
|
||||||
opt=$1
|
local argtype=${CMDARG_TYPES[$longopt]}
|
||||||
argtype=${CMDARG_TYPES[$longopt]}
|
local default=${CMDARG_DEFAULT[$opt]}
|
||||||
default=${CMDARG_DEFAULT[$opt]}
|
local description=${CMDARG_DESC[$opt]}
|
||||||
description=${CMDARG_DESC[$opt]}
|
local flags="${CMDARG_FLAGS[$opt]}"
|
||||||
flags="${CMDARG_FLAGS[$opt]}"
|
local validator="${CMDARG_VALIDATORS[$opt]}"
|
||||||
validator="${CMDARG_VALIDATORS[$opt]}"
|
|
||||||
|
|
||||||
${cmdarg_helpers['describe']} $longopt $opt $argtype "${default}" "${description}" "${flags}" "${validator}"
|
${cmdarg_helpers['describe']} $longopt $opt $argtype "${default}" "${description}" "${flags}" "${validator}"
|
||||||
}
|
}
|
||||||
@@ -127,14 +126,13 @@ function cmdarg_describe
|
|||||||
function cmdarg_describe_default
|
function cmdarg_describe_default
|
||||||
{
|
{
|
||||||
set -u
|
set -u
|
||||||
local longopt opt argtype default description flags validator
|
local longopt=$1
|
||||||
longopt=$1
|
local opt=$2
|
||||||
opt=$2
|
local argtype=$3
|
||||||
argtype=$3
|
local default="$4"
|
||||||
default="$4"
|
local description="$5"
|
||||||
description="$5"
|
local flags="$6"
|
||||||
flags="$6"
|
local validator="${7:-}"
|
||||||
validator="${7:-}"
|
|
||||||
set +u
|
set +u
|
||||||
|
|
||||||
if [ "${default}" != "" ]; then
|
if [ "${default}" != "" ]; then
|
||||||
@@ -193,13 +191,12 @@ function cmdarg_usage
|
|||||||
function cmdarg_validate
|
function cmdarg_validate
|
||||||
{
|
{
|
||||||
set -u
|
set -u
|
||||||
local longopt value
|
local longopt=$1
|
||||||
longopt=$1
|
local value=$2
|
||||||
value=$2
|
local hashkey=${3:-}
|
||||||
hashkey=${3:-}
|
|
||||||
set +u
|
set +u
|
||||||
|
|
||||||
shortopt=${CMDARG_REV[$longopt]}
|
local shortopt=${CMDARG_REV[$longopt]}
|
||||||
if [ "${CMDARG_VALIDATORS[$shortopt]}" != "" ]; then
|
if [ "${CMDARG_VALIDATORS[$shortopt]}" != "" ]; then
|
||||||
( ${CMDARG_VALIDATORS[${shortopt}]} "$value" "$hashkey")
|
( ${CMDARG_VALIDATORS[${shortopt}]} "$value" "$hashkey")
|
||||||
if [ $? -ne 0 ]; then
|
if [ $? -ne 0 ]; then
|
||||||
@@ -213,9 +210,8 @@ function cmdarg_validate
|
|||||||
function cmdarg_set_opt
|
function cmdarg_set_opt
|
||||||
{
|
{
|
||||||
set -u
|
set -u
|
||||||
local key arg
|
local key=$1
|
||||||
key=$1
|
local arg="$2"
|
||||||
arg="$2"
|
|
||||||
set +u
|
set +u
|
||||||
|
|
||||||
case ${CMDARG_TYPES[$key]} in
|
case ${CMDARG_TYPES[$key]} in
|
||||||
@@ -228,9 +224,9 @@ function cmdarg_set_opt
|
|||||||
cmdarg_validate "$key" "$arg" || ${CMDARG_ERROR_BEHAVIOR} 1
|
cmdarg_validate "$key" "$arg" || ${CMDARG_ERROR_BEHAVIOR} 1
|
||||||
;;
|
;;
|
||||||
$CMDARG_TYPE_ARRAY)
|
$CMDARG_TYPE_ARRAY)
|
||||||
arrname="${key}"
|
local arrname="${key}"
|
||||||
str='${#'"$arrname"'[@]}'
|
local str='${#'"$arrname"'[@]}'
|
||||||
prevlen=$(eval "echo $str")
|
local prevlen=$(eval "echo $str")
|
||||||
eval "${arrname}[$((prevlen + 1))]=\"$arg\""
|
eval "${arrname}[$((prevlen + 1))]=\"$arg\""
|
||||||
cmdarg_validate "$key" "$arg" || ${CMDARG_ERROR_BEHAVIOR} 1
|
cmdarg_validate "$key" "$arg" || ${CMDARG_ERROR_BEHAVIOR} 1
|
||||||
;;
|
;;
|
||||||
@@ -254,10 +250,9 @@ function cmdarg_set_opt
|
|||||||
|
|
||||||
function cmdarg_check_empty
|
function cmdarg_check_empty
|
||||||
{
|
{
|
||||||
local key longopt
|
local key=$1
|
||||||
key=$1
|
local longopt=${CMDARG[$key]}
|
||||||
longopt=${CMDARG[$key]}
|
local type=${CMDARG_TYPES[$longopt]}
|
||||||
type=${CMDARG_TYPES[$longopt]}
|
|
||||||
|
|
||||||
case $type in
|
case $type in
|
||||||
$CMDARG_TYPE_STRING)
|
$CMDARG_TYPE_STRING)
|
||||||
@@ -267,13 +262,13 @@ function cmdarg_check_empty
|
|||||||
echo ${cmdarg_cfg[$longopt]}
|
echo ${cmdarg_cfg[$longopt]}
|
||||||
;;
|
;;
|
||||||
$CMDARG_TYPE_ARRAY)
|
$CMDARG_TYPE_ARRAY)
|
||||||
arrname="${longopt}"
|
local arrname="${longopt}"
|
||||||
lval='${!'"${arrname}"'[@]}'
|
local lval='${!'"${arrname}"'[@]}'
|
||||||
eval "echo $lval"
|
eval "echo $lval"
|
||||||
;;
|
;;
|
||||||
$CMDARG_TYPE_HASH)
|
$CMDARG_TYPE_HASH)
|
||||||
arrname="${longopt}"
|
local arrname="${longopt}"
|
||||||
lval='${!'"${arrname}"'[@]}'
|
local lval='${!'"${arrname}"'[@]}'
|
||||||
eval "echo $lval"
|
eval "echo $lval"
|
||||||
;;
|
;;
|
||||||
*)
|
*)
|
||||||
@@ -288,20 +283,20 @@ function cmdarg_parse
|
|||||||
#
|
#
|
||||||
# Call it EXACTLY LIKE THAT, and it will parse your arguments for you.
|
# Call it EXACTLY LIKE THAT, and it will parse your arguments for you.
|
||||||
# This function only knows about the arguments that you previously called 'cmdarg' for.
|
# This function only knows about the arguments that you previously called 'cmdarg' for.
|
||||||
local OPTIND parsing fullopt opt optarg longopt tmpopt failed missing
|
local failed=0
|
||||||
failed=0
|
local missing=""
|
||||||
missing=""
|
|
||||||
|
|
||||||
parsing=0
|
local parsing=0
|
||||||
while [[ "$@" != "" ]]; do
|
while [[ "$@" != "" ]]; do
|
||||||
optarg=""
|
local optarg=""
|
||||||
opt=""
|
local opt=""
|
||||||
longopt=""
|
local longopt=""
|
||||||
fullopt=$1
|
local fullopt=$1
|
||||||
is_equals_arg=1
|
local is_equals_arg=1
|
||||||
|
|
||||||
shift
|
shift
|
||||||
if [[ "${fullopt}" =~ ^(--[a-zA-Z0-9_\-]+|^-[a-zA-Z0-9])= ]]; then
|
if [[ "${fullopt}" =~ ^(--[a-zA-Z0-9_\-]+|^-[a-zA-Z0-9])= ]]; then
|
||||||
tmpopt=$fullopt
|
local tmpopt=$fullopt
|
||||||
fullopt=${tmpopt%%=*}
|
fullopt=${tmpopt%%=*}
|
||||||
optarg=${tmpopt##*=}
|
optarg=${tmpopt##*=}
|
||||||
is_equals_arg=0
|
is_equals_arg=0
|
||||||
@@ -341,7 +336,7 @@ function cmdarg_parse
|
|||||||
|
|
||||||
if [ ${CMDARG["${opt}"]+abc} ]; then
|
if [ ${CMDARG["${opt}"]+abc} ]; then
|
||||||
cmdarg_set_opt "${CMDARG[$opt]}" "$optarg"
|
cmdarg_set_opt "${CMDARG[$opt]}" "$optarg"
|
||||||
rc=$?
|
local rc=$?
|
||||||
failed=$((failed + $rc))
|
failed=$((failed + $rc))
|
||||||
else
|
else
|
||||||
echo "Unknown argument or invalid value : -${opt} | --${longopt}" >&2
|
echo "Unknown argument or invalid value : -${opt} | --${longopt}" >&2
|
||||||
@@ -353,6 +348,7 @@ function cmdarg_parse
|
|||||||
# --- Don't ${CMDARG_ERROR_BEHAVIOR} early during validation, tell the user
|
# --- Don't ${CMDARG_ERROR_BEHAVIOR} early during validation, tell the user
|
||||||
# everything they did wrong first
|
# everything they did wrong first
|
||||||
|
|
||||||
|
local key
|
||||||
for key in "${CMDARG_REQUIRED[@]}"
|
for key in "${CMDARG_REQUIRED[@]}"
|
||||||
do
|
do
|
||||||
if [[ "$(cmdarg_check_empty $key)" == "" ]]; then
|
if [[ "$(cmdarg_check_empty $key)" == "" ]]; then
|
||||||
@@ -369,10 +365,6 @@ function cmdarg_parse
|
|||||||
${cmdarg_helpers['usage']} >&2
|
${cmdarg_helpers['usage']} >&2
|
||||||
${CMDARG_ERROR_BEHAVIOR} 1
|
${CMDARG_ERROR_BEHAVIOR} 1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
if [ ! -z "${cmdarg_cfg[cfgfile]}" ]; then
|
|
||||||
. ${cmdarg_cfg[cfgfile]}
|
|
||||||
fi
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function cmdarg_traceback
|
function cmdarg_traceback
|
||||||
@@ -392,6 +384,14 @@ function cmdarg_traceback
|
|||||||
|
|
||||||
function cmdarg_dump
|
function cmdarg_dump
|
||||||
{
|
{
|
||||||
|
local key
|
||||||
|
local repr
|
||||||
|
local arrname
|
||||||
|
local keys
|
||||||
|
local idx
|
||||||
|
local ref
|
||||||
|
local value
|
||||||
|
|
||||||
for key in ${!cmdarg_cfg[@]}
|
for key in ${!cmdarg_cfg[@]}
|
||||||
do
|
do
|
||||||
repr="${key}:${CMDARG_TYPES[$key]}"
|
repr="${key}:${CMDARG_TYPES[$key]}"
|
||||||
@@ -413,6 +413,8 @@ function cmdarg_dump
|
|||||||
|
|
||||||
function cmdarg_purge
|
function cmdarg_purge
|
||||||
{
|
{
|
||||||
|
local arrays
|
||||||
|
local arr
|
||||||
arrays="cmdarg_cfg CMDARG CMDARG_REV CMDARG_OPTIONAL CMDARG_REQUIRED"
|
arrays="cmdarg_cfg CMDARG CMDARG_REV CMDARG_OPTIONAL CMDARG_REQUIRED"
|
||||||
arrays="$arrays CMDARG_DESC CMDARG_DEFAULT CMDARG_VALIDATORS CMDARG_INFO"
|
arrays="$arrays CMDARG_DESC CMDARG_DEFAULT CMDARG_VALIDATORS CMDARG_INFO"
|
||||||
arrays="$arrays CMDARG_FLAGS CMDARG_TYPES"
|
arrays="$arrays CMDARG_FLAGS CMDARG_TYPES"
|
||||||
|
|||||||
Reference in New Issue
Block a user