I think I need to call Getopt::Long twice. The first time I take from user the name of the configuration file (say --config abc). Then I read the file, and create a hash of parameters.

Then, as a 2nd pass, I need to overwrite any of these parameters in the config with user-specified values from the command line for this particular run. The config hash is too nested, so I just can't pass it to Getopt (like it is mentioned in the doc).

And so I resort to this:

use Getopt::Long; my $configfile; my @ARGVcopy = @ARGV; # because 1st pass eats all options in @ARGV!! { local $SIG{__WARN__} = sub { }; # don't complain when see options re +levant to the 2nd pass Getopt::Long::GetOptions('--config=s', \$configfile); # no check! it + will fail } die "config needed" unless $configfile; my $confighash = parse_configfile($configfile); # 2nd pass with restored ARGV @ARGV = @ARGVcopy; Getopt::Long::GetOptions( 'config=s' => \my $dummy, #<<<< this belongs to 1st phase but is stil +l in cmdline (@ARGV) '--max-value=i' => \$confighash->{'a'}->{'b'}->{'max-value'}, ) or die "error cmd line params";

Is there a more elegant way? I mean 2 passes, and then the $dummy? Modifying @ARGV between 1st pass and 2nd pass is not acceptable as a new can of worms will be opened (edit: or is it a question of 2 shifts?).

bw, bliako


In reply to parsing @ARGV with Getopt::Long multiple times by bliako

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.