autodebug et autohelp

This commit is contained in:
Jephté Clain 2024-03-29 12:04:00 +04:00
parent a02d25f8d1
commit bc14f4b4ca
2 changed files with 21 additions and 35 deletions

View File

@ -154,7 +154,8 @@ function parse_args() {
}
function __parse_args() {
## tout d'abord, construire la liste des options
local __AUTOHELP=1 # faut-il gérer automatiquement l'affichage de l'aide?
local __AUTOH=1 __AUTOHELP=1 # faut-il gérer automatiquement l'affichage de l'aide?
local __AUTOD=1 __AUTODEBUG=1 # faut-il rajouter les options -D et --debug
local __ADVHELP # y a-t-il des options avancées?
local __popt __sopts __lopts
local -a __defs
@ -187,12 +188,15 @@ function __parse_args() {
# -o
__def="${__def#-}"
__sopts="$__sopts$__def$__witharg"
[ "$__def" == h ] && __AUTOH=
[ "$__def" == D ] && __AUTOD=
else
# -longopt ou longopt
__def="${__def#-}"
__lopts="$__lopts${__lopts:+,}$__def$__witharg"
fi
[ "$__def" == help -o "$__def" == help++ ] && __AUTOHELP=
[ "$__def" == debug ] && __AUTODEBUG=
done
# sauter l'action
shift
@ -203,10 +207,10 @@ function __parse_args() {
fi
done
# __sopts ne doit pas être vide, rajouter ? si aucune option courte n'est
# définie, indépendamment de la valeur de autohelp
[ -n "$__AUTOH" ] && __sopts="${__sopts}h"
[ -n "$__AUTOHELP" ] && __lopts="$__lopts${__lopts:+,}help,help++"
[ -n "$__sopts" ] || __sopts=?
[ -n "$__AUTOD" ] && __sopts="${__sopts}D"
[ -n "$__AUTODEBUG" ] && __lopts="$__lopts${__lopts:+,}debug"
__sopts="$__popt$__sopts"
local -a __getopt_args
@ -456,8 +460,12 @@ function __parse_opt() {
[ -n "$__found" ] && return 0
done
if [ -n "$__AUTOH" -a "$option_" == -h ]; then
__action="showhelp@"
return 0
fi
if [ -n "$__AUTOHELP" ]; then
if [ "$option_" == --help -o "$option" == -? ]; then
if [ "$option_" == --help ]; then
__action="showhelp@"
return 0
elif [ "$option_" == --help++ ]; then
@ -465,6 +473,14 @@ function __parse_opt() {
return 0
fi
fi
if [ -n "$__AUTOD" -a "$option_" == -D ]; then
__action=set_debug
return 0
fi
if [ -n "$__AUTODEBUG" -a "$option_" == --debug ]; then
__action=set_debug
return 0
fi
# ici, l'option n'a pas été trouvée, on ne devrait pas arriver ici
eerror "Unexpected option '$option_'"; eval "$__DIE"
}

View File

@ -1,30 +0,0 @@
#@include "base.tools.awk"
BEGIN {
PROFILES[0] = 0; delete PROFILES
PROFILES[0] = "prod"
PROFILES[1] = "test"
HOSTS[0] = 0; delete HOSTS
HOSTS[0] = "first"
HOSTS[1] = "second"
VALUES[0] = 0; delete VALUES
}
{ if (should_generate_password()) { generate_password() } }
/\#@@EACH:PROFILES@@/ {
sub(/\#@@EACH:PROFILES@@/, "")
for (index in PROFILES) {
print gensub(/@@PROFILE@@/, PROFILES[index], "g")
}
next
}
/\#@@EACH:HOSTS@@/ {
sub(/\#@@EACH:HOSTS@@/, "")
for (index in HOSTS) {
print gensub(/@@HOST@@/, HOSTS[index], "g")
}
next
}
/\#@@EACH:VALUES@@/ {
sub(/\#@@EACH:VALUES@@/, "")
next
}
{ print }