modifs.mineures sans commentaires

This commit is contained in:
Jephté Clain 2023-10-14 07:20:09 +04:00
parent e9bbc93a96
commit bfb184be58
1 changed files with 124 additions and 116 deletions

View File

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