From e9b455de2adab1e5cd50c42f2a4e954a7872ab7f Mon Sep 17 00:00:00 2001 From: Jephte CLAIN Date: Sat, 7 Dec 2013 16:09:47 +0400 Subject: [PATCH] =?UTF-8?q?maj=20de=20genparse=20pour=20g=C3=A9n=C3=A9rer?= =?UTF-8?q?=20l'aide=20=C3=A0=20partir=20de=20variables=20HELP=5FDESC,=20H?= =?UTF-8?q?ELP=5FARG=5FDESC,=20...?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/templates/shell | 22 ++--------- ulib/.ulib_version | 2 +- ulib/base | 92 +++++++++++++++++++++++++++++---------------- 3 files changed, 65 insertions(+), 51 deletions(-) diff --git a/lib/templates/shell b/lib/templates/shell index 5aad6db..4227bd9 100755 --- a/lib/templates/shell +++ b/lib/templates/shell @@ -145,31 +145,17 @@ Voulez-vous générer un script avec cette configuration?" X && echo >"$file" '#!/bin/bash '"$modeline"' source "$(dirname "$0")/ulib/auto" || exit 1 +#HELP_DESC="$scriptname: " +#HELP_ARG_DESC= eval "$(genparse)" - -function display_help() { - uecho "$scriptname: - -USAGE - $scriptname [options] - -OPTIONS" -} ' elif [ "$type" == default ]; then echo >"$file" '#!/bin/bash '"$modeline"' source /etc/ulibauto || exit 1 +#HELP_DESC="$scriptname: " +#HELP_ARG_DESC= eval "$(genparse)" - -function display_help() { - uecho "$scriptname: - -USAGE - $scriptname [options] - -OPTIONS" -} ' elif [ "$type" == manual ]; then echo >"$file" '#!/bin/bash diff --git a/ulib/.ulib_version b/ulib/.ulib_version index 8351c19..60d3b2f 100644 --- a/ulib/.ulib_version +++ b/ulib/.ulib_version @@ -1 +1 @@ -14 +15 diff --git a/ulib/base b/ulib/base index c3c5fc1..05c5f42 100644 --- a/ulib/base +++ b/ulib/base @@ -2778,13 +2778,20 @@ function __genparse_shortopt() { shortopt="${shortopt,,}" [ -n "$shortopt" ] && echo "$shortopt" } +HELP_DESC= +HELP_USAGE= +HELP_OPTIONS= function genparse() { # Afficher une ligne de commande à évaluer pour simplifier l'utilisation de # parse_opts(). Une fonction display_help() par défaut est définie et les # options appropriées de parse_opts sont utilisées pour reconnaître les options # spécifiées par les arguments. # Cette fonction peut être utilisée de cette manière: +# HELP_DESC=... +# HELP_ARG_DESC=... # pour chaque arg # eval "$(genparse [args...])" +# D'autres variables peuvent être définies: HELP_USAGE, HELP_OPTIONS, +# HELP_ARG_OPTION. Consulter le source pour connaitre leur utilisation # Les arguments de cette fonction sont de la forme 'sansarg' pour une option # simple qui ne prend pas d'argument ou 'avecarg=[default-value]' pour une # option qui prend un argument. Les options générées sont des options @@ -2796,31 +2803,39 @@ function genparse() { # Par exemple, la commande suivante: # genparse Force enCoding=utf-8 input= long-Option= # affichera ceci: -# function display_help() { -# uecho "USAGE -# $scriptname [options] -# -# OPTIONS -# -f, --force -# -c, --encoding ENCODING -# --input INPUT -# -o, --long-option LONG_OPTION" -# } -# -# force= -# encoding=utf-8 -# input= -# long_option= -# parse_opts "${PRETTYOPTS[@]}" \ -# --help '$exit_with display_help' \ -# -f,--force force=1 \ -# -c:,--encoding: encoding= \ -# --input: input= \ -# -o:,--long-option: long_option= \ -# @ args -- "$@" && set -- "${args[@]}" || die "$args" +# function display_help() { +# [ -n "$HELP_USAGE" ] || HELP_USAGE="USAGE +# $scriptname [options]" +# [ -n "$HELP_OPTIONS" ] || HELP_OPTIONS="OPTIONS +# ${HELP_FORCE_OPTION:- -f, --force${HELP_FORCE_DESC:+ +# $HELP_FORCE_DESC}} +# ${HELP_ENCODING_OPTION:- -c, --encoding VALUE${HELP_ENCODING_DESC:+ +# ${HELP_ENCODING_DESC}}} +# ${HELP_INPUT_OPTION:- --input VALUE${HELP_INPUT_DESC:+ +# ${HELP_INPUT_DESC}}} +# ${HELP_LONG_OPTION_OPTION:- -o, --long-option VALUE${HELP_LONG_OPTION_DESC:+ +# ${HELP_LONG_OPTION_DESC}}}" +# uecho "${HELP_DESC:+$HELP_DESC +# +# }$HELP_USAGE${HELP_OPTIONS:+ +# +# $HELP_OPTIONS}" +# } +# +# force= +# encoding=utf-8 +# input="" +# long_option="" +# parse_opts "${PRETTYOPTS[@]}" \ +# --help '$exit_with display_help' \ +# -f,--force force=1 \ +# -c:,--encoding: encoding= \ +# --input: input= \ +# -o:,--long-option: long_option= \ +# @ args -- "$@" && set -- "${args[@]}" || die "$args" - local -a descs vars options - local desc var option name value shortopt + local -a names descs vars options + local i desc var option name value shortopt # analyser les arguments for var in "$@"; do @@ -2829,6 +2844,7 @@ function genparse() { shortopt="$(__genparse_shortopt "$name")" option="${name,,}" name="${option//-/_}" + array_add names "$name" array_add descs "${shortopt:+-$shortopt, }--$option VALUE" array_add vars "$(set_var_cmd "$name" "$value")" array_add options "${shortopt:+-$shortopt:,}--$option: $name=" @@ -2837,6 +2853,7 @@ function genparse() { shortopt="$(__genparse_shortopt "$name")" option="${name,,}" name="${option//-/_}" + array_add names "$name" array_add descs "${shortopt:+-$shortopt, }--$option" array_add vars "$name=" array_add options "${shortopt:+-$shortopt,}--$option $name=1" @@ -2845,19 +2862,30 @@ function genparse() { # afficher la commande parse_opts echo -n 'function display_help() { - uecho "\ -USAGE + [ -n "$HELP_USAGE" ] || HELP_USAGE="USAGE $scriptname' + [ -n "$descs" ] && echo -n ' [options]' + echo '"' if [ -n "$descs" ]; then - echo -n ' [options] - -OPTIONS' - for desc in "${descs[@]}"; do + echo -n ' [ -n "$HELP_OPTIONS" ] || HELP_OPTIONS="OPTIONS' + i=0 + while [ $i -lt ${#descs[*]} ]; do + name="${names[$i]}" + desc="${descs[$i]}" echo -n " - $desc" +\${HELP_${name^^}_OPTION:- $desc\${HELP_${name^^}_DESC:+ + \${HELP_${name^^}_DESC// +/ + }}}" + i=$(($i + 1)) done + echo '"' fi - echo '" + echo ' uecho "${HELP_DESC:+$HELP_DESC + +}$HELP_USAGE${HELP_OPTIONS:+ + +$HELP_OPTIONS}" } ' for var in "${vars[@]}"; do