in reply to getopt::long treating option as an argument

Hello harpreetsammi, and welcome to the Monastery!

You can use a subroutine to validate the argument. For example:

#! perl use strict; use warnings; use Getopt::Long; my ($name, $long); GetOptions ( 'name=s' => \&name_handler, long => \$long, ) or die "Error in command line arguments\n"; printf "Name is '%s', long is %s\n", $name, $long ? 'set' : 'not set'; sub name_handler { my (undef, $n) = @_; if ($n =~ /^--/) { die "Option 'name' requires an argument\n"; } else { $name = $n; } }

Output:

2:11 >perl 1098_SoPW.pl --name Fred --long Name is 'Fred', long is set 2:11 >perl 1098_SoPW.pl --name --long Option 'name' requires an argument Error in command line arguments 2:11 >

See Getopt::Long#User-defined-subroutines-to-handle-options.

Hope that helps,

Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

Replies are listed 'Best First'.
Re^2: Getopt::Long treating option as an argument
by pme (Monsignor) on Dec 16, 2014 at 16:46 UTC
    You can insert this line after GetOptions() to set up the default value if neccessary.
    $name ||= 'default_name';