modifs.mineures sans commentaires

This commit is contained in:
Jephté Clain 2023-12-15 11:45:24 +04:00
parent 69f3ab76b1
commit d7323d7d45
2 changed files with 35 additions and 26 deletions

View File

@ -94,6 +94,8 @@ action
les fonctions suivantes sont définies:
* 'inc@ NAME' incrémente la variable NAME -- c'est le comportement de set@ si
l'option est sans argument
* 'dec@ NAME' décrémente la variable NAME, et la rend vide quand le compte
arrive à zéro
* 'res@ NAME VALUE' initialise la variable NAME avec la valeur de l'option (ou
VALUE si la valeur de l'option est vide) -- c'est le comportement de set@
si l'option prend un argument
@ -199,16 +201,14 @@ function __parse_args() {
fi
## puis traiter les options
local __defname __defvalue __add __action option_ name_ value_
local __defname __fixvalue __defvalue __add __action option_ name_ value_
function inc@() {
if [ -n "$2" ]; then
# forcer une valeur
eval "$1=\"\$2\""
else
# incrémenter
eval "[ -n \"\$$1\" ] || let $1=0"
eval "let $1=$1+1"
fi
}
function dec@() {
eval "[ -n \"\$$1\" ] && let $1=$1-1"
eval "[ $1 -eq 0 ] && $1="
}
function res@() {
local __value="${value_:-$2}"
@ -219,8 +219,9 @@ function __parse_args() {
eval "$1+=(\"\$__value\")"
}
function set@() {
if [ -n "$__witharg" ]; then
if [ -n "$__add" ]; then
if [ -n "$__fixvalue" ]; then
res@ "$@"
elif [ -n "$__witharg" ]; then
if is_array "$1"; then
add@ "$@"
elif ! is_defined "$1"; then
@ -231,9 +232,6 @@ function __parse_args() {
[ -z "${!1}" ] && eval "$1=()"
add@ "$@"
fi
else
res@ "$@"
fi
else
inc@ "$@"
fi
@ -397,22 +395,22 @@ function __parse_opt() {
# analyser l'action
if [ "${1#\$}" != "$1" ]; then
name_="$__defname"
__fixvalue=
__defvalue=
__add=
__action="${1#\$}"
else
if [ "$1" == . ]; then
name_="$__defname"
__fixvalue=
__defvalue=
__add=1
elif [[ "$1" == *=* ]]; then
name_="${1%%=*}"
__fixvalue=1
__defvalue="${1#*=}"
__add=
else
name_="$1"
__fixvalue=
__defvalue=
__add=1
fi
__action="$(qvals set@ "$name_" "$__defvalue")"
fi

View File

@ -8,6 +8,7 @@ function pa() {
unset count fixed mopt dmopt oopt doopt autoinc autoval a1 a2 a3 a4
count=
fixed=
efixed=1
mopt=
dmopt=
oopt=
@ -22,6 +23,7 @@ function pa() {
"tester la gestion des arguments"
-o,--eopt count "incrémenter count"
-f,--fixed fixed=42 "spécifier fixed"
-e,--efixed efixed= "spécifier efixed"
-a:,--mopt mopt= "spécifier mopt"
-A:,--dmopt dmopt=default "spécifier dmopt"
-b::,--oopt oopt= "spécifier oopt"
@ -41,6 +43,7 @@ function pa() {
pa
assert_z "$count"
assert_z "$fixed"
assert_eq "$efixed" 1
assert_z "$mopt"
assert_z "$dmopt"
assert_z "$oopt"
@ -69,6 +72,14 @@ assert_eq "$fixed" 42
pa -fff
assert_eq "$fixed" 42
assert_same "$efixed" "1"
pa -e
assert_same "$efixed" ""
pa -ee
assert_same "$efixed" ""
pa -eee
assert_same "$efixed" ""
pa -a ""
assert_not_array mopt
assert_same "$mopt" ""