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

Hi Monks,

I have the following problem, for example:
    myPerlProg.pl -number -10
or
    myPerlProg.pl -number "-10"

In this example the value -10 is seen as an options and not as the value of -number
This is a snippet of how I use getOptions:
    $p = new Getopt::Long::Parser ;
    $p->configure('pass_through') ;
    $getoptret = $p->getoptions(    
                   'number:s'  =>\$num,
                    .......) ;
If I remove the line with configure I get a message that 10 is an unknown option!
All suggestions are welcome !!
Thanks in advance
Luca

Replies are listed 'Best First'.
Re: getOptions() parse problem
by gargle (Chaplain) on Sep 13, 2005 at 09:08 UTC

    Hi,

    Why not use -- (double dash) to indicate the option's name? You'd avoid ambiguity with negative numbers

    The cpan module has more info about it.

    --
    if ( 1 ) { $postman->ring() for (1..2); }
      Ok, that works, thanks, but is there no other way to let getOptions ignore -10 as an options. Because I would like to use both - and --, like:
          > myPerl.pl -n -10
      or
          > myPerl.pl --number -10
      
      Both represent the same thing! So in case I only use -- I have to use --n, to me that doesn't look right!!
      
      Any comments on this ?
      Thanks
      Luca

        Hi again,

        I see you declare your 'option' as an optional string (:s):

        $getoptret = $p->getoptions( 'number:s' =>\$num, .......) ;

        try to change it in a mandatory integer:

        $getoptret = $p->getoptions( 'number=i' =>\$num, .......) ;

        According to the camel book:

        =i
        Option takes a mandatory integer argument. This value will be assigned to the option variable. Note that the value may start with - to indicate a negative value.
        --
        if ( 1 ) { $postman->ring() for (1..2); }