in reply to Re: command line args - a chicken and egg problem
in thread command line args - a chicken and egg problem
I have run into this several times too, but I don't remember what I ended up with. Probably something different and slightly wrong each time. I guess you could do something like:
which might be better spelledmy %opts = ( 'config' => 'default.config', 'other1' => 'default1', 'other2' => 'default2' ); %opts = (%opts, process_command_line()); %opts = (read_config_file($opts{config}), %opts);
I wouldn't use either, because I always use Getopt::Long. So perhaps:my %defaults = ( 'other1' => 'default1', 'other2' => 'default2' ); my %from_cmdline = process_command_line(); my $config = $from_cmdline{config} || "default.config"; my %from_config = read_config_file($config); my %opts = (%defaults, %from_config, %from_cmdline);
which doesn't feel very satisfying. Maybe it would be better to use Zaxo's rule 2b and fold them together?my %defaults = ( param1 => 'default1', param2 => 'default2' ); my $config = 'default.config'; GetOptions("config|c=s" => \$config, "param1=s" => \$opt{param1}, "param2=s" => \$opt{param2}); %defaults = (%defaults, read_config_file($config)); while (my ($param, $value) = each %defaults) { $opt{$param} = $value unless defined $opt{$param}; }
Feels about right.my %opts = ( param1 => 'default1', param2 => 'default2' ); my $use_default_config = 1; GetOptions("config|c=s" => sub { my ($param, $value) = @_; $use_default_config = 0; read_config_file(\%opt, $value); }, "param1=s" => \$opt{param1}, "param2=s" => \$opt{param2}); read_config_file(\%opt, 'default.config') if $use_default_config;
Update: I just read through the POD for AppConfig. I agree; it sounds really nice! But it looks like it doesn't expose enough to implement what you want. Seems like a good opportunity to submit a patch.
|
|---|