AtomicPorkchop
AtomicPorkchop

Reputation: 2675

How can I pass Getopt::Long options to a subroutine that's also an option?

I am trying to setup Getopt::Long to handle the arguments from a configuration script.

Here is my starter:

#!/usr/bin/perl
use strict;
use warnings;
use Getopt::Long;

my $config_file = '';

GetOptions (    
    'config|c=s' => \$config_file,
    'add|a' => \&add_server,
    'del|d' => \&del_server,        
);

sub add_server {    
    print "$config_file\n";    
}

sub del_server {    
    # Left blank for now.    
}

The odd thing is I am running into a problem when I run my script with something like this:

./config.pl -a -c config.xml

It does NOT print the -c option, but if I run it like this,

./config.pl -c config.xml -a

it works like it should.

I think I understand the reason why; it has to do with the order execution right?

How can I fix it? Should I use Getopt::Long in conjunction with @ARGV?

Ultimately, I am trying to make the command line args pass into the subroutine that I am calling. So if -a or --add, I want the options of -c or --config to pass into the subroutine when it is called.

Any ideas?

Upvotes: 3

Views: 3040

Answers (5)

hugg
hugg

Reputation: 1

GetOptions(
        'arg=s' => sub { print "$_[1]\n"; },
);

Upvotes: -2

ikegami
ikegami

Reputation: 385657

The callbacks are called as the options are encountered, so add_server is being called before -c has been encountered when you do

./config.pl -a -c config.xml

Based on the latest info, you now want:

use Getopt::Long qw( GetOptions );

GetOptions(
   'a=s' => \my $opt_a,
   'd=s' => \my $opt_d,
   'h=s' => \my $opt_h,
   'p=s' => \my $opt_p,
) or usage();

Upvotes: 0

user554546
user554546

Reputation:

Boiling the example down a little bit...

use strict;
use warnings;
use Getopt::Long;

my $config_file = '';

GetOptions (

    'config|c=s' => \$config_file,
    'add|a' => sub{add_server($config_file);}
);

sub add_server
{

    my $config=shift;

    if(defined($config))
    {
        print "Got this for a config file: $config\n";
    }
    else
    {
        print "No argument supplied to add_server\n";
    }

}

... and running config.pl -c blurg -a returns the output Got this for a config file: blurg, and running config.pl -a -c blurg returns Got this for a config file:.

So, what I suspect is happening is that the options are assigned in the order given. So in the first case $config_file is assigned to the -c argument and then the add_server subroutine is called (with the correct argument), whereas in the second case, add_server is immediately fired off with no argument and then $config_file is assigned.

All this aside, I'd recommend making -a a boolean and do whatever you want to do if it's enabled (and if an argument for -c is supplied).

Upvotes: 0

Christopher Neylan
Christopher Neylan

Reputation: 8272

Enable the pass_through option on Getopt::Long so that it will ignore unknown options, then call GetOptions once for your options, disable it again, and then use GetOptions again for your command.

Upvotes: -1

toolic
toolic

Reputation: 62037

I don't see the need to call the subroutine directly from the GetOptions call. Control the order like this:

use strict;
use warnings;
use Getopt::Long;

my %opts = (config => '');

GetOptions(\%opts, qw(
   config|c=s
   add|a
   del|d
));

add_server() if $opts{add};
del_server() if $opts{del};

sub add_server {    
    print "$opts{config}\n";
}

sub del_server {}

Upvotes: 4

Related Questions