http://qs321.pair.com?node_id=1119335


in reply to GetOpt Organization

My default - less or more:

use Getopt::Long; use Pod::Usage; # IMHO a must Getopt::Long::Configure("no_ignore_case"); # double your options ;-) GetOptions(\%options, "help", "foo=s","bar=s",); # or what ever pod2usage( -exitstatus => 0, -verbose => 2 ) if $options{help}; foreach my $option (%options ) { pod2usage( -exitstatus => 2, -verbose => 1 ) unless $option; } __END__ # POD section here

Something like this is also very handy (should be self explaining):

GetOptions( \%options, "help", "suffix:s", "amount=i", "dir=s", "logpath=s", " +From=s", "Subject:s", "Data:s", "Host:s", "patterns=s{1,}" => \@patterns, "To=s{1,}" => \@recipients ); # stuff __END__ ./foo.pl -a 2 \ -d /path/to/dir \ -l /path/to/logs \ -F user@domain -s suffix \ -p foo bar \ -T user@domain other@domain

Hint: If you declare "foo", you can call it as -f as well as --foo.

Regards, Karl

«The Crux of the Biscuit is the Apostrophe»

Replies are listed 'Best First'.
Re^2: GetOpt Organization (error check)
by Discipulus (Canon) on Mar 10, 2015 at 09:46 UTC
    tsk, tsk.. no monk checks for the return status of GetOptions ?? You want your program running with invalid option passed?

    Here is my usual pattern:
    use Getopt::Long; use Pod::Usage; unless ( GetOptions ( "color=s"=>\$par_color, "help" => \$par_help, ) ) {pod2usage(-verbose => 1,-exitval => 'NOEXIT'); &wait_for_in +put; exit;} if (defined $par_help){pod2usage(-verbose => 1,-exitval => 'NOEXIT'); +&wait_for_input; exit;}
    The function pod2usage supports -exitval => 'NOEXIT' (ie normally it exits the program unless you specify it to not do) useful if you started the program without a console, like in a Tk application as Tk Tartaglia's triangle fun - Pascal's triangle fun.

    HtH
    L*
    There are no rules, there are no thumbs..
    Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.

      tsk, tsk.. no monk checks for the return status of GetOptions ?? You want your program running with invalid option passed?

      The validation options and return value of GetOptions , and esp their combination, isn't that useful

      You still have to do your own validation, so let GetOptions warn, then write your program

        Yes you are absolutely right.

        There are no rules, there are no thumbs..
        Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.
      ...You want your program running with invalid option passed?

      No, perish the thought.

      When i run the snippet i posted with an invalid option i get:

      monks>options.pl --goo Unknown option: goo

      Best regards, Karl

      «The Crux of the Biscuit is the Apostrophe»

        Well, i have to dissent my friend, try to add print "Here i'm still alive!\n";
        at the bottom of your snippet: i get
        Unknown option: goo Here i'm still alive!


        L*
        There are no rules, there are no thumbs..
        Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.