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.

In reply to Re: Getopt::Long return value by PetaMem
in thread Getopt::Long return value by PetaMem

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.