isposix

Un outil pour savoir si une commande est posix - retour accueil

git clone git://bebou.netlib.re/isposix
Log | Files | Refs | README |

commit 8d24f7c709b99feb235956aa88c94e23e95a1b86
parent ab66e31bd709609690c1a4208d82e3a342899d9f
Auteurice: Arthur Pons <arthur.pons@unistra.fr>
Date:   Sun, 24 Nov 2024 22:25:58 +0100

Utilisation de getopts pour parser les options

Pour gérer les options concaténées genre -laH

Diffstat:
Misposix | 51++++++++++++++++++++++++++-------------------------
1 file changed, 26 insertions(+), 25 deletions(-)

diff --git a/isposix b/isposix @@ -2,22 +2,22 @@ set -e red="\033[91m";green="\033[92m";blue="\033[94m";norm="\033[0m" -_die() { echo "$red$@$norm" >&2 ; } -_success() { echo "$green$@$norm" >&2; } -_inform() { echo "$blue$@$norm" >&2; } +_die() { printf "$red$@$norm" >&2 ; } +_success() { printf "$green$@$norm" >&2; } +_inform() { printf "$blue$@$norm" >&2; } _cmdnotposix() { if command -V fzy 2>&1 > /dev/null;then fzcmd=$(find bdd -name '*.html' | xargs -I{} basename {} | sed 's/\.html$//' | fzy -e "$cmd" | head -n3) fi - [ -z "$fzcmd" ] && { _die "$cmd is not posix";exit 1;} + [ -z "$fzcmd" ] && { _die "$cmd is not posix"; return 1;} if [ -t 0 ];then - _inform "$cmd appears not to be posix, did you mean :$norm" + _inform "$cmd appears not to be posix, did you mean : $norm" cmd=$(echo "$fzcmd" | fzy) else - _inform "$cmd appears not to be posix, did you mean :\n$fzcmd" - exit 1 + _inform "$cmd appears not to be posix, did you mean :\n$(echo "$fzcmd" | paste -s -d' or ')" + return 1 fi } @@ -35,27 +35,28 @@ usage() { } checkoption() { - < bdd/$cmd.html grep -q "<dt><b>$option.*</dt>" \ - && { _success "$option is a posix option";return 0;} \ - || { _die "$option is not a posix option"; return 1;} + cmd="$1";option="$2" + < bdd/$cmd.html grep -Eq "<dt><b>-$option.*</dt>" \ + && { _success " -$option";return 0;} \ + || { _die " -$option"; return 1;} } -isposix=0 -cmd="$1" - -[ "$cmd" = "-h" ] && { usage; exit 0; } - -[ ! -f bdd/$cmd.html ] && _cmdnotposix -_success "$cmd is a posix command" - -shift;options="$@" -for option in "$@";do - checkoption || isposix=1 +cat | while read line;do + set -- $line + isposix=0 + cmd="$1";shift + [ ! -f bdd/$cmd.html ] \ + && { _cmdnotposix;isposix=1; } \ + || _success "$cmd" + while getopts "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" opt; do + case "$opt" in + ( * ) checkoption "$cmd" "$opt" || isposix=1;; + esac + done + [ $isposix = "0" ] \ + && _success " : is posix\n" \ + || _die " : not posix\n" done -[ $isposix = "0" ] \ - && _success "$cmd $@ is posix" \ - || { _die "$cmd $@ is not posix"; exit 1; } - exit $isposix