Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Comment on

( #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":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

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

    How do I use this? | Other CB clients
    Other Users?
    Others pondering the Monastery: (10)
    As of 2014-11-20 22:44 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      My preferred Perl binaries come from:














      Results (102 votes), past polls