Using GetOptions twice in the script

Discussion in 'Perl Misc' started by Moti Shtrobach, Nov 25, 2003.

  1. 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
     
    Moti Shtrobach, Nov 25, 2003
    #1
    1. Advertising

  2. Moti Shtrobach

    Ben Morrow Guest

    "Moti Shtrobach" <> wrote:
    > 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

    --
    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.
    ~ ~ Jorge Luis Borges, 'The Babylon Lottery'
     
    Ben Morrow, Nov 25, 2003
    #2
    1. Advertising

  3. Thanks...

    "Ben Morrow" <> wrote in message
    news:bq03bo$bgf$...
    >
    > "Moti Shtrobach" <> wrote:
    > > 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
    >
    > --
    > 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.
    > ~ ~ Jorge Luis Borges, 'The Babylon

    Lottery'
     
    Moti Shtrobach, Nov 25, 2003
    #3
  4. Moti Shtrobach wrote:

    > 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.
     
    Darin McBride, Nov 25, 2003
    #4
  5. Moti Shtrobach

    Greg Bacon Guest

    In article <3fc38c09$1@news.012.net.il>,
    Moti Shtrobach <> wrote:

    : 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
    --
    Socialism in its contemporary watered down form is little more than
    envy disguised as principle.
    -- Martin Pot
     
    Greg Bacon, Nov 25, 2003
    #5
  6. 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
     
    Andrew Burgess, Nov 26, 2003
    #6
  7. "Moti Shtrobach" <> writes:

    > 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
     
    Johan Vromans, Dec 3, 2003
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Kiuhnm

    twice(twice(x))

    Kiuhnm, Apr 1, 2006, in forum: C++
    Replies:
    2
    Views:
    430
    Kiuhnm
    Apr 1, 2006
  2. perl_newbie
    Replies:
    7
    Views:
    205
    A. Sinan Unur
    Jun 2, 2005
  3. GetOptions::Long question

    , Jun 1, 2005, in forum: Perl Misc
    Replies:
    0
    Views:
    105
  4. a
    Replies:
    1
    Views:
    261
    -berlin.de
    Feb 21, 2007
  5. Tom Gur

    Errormsgs on GetOptions

    Tom Gur, Dec 17, 2007, in forum: Perl Misc
    Replies:
    1
    Views:
    95
    Paul Lalli
    Dec 17, 2007
Loading...

Share This Page