Hi,

Your problem is that Getopt::Long is consuming your -help as the argument to 
-abc before it can be recognised. You might be able to do something with the 
other Getopt::* modules, but the following piece of code will do what you want 
if you really need it:

use List::MoreUtils qw(any);
use Getopt::Long;

my $abc;
my $help;

if ( any { $_ eq '-help' } @ARGV ) {
    $help = 1;
}

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 you don't have List::MoreUtils installed and can't install it, replace the 
'if ( any...)' block with:

foreach my $arg (@ARGV) {
    if ( $arg eq '-help' ) {
        $help = 1;
        last;
    }
}

Cheers,
Michael

-- 
Michael Brader
Senior Software Engineer - Billing
Techops - Softdev
Internode http://www.internode.on.net/

________________________________________
From: Rajesh Saha [rajeshsaha...@gmail.com]
Sent: Wednesday, 16 May 2012 6:39 PM
To: beginners@perl.org
Subject: Getopt::Long

Hi,

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 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 n regards
Rajesh Saha

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to