From d7323d7d45a73edf8d033253a6b52ae8a3f6e25e Mon Sep 17 00:00:00 2001 From: Jephte Clain Date: Fri, 15 Dec 2023 11:45:24 +0400 Subject: [PATCH] modifs.mineures sans commentaires --- bash/src/base.args.sh | 50 +++++++++++++++++------------------- bash/tests/test-args-base.sh | 11 ++++++++ 2 files changed, 35 insertions(+), 26 deletions(-) diff --git a/bash/src/base.args.sh b/bash/src/base.args.sh index aa846bb..bd3d8ec 100644 --- a/bash/src/base.args.sh +++ b/bash/src/base.args.sh @@ -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 + eval "[ -n \"\$$1\" ] || let $1=0" + eval "let $1=$1+1" + } + function dec@() { + eval "[ -n \"\$$1\" ] && let $1=$1-1" + eval "[ $1 -eq 0 ] && $1=" } function res@() { local __value="${value_:-$2}" @@ -219,20 +219,18 @@ function __parse_args() { eval "$1+=(\"\$__value\")" } function set@() { - if [ -n "$__witharg" ]; then - if [ -n "$__add" ]; then - if is_array "$1"; then - add@ "$@" - elif ! is_defined "$1"; then - # première occurrence: variable - res@ "$@" - else - # deuxième occurence: tableau - [ -z "${!1}" ] && eval "$1=()" - add@ "$@" - fi - else + if [ -n "$__fixvalue" ]; then + res@ "$@" + elif [ -n "$__witharg" ]; then + if is_array "$1"; then + add@ "$@" + elif ! is_defined "$1"; then + # première occurrence: variable res@ "$@" + else + # deuxième occurence: tableau + [ -z "${!1}" ] && eval "$1=()" + add@ "$@" fi else inc@ "$@" @@ -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 diff --git a/bash/tests/test-args-base.sh b/bash/tests/test-args-base.sh index 02ebd6b..0b77517 100755 --- a/bash/tests/test-args-base.sh +++ b/bash/tests/test-args-base.sh @@ -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" ""