bliako has asked for the wisdom of the Perl Monks concerning the following question:
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
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: parsing @ARGV with Getopt::Long multiple times
by Corion (Patriarch) on Apr 01, 2020 at 12:14 UTC | |
by bliako (Abbot) on Apr 01, 2020 at 12:28 UTC |