Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Re: getopts and -?

by kejohm (Hermit)
on May 10, 2011 at 01:20 UTC ( #903871=note: print w/ replies, xml ) Need Help??


in reply to getopts and -?

Have a look at using the Getopt::Long module, specifically this.

Update: Link fixed.


Comment on Re: getopts and -?
Re^2: getopts and -?
by viffer (Sexton) on May 10, 2011 at 01:47 UTC
    Thank you for your response, however having looked at that, I can't see how I can combine the other options with the "help|?"

    I presume I need to define an $opt_help variable, but can't see how I can combine the

    getopts('dhlLio:P:pqrv:c');

    with

    #GetOptions ("help|?");

    patently

    GetOptions ("help|?dhlLio:P:pqrv:c");  

    doesn't do it!

      Getopt::Long doesn't work exactly like Getopt::Std, like I showed, so use
      GetOption( 'd=s' => $opt_d, # d 'h=s' => $opt_h, # h 'l=s' => $opt_l, # l 'L=s' => $opt_L, # L 'i=s' => $opt_i, # i 'o!' => $opt_o, # o: 'P!' => $opt_P, # P: 'p=s' => $opt_p, # p 'q=s' => $opt_q, # q 'r=s' => $opt_r, # r 'v!' => $opt_v, # v: 'c=s' => $opt_c, # c )
      or
      GetOption( %h,#values in %h 'd=s', # d 'h=s', # h 'l=s', # l 'L=s', # L 'i=s', # i 'o!', # o: 'P!', # P: 'p=s', # p 'q=s', # q 'r=s', # r 'v!', # v: 'c=s', # c )
      generated with
      #!/usr/bin/perl -- use strict; use warnings; Main( @ARGV ); exit( 0 ); sub Main { StdToLong('dhlLio:P:pqrv:c'); } BEGIN { my %Flags = ( #~ ':' => '', # boolean, default ':' => '!', # boolean, explicit, negatable '' => '=s', ); sub StdToLong { StdToLong1(@_); StdToLong2(@_); } sub StdToLong1 { print "GetOption(\n"; while( $_[0] =~ m/(\w)(\W)?/g ){ no warnings 'uninitialized'; print qq[ '$1$Flags{$2}' => \$opt_$1, # $1$2 \n]; } print "\)\n"; } sub StdToLong2 { print "GetOption( \%h,#values in %h\n"; while( $_[0] =~ m/(\w)(\W)?/g ){ no warnings 'uninitialized'; print qq[ '$1$Flags{$2}', # $1$2 \n]; } print "\)\n"; } } __END__

      The parameters passed to the Getopt::Long::GetOptions() function are different to the Getopt::Std::getopts() function. One way to call GetOptions() is to pass a hash with the keys specifying each option, and the values specifying a reference to a variable that will be set. Here is an example based on your code:

      our ( $opt_d,$opt_h,$opt_l,$opt_L,$opt_i,$opt_o,$opt_P,$opt_p, $opt_q,$opt_r,$opt_v,$opt_c,$opt_help ); GetOptions( 'd' => \$opt_d, 'h' => \$opt_h, 'l' => \$opt_l, 'L' => \$opt_L, 'i' => \$opt_i, 'o=s' => \$opt_s, 'P=s' => \$opt_P, 'p' => \$opt_p, 'q' => \$opt_q, 'r' => \$opt_r, 'v=s' => \$opt_v, 'c' => \$opt_c, 'help|?' => \$opt_help, );

      Since your code has a lot of options, you may want to look at storing the options in a hash, rather than individual variables. Check out the Getopt::Long manpage for more info.

        Why package variables? my works as well, and you can even write:
        GetOptions( d => \my $opt_d, h => \my $opt_h, l => \my $opt_l, ... ) or die "Failed to parse options";

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://903871]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (10)
As of 2014-12-21 12:53 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (105 votes), past polls