zsh-users
 help / color / mirror / code / Atom feed
* Living without zsh: command line parsing
@ 2004-05-04 17:38 DervishD
  2004-05-05 10:29 ` DervishD
  0 siblings, 1 reply; 2+ messages in thread
From: DervishD @ 2004-05-04 17:38 UTC (permalink / raw)
  To: Zsh Users

    Hi all :)

    First of all, sorry for the length of the following message and
for being a bit off-topic, but I don't know anyone in the free
software world who knows more of shell scripting that you. So please
be patient with me O:)

    I have a problem with a free software project I'm making, where I
must process certain command line and store some values in a variable
depending on that command line. The problem is that I must do it so
it works with a Single Unix Specification V3 compliant shell, I want
to know how. If anyone has curiosity, all this is related with the
upcoming new version of MOBS, a building framework I use for my
projects, published in my homepage.

    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" ;;

    Well, this works, because when I use 'XDEFS' in the compilation I
have a series of -D and -U and only the lasts have effect, but what I
want is to have just one copy, that is, I want to have just one set
of '-D' and '-U' relating the $argument, the last one in fact. I have
the following limitations:

    - The syntax must be SUSv3 compatible, so no Zsh tricks :(
    - XDEFS is used for more options, so I cannot just throw away it.
    - I can have any number of --enable or --disable options with the
same $argument.
    - They can come in any order, interspersed with other options.

    I'm sure that there is a simple solution that I'm missing
completely that is far better than the duplication I'm currently
making :((

    Thanks a lot in advance :) If anyone wants to take a look at the
latest stable release of this project, which is no much different
than the development one, it is GPL'd and can be got in my homepage
(see the signature below). It's called MOBS (My Own Building System).

    Raúl Núñez de Arenas Coronado

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


^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: Living without zsh: command line parsing
  2004-05-04 17:38 Living without zsh: command line parsing DervishD
@ 2004-05-05 10:29 ` DervishD
  0 siblings, 0 replies; 2+ messages in thread
From: DervishD @ 2004-05-05 10:29 UTC (permalink / raw)
  To: Zsh Users

    Hi all :)

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

* DervishD <raul@pleyades.net> 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:

    --enable*)
        argument="${option#--enable}"
        argument="${argument#-}"
        eval ENABLE_$argument=yes
        eval unset DISABLE_$argument
        FEATURES="$FEATURES $argument"
 
    --disable*)
        argument="${option#--disable}"
        argument="${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/


^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2004-05-05 10:31 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2004-05-04 17:38 Living without zsh: command line parsing DervishD
2004-05-05 10:29 ` DervishD

Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/zsh/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).