perl - How to properly use a Getopt::Long to parse optional arguments? -


i use getopt::long command line options perl script. pass optional argument can if value specified, , else if option called, no value passed.

the script invoked this:

/root/perlscripts/pingm.pl --installdaemon 

for no argument specified, and:

--installdaemon=7.7.7.7 

for specifying optional argument.

then i'd this:

getopt::long::configure(qw(bundling no_getopt_compat)); getoptions ('installdaemon:s' => \$daeminstall) or die ("error in command line arguments\n"); 

the next step doubtful.

if do:

if ($daeminstall) {         print "i called!\n";         $installdaemon=1;     } 

then, if block never called if script called /root/perlscripts/pingm.pl --installdaemon, because according perldoc, optional argument take '' string if no value specified.

so how can check whether option specified without passing value?

check defined $daemsintall instead. if defined, corresponding option specified; can compare empty string see whether or not set value.

example (it uses getoptionsfromstring method, approach same):

use getopt::long(qw/getoptionsfromstring/); $tag; getoptionsfromstring('--tag', 'tag:s' => \$tag); if (defined $tag) {     if ($tag eq '') {         $tag = '7.7.7.7';     }     print $tag; } else {     print 'no option supplied'; } 

and here's ideone demo.


Comments

Popular posts from this blog

Detect support for Shoutcast ICY MP3 without navigator.userAgent in Firefox? -

web - SVG not rendering properly in Firefox -

java - JavaFX 2 slider labelFormatter not being used -