maj de genparse pour générer l'aide à partir de variables HELP_DESC, HELP_ARG_DESC, ...
This commit is contained in:
parent
ca5ac77aec
commit
e9b455de2a
|
@ -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
|
||||
|
|
|
@ -1 +1 @@
|
|||
14
|
||||
15
|
||||
|
|
92
ulib/base
92
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
|
||||
|
|
Loading…
Reference in New Issue