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, |
In reply to Re: Getopt::Long treating option as an argument
by Athanasius
in thread getopt::long treating option as an argument
by harpreetsammi
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |