Ace128 has asked for the wisdom of the Perl Monks concerning the following question:

Hey friends,

Well, using this module, I stumbled apponed something annoying. Im planing of using int arguments for creating SQL queries. Now, one can type:
--size=45
and there is no problem. However, one is probably wanna do < 45 or > 45 more often, but how to make this reality here? If I do:
--size=>45
it would be neat, but it doesnt work since the shell thinks its redirecting and stuff. Ideas for this? I was thinking of something like:
--size=45 --extra=gt
but that just complicates things... And is specially bad because of lot more typing...

One other way would to use string there instead of int, and parse that. (This is probably the way I will go, if noone comes up with something revolutionary :) ) Like:
--size=gt45
but that doesnt look too intuitive...

What would you, as a user, prefer? :)

Thanks,
Ace

Replies are listed 'Best First'.
Re: Special chars in arguments using GetOptions (Getopt::Long)
by Aristotle (Chancellor) on Nov 27, 2005 at 01:01 UTC

    Getopt::Long is a red herring. You’d have these problems regardless of what program you’re invoking, whether it’s a Perl script, whether its uses a command line parsing module, and which. That goes for your plus sign getting removed, too:

    $ cat x.pl
    use Getopt::Long;
    GetOptions( 'test=s' => \my $opt_test );
    print $opt_test, "\n";
    
    $ ./x.pl --test +10
    +10
    

    You want to learn about shell quoting. Something like --size \>45 or --size '>45' whatever will do the job, at least on a Bourne-derived shell as is common on Unixoid systems. If you’re using some other kind of shell, find out how to quote things in it. (Btw, contrary to widespread misunderstanding, quoting characters are completely transparent to the program being invoked; you can quote each word on the command line with no difference to the outcome, ie you can just as well say '--size' '>45' or such.)

    Makeshifts last the longest.

Re: Special chars in arguments using GetOptions (Getopt::Long)
by BUU (Prior) on Nov 26, 2005 at 23:24 UTC
    TESTS Numeric arguments can be specified as +n for greater than n, -n for less than n, n for exactly n.
    And so on.
      Wow, think I have drunk too much alcohol ;) since that is pretty clever indeed and I never thought of it..! :)

      Although a problem accures if the user really mean -, and not <... (although I think I get it through the context, unless it can be both)

      UPDATE: Well, seems that + is removed from the argument by GetOptions... which means I cant know if user typed it or not...
Re: Special chars in arguments using GetOptions (Getopt::Long)
by pileofrogs (Priest) on Nov 27, 2005 at 05:42 UTC

    Your problem isn't with perl, it's with your shell, so you should probably solve it with your shell instead of with perl.

    I'm using bash, so I can make it work by escaping the '<' and '>' chars: E.G.
    --size=\<45

    Or can quote the whole arg, E.G.:
    --size="<45"

    If you don't want your users to have to deal with that, maybe you'd be better off with different arg names, like:

    --min-size=45

    or

    --max-size=45

    Which would be logically equivilent to --size>45 and --size<45 respectively

    -Pileofrogs

Re: Special chars in arguments using GetOptions (Getopt::Long)
by ambrus (Abbot) on Nov 27, 2005 at 11:36 UTC

    This is a matter of taste, and there are many good solutions.

    I'd however recommend using a comma:

    • --size=45 = exactly 45
    • --size=45, = at least 45
    • --size=,45 = at most 45
    • --size=20,45 = between 20 and 4t

    However, using two options like --size-min and --size-max might be better in some cases.

    Update: I'm not very familiar with Windows shells, so this might not be a good solution on Windows.