modifs.mineures sans commentaires
This commit is contained in:
parent
e9bbc93a96
commit
bfb184be58
|
@ -97,87 +97,95 @@ action
|
||||||
optdesc
|
optdesc
|
||||||
: description de l'option. cette valeur est facultative"
|
: description de l'option. cette valeur est facultative"
|
||||||
function parse_args() {
|
function parse_args() {
|
||||||
local NUCORE_NO_DISABLE_SET_X=1 #XXX
|
|
||||||
[ -z "$NUCORE_NO_DISABLE_SET_X" ] && [[ $- == *x* ]] && { set +x; local NUCORE_ARGS_SET_X=1; }
|
[ -z "$NUCORE_NO_DISABLE_SET_X" ] && [[ $- == *x* ]] && { set +x; local NUCORE_ARGS_SET_X=1; }
|
||||||
local r=0
|
local __r=
|
||||||
|
local __DIE='[ -n "$NUCORE_ARGS_ONERROR_RETURN" ] && return 1 || die'
|
||||||
|
|
||||||
# distinguer les descriptions des définition d'arguments
|
if ! is_array args; then
|
||||||
local NUCORE_ARGS_USAGE NUCORE_ARGS_DESC
|
eerror "Invalid args definition: args must be defined"
|
||||||
local -a NUCORE_ARGS_DEFS NUCORE_ARGS_ARGS
|
__r=1
|
||||||
NUCORE_ARGS_ARGS=("$@")
|
fi
|
||||||
set -- "${args[@]}"
|
# distinguer les descriptions des définition d'arguments
|
||||||
[ "${1#-}" == "$1" ] && { NUCORE_ARGS_DESC="$1"; shift; }
|
local __USAGE __DESC
|
||||||
[ "${1#-}" == "$1" ] && { NUCORE_ARGS_USAGE="$1"; shift; }
|
local -a __DEFS __ARGS
|
||||||
if [ "$1" != "+" -a "${1#-}" == "$1" ]; then
|
__ARGS=("$@")
|
||||||
eerror "Invalid args definition: third arg must be an option"
|
set -- "${args[@]}"
|
||||||
r=1
|
[ "${1#-}" == "$1" ] && { __DESC="$1"; shift; }
|
||||||
else
|
[ "${1#-}" == "$1" ] && { __USAGE="$1"; shift; }
|
||||||
NUCORE_ARGS_DEFS=("$@")
|
if [ -n "$__r" ]; then
|
||||||
__parse_args || r=1
|
:
|
||||||
|
elif [ "$1" != "+" -a "${1#-}" == "$1" ]; then
|
||||||
|
eerror "Invalid args definition: third arg must be an option"
|
||||||
|
__r=1
|
||||||
|
else
|
||||||
|
__DEFS=("$@")
|
||||||
|
__parse_args || __r=1
|
||||||
|
fi
|
||||||
|
[ -n "$NUCORE_ARGS_SET_X" ] && set -x
|
||||||
|
if [ -n "$__r" ]; then
|
||||||
|
eval "$__DIE"
|
||||||
fi
|
fi
|
||||||
[ -n "$NUCORE_ARGS_SET_X" ] && set -x; return $r
|
|
||||||
}
|
}
|
||||||
function __parse_args() {
|
function __parse_args() {
|
||||||
local __die='[ -n "$NUCORE_ARGS_ONERROR_RETURN" ] && return 1 || die'
|
|
||||||
## tout d'abord, construire la liste des options
|
## tout d'abord, construire la liste des options
|
||||||
local popt sopts lopts autohelp=1
|
local __popt __sopts __lopts __autohelp=1
|
||||||
local -a defs
|
local -a __defs
|
||||||
set -- "${NUCORE_ARGS_DEFS[@]}"
|
set -- "${__DEFS[@]}"
|
||||||
while [ $# -gt 0 ]; do
|
while [ $# -gt 0 ]; do
|
||||||
case "$1" in
|
case "$1" in
|
||||||
+) popt="$1"; shift; continue;;
|
+) __popt="$1"; shift; continue;;
|
||||||
-) popt="$1"; shift; continue;;
|
-) __popt="$1"; shift; continue;;
|
||||||
-*) IFS=, read -a defs <<<"$1"; shift;;
|
-*) IFS=, read -a __defs <<<"$1"; shift;;
|
||||||
*) eerror "Invalid arg definition: expected option, got '$1'"; eval "$__die";;
|
*) eerror "Invalid arg definition: expected option, got '$1'"; eval "$__DIE";;
|
||||||
esac
|
esac
|
||||||
# est-ce que l'option prend un argument?
|
# est-ce que l'option prend un argument?
|
||||||
local def witharg=
|
local __def __witharg=
|
||||||
for def in "${defs[@]}"; do
|
for __def in "${__defs[@]}"; do
|
||||||
if [ "${def%::}" != "$def" ]; then
|
if [ "${__def%::*}" != "$__def" ]; then
|
||||||
[ "$witharg" != : ] && witharg=::
|
[ "$__witharg" != : ] && __witharg=::
|
||||||
elif [ "${def%:}" != "$def" ]; then
|
elif [ "${__def%:*}" != "$__def" ]; then
|
||||||
witharg=:
|
__witharg=:
|
||||||
fi
|
fi
|
||||||
done
|
done
|
||||||
# définitions sopts et lopts
|
# définitions __sopts et __lopts
|
||||||
for def in "${defs[@]}"; do
|
for __def in "${__defs[@]}"; do
|
||||||
def="${def%::}"; def="${def%:}"
|
__def="${__def%%:*}"
|
||||||
if [[ "$def" == --* ]]; then
|
if [[ "$__def" == --* ]]; then
|
||||||
# --longopt
|
# --longopt
|
||||||
def="${def#--}"
|
__def="${__def#--}"
|
||||||
lopts="$lopts${lopts:+,}$def$witharg"
|
__lopts="$__lopts${__lopts:+,}$__def$__witharg"
|
||||||
elif [[ "$def" == -* ]] && [ ${#def} -eq 2 ]; then
|
elif [[ "$__def" == -* ]] && [ ${#__def} -eq 2 ]; then
|
||||||
# -o
|
# -o
|
||||||
def="${def#-}"
|
__def="${__def#-}"
|
||||||
sopts="$sopts$def$witharg"
|
__sopts="$__sopts$__def$__witharg"
|
||||||
else
|
else
|
||||||
# -longopt ou longopt
|
# -longopt ou longopt
|
||||||
def="${def#-}"
|
__def="${__def#-}"
|
||||||
lopts="$lopts${lopts:+,}$def$witharg"
|
__lopts="$__lopts${__lopts:+,}$__def$__witharg"
|
||||||
fi
|
fi
|
||||||
[ "$def" == help ] && autohelp=
|
[ "$__def" == help ] && __autohelp=
|
||||||
done
|
done
|
||||||
# sauter l'action
|
# sauter l'action
|
||||||
shift
|
shift
|
||||||
# sauter la description le cas échéant
|
# sauter la description le cas échéant
|
||||||
[ "${1#-}" == "$1" ] && shift
|
[ "${1#-}" == "$1" ] && shift
|
||||||
done
|
done
|
||||||
[ -n "$autohelp" ] && lopts="$lopts${lopts:+,}help"
|
[ -n "$__autohelp" ] && __lopts="$__lopts${__lopts:+,}help"
|
||||||
sopts="$popt$sopts"
|
__sopts="$__popt$__sopts"
|
||||||
local -a getopt_args
|
local -a __getopt_args
|
||||||
getopt_args=(-n "$MYNAME" ${sopts:+-o "$sopts"} ${lopts:+-l "$lopts"} -- "${NUCORE_ARGS_ARGS[@]}")
|
__getopt_args=(-n "$MYNAME" ${__sopts:+-o "$__sopts"} ${__lopts:+-l "$__lopts"} -- "${__ARGS[@]}")
|
||||||
|
|
||||||
## puis analyser et normaliser les arguments
|
## puis analyser et normaliser les arguments
|
||||||
if args="$(getopt -q "${getopt_args[@]}")"; then
|
if args="$(getopt -q "${__getopt_args[@]}")"; then
|
||||||
eval "set -- $args"
|
eval "set -- $args"
|
||||||
else
|
else
|
||||||
# relancer pour avoir le message d'erreur
|
# relancer pour avoir le message d'erreur
|
||||||
LANG=C getopt "${getopt_args[@]}" 2>&1 1>/dev/null
|
LANG=C getopt "${__getopt_args[@]}" 2>&1 1>/dev/null
|
||||||
eval "$__die"
|
eval "$__DIE"
|
||||||
fi
|
fi
|
||||||
|
|
||||||
## puis traiter les options
|
## puis traiter les options
|
||||||
local defname defvalue autoadd option_ name_ value_
|
local __defname __defvalue __add __action option_ name_ value_
|
||||||
function inc@() {
|
function inc@() {
|
||||||
if [ -n "$2" ]; then
|
if [ -n "$2" ]; then
|
||||||
# forcer une valeur
|
# forcer une valeur
|
||||||
|
@ -189,16 +197,16 @@ function __parse_args() {
|
||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
function res@() {
|
function res@() {
|
||||||
local value_="${value_:-$2}"
|
local __value="${value_:-$2}"
|
||||||
eval "$1=\"\$value_\""
|
eval "$1=\"\$__value\""
|
||||||
}
|
}
|
||||||
function add@() {
|
function add@() {
|
||||||
local value_="${value_:-$2}"
|
local __value="${value_:-$2}"
|
||||||
eval "$1+=(\"\$value_\")"
|
eval "$1+=(\"\$__value\")"
|
||||||
}
|
}
|
||||||
function set@() {
|
function set@() {
|
||||||
if [ -n "$witharg" ]; then
|
if [ -n "$__witharg" ]; then
|
||||||
if [ -n "$autoadd" ]; then
|
if [ -n "$__add" ]; then
|
||||||
if is_array "$1"; then
|
if is_array "$1"; then
|
||||||
add@ "$@"
|
add@ "$@"
|
||||||
elif ! is_defined "$1"; then
|
elif ! is_defined "$1"; then
|
||||||
|
@ -218,48 +226,48 @@ function __parse_args() {
|
||||||
}
|
}
|
||||||
function showhelp@() {
|
function showhelp@() {
|
||||||
local help="$MYNAME"
|
local help="$MYNAME"
|
||||||
if [ -n "$NUCORE_ARGS_DESC" ]; then
|
if [ -n "$__DESC" ]; then
|
||||||
help="$help: $NUCORE_ARGS_DESC"
|
help="$help: $__DESC"
|
||||||
fi
|
fi
|
||||||
help="$help
|
help="$help
|
||||||
|
|
||||||
USAGE
|
USAGE
|
||||||
$MYNAME"
|
$MYNAME"
|
||||||
if [ -n "$NUCORE_ARGS_USAGE" ]; then
|
if [ -n "$__USAGE" ]; then
|
||||||
help="$help $NUCORE_ARGS_USAGE"
|
help="$help $__USAGE"
|
||||||
else
|
else
|
||||||
help="$help [options]"
|
help="$help [options]"
|
||||||
fi
|
fi
|
||||||
help="$help
|
help="$help
|
||||||
|
|
||||||
OPTIONS"
|
OPTIONS"
|
||||||
set -- "${NUCORE_ARGS_DEFS[@]}"
|
set -- "${__DEFS[@]}"
|
||||||
while [ $# -gt 0 ]; do
|
while [ $# -gt 0 ]; do
|
||||||
case "$1" in
|
case "$1" in
|
||||||
+) shift; continue;;
|
+) shift; continue;;
|
||||||
-) shift; continue;;
|
-) shift; continue;;
|
||||||
-*) IFS=, read -a defs <<<"$1"; shift;;
|
-*) IFS=, read -a __defs <<<"$1"; shift;;
|
||||||
esac
|
esac
|
||||||
# est-ce que l'option prend un argument?
|
# est-ce que l'option prend un argument?
|
||||||
witharg=
|
__witharg=
|
||||||
for def in "${defs[@]}"; do
|
for __def in "${__defs[@]}"; do
|
||||||
if [ "${def%::}" != "$def" ]; then
|
if [ "${__def%::*}" != "$__def" ]; then
|
||||||
[ "$witharg" != : ] && witharg=::
|
[ "$__witharg" != : ] && __witharg=::
|
||||||
elif [ "${def%:}" != "$def" ]; then
|
elif [ "${__def%:*}" != "$__def" ]; then
|
||||||
witharg=:
|
__witharg=:
|
||||||
fi
|
fi
|
||||||
done
|
done
|
||||||
# description de l'option
|
# description de l'option
|
||||||
local first=1
|
local first=1
|
||||||
for def in "${defs[@]}"; do
|
for __def in "${__defs[@]}"; do
|
||||||
def="${def%::}"; def="${def%:}"
|
__def="${__def%%:*}"
|
||||||
if [[ "$def" == --* ]]; then
|
if [[ "$__def" == --* ]]; then
|
||||||
: # --longopt
|
: # --longopt
|
||||||
elif [[ "$def" == -* ]] && [ ${#def} -eq 2 ]; then
|
elif [[ "$__def" == -* ]] && [ ${#__def} -eq 2 ]; then
|
||||||
: # -o
|
: # -o
|
||||||
else
|
else
|
||||||
# -longopt ou longopt
|
# -longopt ou longopt
|
||||||
def="--${def#-}"
|
__def="--${__def#-}"
|
||||||
fi
|
fi
|
||||||
if [ -n "$first" ]; then
|
if [ -n "$first" ]; then
|
||||||
first=
|
first=
|
||||||
|
@ -268,9 +276,9 @@ OPTIONS"
|
||||||
else
|
else
|
||||||
help="$help, "
|
help="$help, "
|
||||||
fi
|
fi
|
||||||
help="$help$def"
|
help="$help$__def"
|
||||||
done
|
done
|
||||||
[ -n "$witharg" ] && help="$help VALUE"
|
[ -n "$__witharg" ] && help="$help VALUE"
|
||||||
# sauter l'action
|
# sauter l'action
|
||||||
shift
|
shift
|
||||||
# prendre la description le cas échéant
|
# prendre la description le cas échéant
|
||||||
|
@ -283,7 +291,7 @@ OPTIONS"
|
||||||
uecho "$help"
|
uecho "$help"
|
||||||
exit 0
|
exit 0
|
||||||
}
|
}
|
||||||
if [ "$popt" != + ]; then
|
if [ "$__popt" != + ]; then
|
||||||
while [ $# -gt 0 ]; do
|
while [ $# -gt 0 ]; do
|
||||||
if [ "$1" == -- ]; then
|
if [ "$1" == -- ]; then
|
||||||
shift
|
shift
|
||||||
|
@ -292,14 +300,14 @@ OPTIONS"
|
||||||
[[ "$1" == -* ]] || break
|
[[ "$1" == -* ]] || break
|
||||||
option_="$1"; shift
|
option_="$1"; shift
|
||||||
__parse_opt "$option_"
|
__parse_opt "$option_"
|
||||||
if [ -n "$witharg" ]; then
|
if [ -n "$__witharg" ]; then
|
||||||
# l'option prend un argument
|
# l'option prend un argument
|
||||||
value_="$1"; shift
|
value_="$1"; shift
|
||||||
else
|
else
|
||||||
# l'option ne prend pas d'argument
|
# l'option ne prend pas d'argument
|
||||||
value_=
|
value_=
|
||||||
fi
|
fi
|
||||||
eval "$action"
|
eval "$__action"
|
||||||
done
|
done
|
||||||
fi
|
fi
|
||||||
unset -f inc@ res@ add@ set@ showhelp@
|
unset -f inc@ res@ add@ set@ showhelp@
|
||||||
|
@ -308,75 +316,75 @@ OPTIONS"
|
||||||
function __parse_opt() {
|
function __parse_opt() {
|
||||||
# $1 est l'option spécifiée
|
# $1 est l'option spécifiée
|
||||||
local option_="$1"
|
local option_="$1"
|
||||||
set -- "${NUCORE_ARGS_DEFS[@]}"
|
set -- "${__DEFS[@]}"
|
||||||
while [ $# -gt 0 ]; do
|
while [ $# -gt 0 ]; do
|
||||||
case "$1" in
|
case "$1" in
|
||||||
+) shift; continue;;
|
+) shift; continue;;
|
||||||
-) shift; continue;;
|
-) shift; continue;;
|
||||||
-*) IFS=, read -a defs <<<"$1"; shift;;
|
-*) IFS=, read -a __defs <<<"$1"; shift;;
|
||||||
esac
|
esac
|
||||||
# est-ce que l'option prend un argument?
|
# est-ce que l'option prend un argument?
|
||||||
witharg=
|
__witharg=
|
||||||
for def in "${defs[@]}"; do
|
for __def in "${__defs[@]}"; do
|
||||||
if [ "${def%::}" != "$def" ]; then
|
if [ "${__def%::*}" != "$__def" ]; then
|
||||||
[ "$witharg" != : ] && witharg=::
|
[ "$__witharg" != : ] && __witharg=::
|
||||||
elif [ "${def%:}" != "$def" ]; then
|
elif [ "${__def%:*}" != "$__def" ]; then
|
||||||
witharg=:
|
__witharg=:
|
||||||
fi
|
fi
|
||||||
done
|
done
|
||||||
# nom le plus long
|
# nom le plus long
|
||||||
defname=
|
__defname=
|
||||||
local found=
|
local __found=
|
||||||
for def in "${defs[@]}"; do
|
for __def in "${__defs[@]}"; do
|
||||||
def="${def%::}"; def="${def%:}"
|
__def="${__def%%:*}"
|
||||||
[ "$def" == "$option_" ] && found=1
|
[ "$__def" == "$option_" ] && __found=1
|
||||||
if [[ "$def" == --* ]]; then
|
if [[ "$__def" == --* ]]; then
|
||||||
# --longopt
|
# --longopt
|
||||||
def="${def#--}"
|
__def="${__def#--}"
|
||||||
[ ${#def} -gt ${#defname} ] && defname="$def"
|
[ ${#__def} -gt ${#__defname} ] && __defname="$__def"
|
||||||
elif [[ "$def" == -* ]] && [ ${#def} -eq 2 ]; then
|
elif [[ "$__def" == -* ]] && [ ${#__def} -eq 2 ]; then
|
||||||
# -o
|
# -o
|
||||||
def="${def#-}"
|
__def="${__def#-}"
|
||||||
[ ${#def} -gt ${#defname} ] && defname="$def"
|
[ ${#__def} -gt ${#__defname} ] && __defname="$__def"
|
||||||
else
|
else
|
||||||
# -longopt ou longopt
|
# -longopt ou longopt
|
||||||
def="${def#-}"
|
__def="${__def#-}"
|
||||||
[ ${#def} -gt ${#defname} ] && defname="$def"
|
[ ${#__def} -gt ${#__defname} ] && __defname="$__def"
|
||||||
fi
|
fi
|
||||||
done
|
done
|
||||||
defname="${defname//-/_}"
|
__defname="${__defname//-/_}"
|
||||||
# analyser l'action
|
# analyser l'action
|
||||||
if [ "${1#\$}" != "$1" ]; then
|
if [ "${1#\$}" != "$1" ]; then
|
||||||
name_="$defname"
|
name_="$__defname"
|
||||||
defvalue=
|
__defvalue=
|
||||||
autoadd=
|
__add=
|
||||||
action="${1#\$}"
|
__action="${1#\$}"
|
||||||
else
|
else
|
||||||
if [ "$1" == . ]; then
|
if [ "$1" == . ]; then
|
||||||
name_="$defname"
|
name_="$__defname"
|
||||||
defvalue=
|
__defvalue=
|
||||||
autoadd=1
|
__add=1
|
||||||
elif [[ "$1" == *=* ]]; then
|
elif [[ "$1" == *=* ]]; then
|
||||||
name_="${1%%=*}"
|
name_="${1%%=*}"
|
||||||
defvalue="${1#*=}"
|
__defvalue="${1#*=}"
|
||||||
autoadd=
|
__add=
|
||||||
else
|
else
|
||||||
name_="$1"
|
name_="$1"
|
||||||
defvalue=
|
__defvalue=
|
||||||
autoadd=1
|
__add=1
|
||||||
fi
|
fi
|
||||||
action="$(qvals set@ "$name_" "$defvalue")"
|
__action="$(qvals set@ "$name_" "$__defvalue")"
|
||||||
fi
|
fi
|
||||||
shift
|
shift
|
||||||
# sauter la description le cas échéant
|
# sauter la description le cas échéant
|
||||||
[ "${1#-}" == "$1" ] && shift
|
[ "${1#-}" == "$1" ] && shift
|
||||||
|
|
||||||
[ -n "$found" ] && return 0
|
[ -n "$__found" ] && return 0
|
||||||
done
|
done
|
||||||
# ici, l'option n'a pas été trouvée, on ne devrait pas arriver ici
|
# ici, l'option n'a pas été trouvée, on ne devrait pas arriver ici
|
||||||
if [ "$option_" == --help -a -n "$autohelp" ]; then
|
if [ "$option_" == --help -a -n "$__autohelp" ]; then
|
||||||
action="showhelp@"
|
__action="showhelp@"
|
||||||
return 0
|
return 0
|
||||||
fi
|
fi
|
||||||
eerror "Unexpected option '$option_'"; eval "$__die"
|
eerror "Unexpected option '$option_'"; eval "$__DIE"
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue