in reply to Getopt::Long return value

Hi, there is a simple solution:

I have the "Programming with perl-Modules" from Patwardhan and Irving (year 1997) where this information is not present...

#!/usr/bin/perl -w use strict; use Getopt::Long; # If it comes to parallelizing, How many processes do we allow to star +t # default is amount of CPUs on that system. May be overridden by comma +ndline my $cpu = &num_cpus; my $goback = GetOptions('cpu=i', \$cpu, 'c=i', \$cpu, 'help', \&cmdline_usage, 'h', \&cmdline_usage); print "Number of CPUs here: $cpu\n"; print "Goback was: $goback\n"; # {{{ get the number of CPUs on that system # returns: number of CPUs or 1 if no /proc/cpuinfo was found sub num_cpus { my $num_cpus = 0; my $infofile = '/proc/cpuinfo'; if($^O eq 'linux') { # Yes this is Linux we're running on if(-e $infofile) { # and cpuinfo exists open FILE, $infofile; while(<FILE>) { $num_cpus++ if(/processor/); } close FILE; } } # elsif ... your favourite OS detection routine here # If this is not a known OS we're running on ($num_cpus is then 0) # so better be careful and assume 1 CPU return $num_cpus || 1; } # }}}
now this gives ok results *most* of the cases, but if you have commandline parameters separatedly, GetOpt silently ignores them:

rj@proxima:~ > pxp.pl -cpu 1
Number of CPUs here: 1
Goback was: 1

=> Ok

rj@proxima:~ > pxp.pl -cpu
Option cpu requires an argument
Number of CPUs here: 2
Goback was:

=> Ok

rj@proxima:~ > pxp.pl -cpu 1 -h=1
Option h does not take an argument
Number of CPUs here: 1
Goback was:

=> Ok

rj@proxima:~ > pxp.pl -cpu 1 -h 1
Usage:
 
-c, --cpu        set the number of CPUs (define parallelization)
-h, --help       print this usage information
Number of CPUs here: 1
Goback was: 1

=> IMHO not Ok
Ciao

Update: The last case definitedly is not ok according to 1211. Iīm citing:

If the option specifier is ``one:i'' (i.e. takes an optional integer argument), then the
  following situations are handled: 

     -one -two            -> $opt_one = '', -two is next option
     -one -2              -> $opt_one = -2
Now that means it WOULD have been ok if I declared h as h:i, but I havenīt.

Replies are listed 'Best First'.
Re: Re: Getopt::Long return value
by bikeNomad (Priest) on Jun 26, 2001 at 00:40 UTC
    What is the output when you call Getopt::Long::Configure('debug') ?

    update: fixed spelling on Configure

      Here it is:
      rj@proxima:~/proj/elric/src > pxp.pl GetOpt::Long 2.23 called from package "main". GetOptionsAl $Revision: 2.27 $ ARGV: () autoabbrev=1,bundling=0,getopt_compat=1,order=1, ignorecase=1,passthrough=0,genprefix="(--|-|\+)". => link "cpu" to SCALAR(0x8142340) => link "c" to SCALAR(0x8142340) => link "help" to CODE(0x814258c) => link "h" to CODE(0x814258c) => $opctl{"cpu"} = "=i" $opctl{"h"} = "" $opctl{"help"} = "" $opctl{"c"} = "=i"
Re: Re: Getopt::Long return value
by Anonymous Monk on Jun 26, 2001 at 01:25 UTC
    Why not? How else would you get a File named "1" into your script?
Re: Re: Getopt::Long return value
by bikeNomad (Priest) on Jun 26, 2001 at 06:48 UTC
    I don't see what's wrong here (in the -cpu 1 -h 1 case).

    If you look at @ARGV after the GetOptions call, it should contain the argument 1.

    As the AM said, how does anyone know that the 1 is a mistaken argument? It looks like a file name to me (and to GetOptions)... what happens if you say -h 1 -cpu 1? It should complain.

      It doesnīt:

      rj@satyr: pxp.pl -h 1 -cpu 1 Usage: elric [option] -c, --cpu set the number of CPUs (define parallelization) -h, --help print this usage information Number of CPUs here: 1 Goback was: 1
      Bug here?

      Ciao