Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot

Help required on Getopt::Long

by ghosh123 (Monk)
on May 16, 2012 at 09:32 UTC ( #970784=perlquestion: print w/replies, xml ) Need Help??
ghosh123 has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monk,
In my program, I am using the module Getopt::Long My intension is that if the user chooses "--help", the program should show the help message without bothering whether other options has been used or not. My program (say, :

use Getopt::Long my $abc; my $help; GetOptions ( "abc=s" => \$abc, "help" => \$help ); if ($help) { print "This is help message\n" ; exit ;} if ($abc) { print "You have choosen \"abc\" as $abc" };

If I run this:
1. -help
Output: This is help message

2. -abc ABC
Output : You have choosen abc as ABC

3. -abc -help
Output: You have choosen abc as -help

Out of the above results 1 & 2 are fine, but 3 is not as I want. I understand what's happening here $abc is assigned as "-help", but don't have idea how to implement it properly so that in the 3rd it would print like " This is help message" .

Can anybody help please ?
Thanks and regards

Replies are listed 'Best First'.
Re: Help required on Getopt::Long
by Anonymous Monk on May 16, 2012 at 09:48 UTC
    This works as intended, --abc requires a string argument, so there is no way to make an exception for your case without breaking it for everyone else. I propose two work-arounds:

    Mention in the documentation that --help should come as first option (circular problem is some cases).

    Filter @ARGV for --help yourself before you call GetOptions.

Re: Help required on Getopt::Long
by zeni (Beadle) on May 16, 2012 at 09:54 UTC

    This is because -help is treated as string for the option "abc". even though '=' is not used the next string is tied for "abc" option.

    Also, when i executed this code, i got the following error:

    Undefined subroutine &main::Getoptions called.

    Always include use strict and use warnings in your code. And the subroutine should be GetOptions
      read "Getoptions" as "GetOptions". Sorry for the typo.
Re: Help required on Getopt::Long
by Anonymous Monk on May 16, 2012 at 13:23 UTC

    pass_through configuration option and &GetOptionsFromArray has a solution -- complete with option requirement type checking -- for you ...

    cat perl -h -p 1 ; \ perl -p q -h ; \ perl -p -h ; \ perl -p q ; \ perl -p ; \ perl -p 3 use strict; use warnings; # May or may not work wit previous version(s). use Getopt::Long 2.38; my ( $h ); Getopt::Long::Configure( 'pass_through' ); GetOptions( 'help' => \$h ); die "HELP" if $h; my ( $p ); Getopt::Long::Configure( 'nopass_through' ); Getopt::Long::GetOptionsFromArray ( \@ARGV , 'p=i' => \$p ) or die; printf "p: %s\n" , defined $p ? $p : 'undef'; __END__ HELP at line 10. HELP at line 10. HELP at line 10. Value "q" invalid for option p (number expected) Died at line 14. Option p requires an argument Died at line 14. p: 3

      Come to think of it, Getopt::Long::GetOptionsFromArray( \@ARGV , ... ) can be replaced with GetOptions( ... ).

Re: Help required on Getopt::Long
by uday_sagar (Scribe) on May 16, 2012 at 11:05 UTC

    Problem Solved!

    Just replace '=' with ':' at line 5

    Here is the code.

    use Getopt::Long; my $abc; my $help; GetOptions ( "abc:s" => \$abc, "help" => \$help ); if ($help) { print "This is help message\n" ; exit ;} if ($abc) { print "You have choosen \"abc\" as $abc" };

    You can get more information on Getopt::Long here

      Replacing equal sign with colon alone does not solve the problem. It changes the problem by making the value for option abc optional instead of being required; equals sign impose that a value need to be specified ...

      = type [ desttype ] [ repeat ] The option requires an argument of the given type. . . . : type [ desttype ] Like "=", but designates the argument as optional. If omitted, an empty string will be assigned to string values options, and the value zero to numeric options.

        ".. equals sign impose that a value need to be specified ..." -- self.

        GAAH! Make that "... equals sign imposes that a value is need to be specified ...".

Re: Help required on Getopt::Long
by vagabonding electron (Chaplain) on May 16, 2012 at 12:49 UTC
      It is not robust enough in case "help" is part of a string of a value to another option or an argument passed.
        but that seems to be easily remedied with anchors in the expression (and -- for ease of illustration -- using a defined array rather than @ARGV):
        #!/usr/bin/perl use 5.014; my $count; my @arr = qw/helper abc help def helpless helpful whelp help/; $count = grep {/^help$/}@arr and print "This is a help message. "; +# Note1 say "\$count: $count"; # This is a help message. $count: 2
        Note1: can also be written $count = grep /^help$/, @arr ....

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://970784]
Front-paged by Arunbear
[Corion]: hippo: Yes, but I'm not sure that it's even worth the effort of implementing it at all...
[Corion]: You'll only ever need that option if you have a long-running query whose results are not cached by your DB already, and in those cases I presume that the programmer will want to maintain the temporary tables themselves - I wouldn't know when to drop ...
[Corion]: ... the temporary tables, and also don't have a good idea on how to create unique table names for them
[hippo]: OIC. In that case leave it out but invite feature requests and see if any of the users suggest it. :-)
[Corion]: Talking about this makes me realize that it's likely only a half useful idea. But it still would be convenient to have as an option...
[Corion]: hippo: Hmmm - yeah, I could document it and wait for code implementing that option to show up ;-D

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (11)
As of 2017-02-23 15:36 GMT
Find Nodes?
    Voting Booth?
    Before electricity was invented, what was the Electric Eel called?

    Results (347 votes). Check out past polls.