|XP is just a number|
CLI Args and Config Files: Best Practices?by temporal (Pilgrim)
|on Jan 31, 2013 at 22:02 UTC||Need Help??|
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:
Okay, high time for a code example:
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:
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?