0

I want to log options and their arguments from user command after running the script.

Consider this command:

./test.pl --ip localhost --id 400154 --class firstgrade 

...and many other options and values. My desired output would be like this(by using log4perl):

debug - ip=>localhost id=>400154 class=>firstgrade 

I do:

use Getopt::Long; 
my $ip;
my $id; 
my $class;
my %h =('ip' => \$ip,
        'id' => \$id,
    'class' => \$class);
GetOptions(\%h);
$logger->debug(join('=>',%h));

but it doesn't work. Please help.

Matt Ston
  • 11
  • 2

4 Answers4

4

Your code is weird combination of two distinct features of Getopt::Long - it can either parse options into hash or fill individual options into variables. It is even possible to put part into hash and rest into variables.

This should work:

use Getopt::Long;

my @options = qw(ip id class);
my %h = ();
GetOptions(\%h,
    map { "$_:s" } @options
) or die "Could not parse";
warn map { "$_=>$h{$_} " } keys %h;

This is variant where parsed options are put into the hash. Note :s after each option to indicate that it takes an argument.

Edit: updated the answer per clarification below.

bvr
  • 9,687
  • 22
  • 28
  • thanks for reply. but i think i explain the problem in wrong way. I defined %hash, because I have more than 50 options in each command, these options are static, I should be able to get them with their values (e.g --ip localhost) from command. this is my problem. If Im not clear please let me know to explain more. – Matt Ston Feb 09 '11 at 12:43
  • @Matt Ston: I updated the answer. Right now the list of options is generated from @options array. This way you can pass arbitrary number of expected options. – bvr Feb 09 '11 at 13:23
  • According to your code, how i can print @options element i mean option and its related argument just like the one you warn map in hash format? – Matt Ston Feb 09 '11 at 13:29
  • 1
    I don't quite follow here. You can certainly warn/print directly the `@options` array, but it alone is just list of options available. Parsed options are filled into `%h` hash. – bvr Feb 09 '11 at 13:45
1

Try this:

my $ip = ""; my $id = ""; my $class= "";
GetOptions('ip=s' => \$ip, 'id=s' => \$id, 'class=s' => \$class);
print "debug - ip=>$ip id=>$id, class=>$class";

And you should probably call it like this:

./test.pl --ip localhost --id 400154 --class firstgrade
erickb
  • 6,193
  • 4
  • 24
  • 19
1

The following code demonstrates two ways to achieve what you want.

The 'home grown' method uses map and join to generate the options list. (The grep eliminates undef options. You can remove the grep {} part.)

The Data::Dumper method may be desirable because it's eval-able.


#!/usr/bin/env perl

use strict;
use warnings;

use Getopt::Long qw(:config gnu_getopt);
use Data::Dumper;
$Data::Dumper::Sortkeys = 1;
$Data::Dumper::Indent = 0;
$Data::Dumper::Terse = 1;

my %opts = (
  dir => undef,
  verbose => 0,
  silent => 0,
 );

GetOptions(\%opts,
           'dir|d=s',
           'verbose|v+',
           'silent+',
          )
  or die("Usage: blah\n");

# also see Getopt::Long perldoc for pod2usage

print( "home grown:\n",
       join(" ", map { sprintf('%s=>%s',$_,$opts{$_}||'undef') } 
              grep {defined $opts{$_}} keys %opts ),
       "\n" );

print( "Dumper:\n",
       Dumper(\%opts), 
       "\n" );

Example:

apt12j$ ~/tmp/x.pl  -vv --silent
home grown:
verbose=>2 silent=>1
Dumper:
{'dir' => undef,'silent' => 1,'verbose' => 2}
Reece
  • 7,616
  • 4
  • 30
  • 46
  • Thank you so much, you cant even imagine how happy I am now :) – Matt Ston Feb 13 '11 at 10:44
  • For the last question: how I can get the argument inside an other method, I have a method that send these arguments via mail function. something like this: `my $TicketID = $TicketObject->TicketCreate( # I want to get the id, ip and class value, those user enter in command id => '?', class => '?', ip => '?', );` – Matt Ston Feb 13 '11 at 10:58
  • It is not hard :D I got the answer. problem solved thanks again :) – Matt Ston Feb 13 '11 at 12:21
  • You're welcome. I don't often get to make people happy with Perl! – Reece Feb 17 '11 at 22:48
0

Checkout MooseX::Getopt, it'll help you two-fold:

  1. get you into modern OO perl

  2. create super simple command line apps.

Checkout MooseX::App::Cmd. It'll help you separate your logic out as well. Or App::Cmd if you don't want to drink the Moose kool-aid just yet.

dhoss
  • 160
  • 6