Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Help required on Getopt::Long

by ghosh123 (Monk)
on May 16, 2012 at 09:32 UTC ( [id://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, test.pl) :

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. test.pl -help
Output: This is help message

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

3. test.pl -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 p.pl perl p.pl -h -p 1 ; \ perl p.pl -p q -h ; \ perl p.pl -p -h ; \ perl p.pl -p q ; \ perl p.pl -p ; \ perl p.pl -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 p.pl line 10. HELP at p.pl line 10. HELP at p.pl line 10. Value "q" invalid for option p (number expected) Died at p.pl line 14. Option p requires an argument Died at p.pl 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 (Curate) 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?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://970784]
Front-paged by Arunbear
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others perusing the Monastery: (5)
As of 2024-04-19 13:34 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found