Using GetOptions twice in the script

M

Moti Shtrobach

Hi

I tried to split the arguments manipulation to two parts.
It looks like that:


&getA;
&getB;

sub getA
{
GetOptions ( "a1" => \$a1,
"a2"=> \$a2,
"a3"=> \$a3
);
}

sub getB
{
GetOptions ( "b1" => \$b1,
"b2"=> \$b2,
"b3"=> \$b3
);
}


It looks like only the first "GetOptions" command processed (getA) since I
get "Unknown option" message for all arguments in second subroutine (getB).

Is there any way to make both subroutines get the options ?
Do I have to use the "GetOptions" command only once?

Thanks a lot
Moti
 
B

Ben Morrow

Moti Shtrobach said:
I tried to split the arguments manipulation to two parts.
It looks like that:

&getA;
&getB;

Don't call subs like this. Use 'getA();'.
sub getA
{
GetOptions ( "a1" => \$a1,
"a2"=> \$a2,
"a3"=> \$a3
);
}

sub getB
{
GetOptions ( "b1" => \$b1,
"b2"=> \$b2,
"b3"=> \$b3
);
}


It looks like only the first "GetOptions" command processed (getA) since I
get "Unknown option" message for all arguments in second subroutine (getB).

Is there any way to make both subroutines get the options ?
Do I have to use the "GetOptions" command only once?

Yes, you may only use it once.

Ben
 
M

Moti Shtrobach

Thanks...

Ben Morrow said:
Don't call subs like this. Use 'getA();'.


Yes, you may only use it once.

Ben

--
Like all men in Babylon I have been a proconsul; like all, a slave ... During
one lunar year, I have been declared invisible; I shrieked and was not heard,
I stole my bread and was not decapitated.
~ (e-mail address removed) ~ Jorge Luis Borges, 'The Babylon
Lottery'
 
D

Darin McBride

Moti said:
Is there any way to make both subroutines get the options ?
Do I have to use the "GetOptions" command only once?

What I've done is to create a whole new module around Getoptions::Long
(although it could work with ::Std, too) where I can register all my
options from all my modules during initialisation, then call my module
to say parse_args(), and all my options are in the one module's
namespace. I can also register online help for each option, and
validation coderefs.

It's not really pretty, but it works.
 
G

Greg Bacon

: I tried to split the arguments manipulation to two parts.
:
: [snip code]
:
: It looks like only the first "GetOptions" command processed (getA)
: since I get "Unknown option" message for all arguments in second
: subroutine (getB).
:
: Is there any way to make both subroutines get the options ?
: Do I have to use the "GetOptions" command only once?

Making the code a little more chatty shows what's going on:

#! /usr/local/bin/perl

use warnings;
use strict;

use Getopt::Long;

$Getopt::Long::debug = 1;

&getA;
&getB;

sub getA {
my($a1,$a2,$a3);

my $result = GetOptions (
"a1" => \$a1,
"a2" => \$a2,
"a3" => \$a3,
);

if ($result) {
$_ = defined $_ ? $_ : "<undef>" for ($a1, $a2, $a3);

print "\$a1 = $a1\n",
"\$a2 = $a2\n",
"\$a3 = $a3\n";
}
else {
print "getA: no dice\n";
}
}

sub getB {
my($b1,$b2,$b3);

my $result = GetOptions (
"b1" => \$b1,
"b2" => \$b2,
"b3" => \$b3,
);

if ($result) {
$_ = defined $_ ? $_ : "<undef>" for ($b1, $b2, $b3);

print "\$b1 = $b1\n",
"\$b2 = $b2\n",
"\$b3 = $b3\n";
}
else {
print "getB: no dice\n";
}
}

Watch what happens:

$ ./try --b3
Getopt::Long 2.34 ($Revision: 2.68 $) called from package "main".
ARGV: (--b3)
autoabbrev=1,bundling=0,getopt_compat=1,gnu_compat=0,order=1,
ignorecase=1,requested_version=0,passthrough=0,genprefix="(--|-|\+)".
=> link "a1" to SCALAR(0x101d728c)
=> link "a2" to SCALAR(0x101d7268)
=> link "a3" to SCALAR(0x101d72a4)
=> $opctl{a1} = ARRAY(0x101ebb88) ["","a1",M,$,"<undef>"]
$opctl{a2} = ARRAY(0x1014c900) ["","a2",M,$,"<undef>"]
$opctl{a3} = ARRAY(0x1014c924) ["","a3",M,$,"<undef>"]
=> arg "--b3"
=> find "--b3"
=> split "--"+"b3"
=> 0 hits () with "b3" out of 3
Unknown option: b3
getA: no dice
Getopt::Long 2.34 ($Revision: 2.68 $) called from package "main".
ARGV: ()
autoabbrev=1,bundling=0,getopt_compat=1,gnu_compat=0,order=1,
ignorecase=1,requested_version=0,passthrough=0,genprefix="(--|-|\+)".
=> link "b1" to SCALAR(0x101eba2c)
=> link "b2" to SCALAR(0x101eba44)
=> link "b3" to SCALAR(0x101eba5c)
=> $opctl{b1} = ARRAY(0x1014d474) ["","b1",M,$,"<undef>"]
$opctl{b3} = ARRAY(0x1014c924) ["","b3",M,$,"<undef>"]
$opctl{b2} = ARRAY(0x10149948) ["","b2",M,$,"<undef>"]
$b1 = <undef>
$b2 = <undef>
$b3 = <undef>

As you can see, the first call to Getoptions empties @ARGV. What
benefit would you see in processing options in separate places?

Hope this helps,
Greg
 
A

Andrew Burgess

From perldoc Getopt::Long, you could use:

use Getopt::Long qw:)config pass_through);

to ignore unknown arguments and leave them in @ARGV. Alternatively you
can switch this feature on and off using calls to Configure().

The following seems to do what you want:

#!/usr/bin/perl

use strict;
use warnings;

use Getopt::Long qw:)config pass_through);

our ($a1, $a2, $a3, $b1, $b2, $b3);

getA();
getB();

sub getA {
print "Parsing options in part A\n";
GetOptions ( "a1" => \$a1,
"a2"=> \$a2,
"a3"=> \$a3);

print "Got a1\n" if ($a1);
print "Got a2\n" if ($a2);
print "Got a3\n" if ($a3);

}

sub getB {
print "Parsing options in part B\n";
GetOptions ( "b1" => \$b1,
"b2"=> \$b2,
"b3"=> \$b3);

print "Got b1\n" if ($b1);
print "Got b2\n" if ($b2);
print "Got b3\n" if ($b3);
}

HTH

Andrew
 
J

Johan Vromans

Moti Shtrobach said:
It looks like only the first "GetOptions" command processed (getA) since I
get "Unknown option" message for all arguments in second subroutine (getB).
Is there any way to make both subroutines get the options ?

Yes. Copy @ARGV.

@saveargv = @ARGV;
&getA;
@ARGV = @saveargv;
&getB;

Alternatively, use the "pass_through" config option. This will have
the first call leave all unrecognized options in @ARGV, so the second
call can process them.

Happy hacking,

-- Johan
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,901
Latest member
Noble71S45

Latest Threads

Top