Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

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?


In reply to CLI Args and Config Files: Best Practices? by temporal

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



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (3)
As of 2024-04-24 23:22 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found