http://www.perlmonks.org?node_id=1016403

temporal has asked for the wisdom of the Perl Monks concerning the following question:

First off, sorry about the double post - just one of those days when I want The Wisdom.

I'm writing a little daemon that I'd like to take arguments from both CLI and a config file. Currently, CLI arguments override config file settings and that's that - I figure if the user specifically requests a value set in the command line they're serious about it.

However, now I'd like to refresh the configs each time the daemon runs, just in case something in the config file has changed. Now I run into a problem - should these changes overwrite CLI arguments originally provided? I'd rather they not. But I'd like to set any of those other configs that have may have changed.

So what has arisen organically is that I was using Getopt::Long to handle CLI arguments, then added config files later on using Config::Simple. Okay, that works. But I'd really like to bring all my argument/options handling under one process - easier to track and maintain, for sure. It's awfully ugly juggling configuration variables between the two parsing utilities.

Enter AppConfig. Yay, it parses config files - even recognizes inline comments (Config::Simple does not) and is more sophisticated in lots of other ways to boot. It also does CLI args, even providing the option of just using Getopt::Long. All in one module, stored in the same spot with lots of utility built in.

I think this module (AppConfig) is pretty popular for building scripts and such that have more complex configuration requirements. So here's a quick question (which hopefully won't start a war) - what do you use?

BOOOOM!... and while that war wages - on to my real question:
What would be the best practice within AppConfig for enforcing the case I described initially - that is, CLI args are loaded at script startup and preserved throughout execution despite reloading from the config file with each lifecycle of the daemon?

Okay, high time for a code example:

use strict; use AppConfig qw(:argcount); # my usual AppConfig initialization my $temp_conf = AppConfig->new( { CREATE => 1, GLOBAL => { ARGCOUNT => ARGCOUNT_ONE, DEFAULT => "<undef>",}, } ); # ->define() some default values and set any options for them in this +sub define_defaults(); # load in CLI args $temp_conf->args(); # parse conf file (path specified in CLI args, otherwise use default) $temp_conf->file( $temp_conf->config_file() );

So, hm. Now I've got my initial configuration. However, I'm not giving the CLI arguments precedence over config file settings. Problem being that I have to load them first to get the possible supplied config file path, which consumes the arguments. I suppose I could save the CLI args to an array and do something like this:

# save the CLI args my @CLI = @ARGV; # load in CLI args, gets those useful ones like path right off the bat $temp_conf->args(); # parse conf file (path specified in CLI args, otherwise use default) $temp_conf->file( $temp_conf->config_file() ); # reinforce those CLI args! $temp_conf->args(\@CLI);

Is there an AppConfig option for this or is this something I'll have to implement manually (like above)? Or maybe some enterprising monk has an entirely different approach?