perl - Getting command line options and their values -
i want log options , arguments user command after running script.
consider command:
./test.pl --ip localhost --id 400154 --class firstgrade
...and many other options , values. desired output this(by using log4perl):
debug - ip=>localhost id=>400154 class=>firstgrade
i do:
use getopt::long; $ip; $id; $class; %h =('ip' => \$ip, 'id' => \$id, 'class' => \$class); getoptions(\%h); $logger->debug(join('=>',%h));
but doesn't work. please help.
your code weird combination of 2 distinct features of getopt::long
- can either parse options hash or fill individual options variables. possible put part hash , rest variables.
this should work:
use getopt::long; @options = qw(ip id class); %h = (); getoptions(\%h, map { "$_:s" } @options ) or die "could not parse"; warn map { "$_=>$h{$_} " } keys %h;
this variant parsed options put hash. note :s
after each option indicate takes argument.
edit: updated answer per clarification below.
Comments
Post a Comment