Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: Living without zsh: command line parsing

    Hi all :)

    I've found a solution, and I give here just in case someone has
the curiosity.

* DervishD <raul@xxxxxxxxxxxx> dixit:
>     What I want to do, and I *really* need your help for that, is the
> following: I have a command line like, let's say
>     $./0 --enable-feature1 --enable-feature2 --disable-feature1 ...
>     I want to parse that command line and store some values in
> variables for that. I cannot depend on 'getopt', so I parse the
> command line using a case construct. When I parse the above command
> line, what I do now is something like:
> # Note that some code relating to checks is missing
>     --enable*)
>         argument="${option#--enable}"
>         argument="${argument#-}"
>         XDEFS="${XDEFS} -D_ENABLE_$argument -U_DISABLE_$argument" ;;
>     --disable*)
>         argument="${option#--disable}"
>         argument="${argument#-}"
>         XDEFS="${XDEFS} -D_DISABLE_$argument -U_ENABLE_$argument" ;;

    Now I use the following:

        eval ENABLE_$argument=yes
        eval unset DISABLE_$argument
        FEATURES="$FEATURES $argument"
        eval DISABLE_$argument=yes
        eval unset ENABLE_$argument
        FEATURES="$FEATURES $argument"

    And after doing the command line parsing I do:
FEATURES=`printf -- "$FEATURES"|tr -s ' ' '\n'|sort|uniq|tr -s '\n' ' '`

    This way, 'FEATURES' contain a list of the selected features,
with no repeated ocurrences. This variable can be processed later
using a for loop. All is SUSv3 compliant AFAIK.

    As I told the solution was simple but I was missing it.
    Raúl Núñez de Arenas Coronado

Linux Registered User 88736
http://www.pleyades.net & http://raul.pleyades.net/

Messages sorted by: Reverse Date, Date, Thread, Author