staffanm / layeredconfig

Manages configuration coming from config files, environment variables, command line arguments, code defaults or other sources
BSD 3-Clause "New" or "Revised" License
18 stars 8 forks source link

[Question]: argparse compatibility? #1

Closed prologic closed 9 years ago

prologic commented 9 years ago

Is this compatible with argparse?

staffanm commented 9 years ago

Not directly -- since argparse has it's own handling of in particular default values and typing, I'm not sure how to best achieve a deeper integration, but I am considering this for the next version.

However, you can have them sort-of coexist by letting a Commandline source handle the cmd line arguments it can, and then sending the rest to argparse. Something like this should work:

cmdlinesrc = Commandline()
layeredcfg = LayeredConfig(cmdlinesrc) # will process long options eg --foo=bar, but not -f
parser = argparse.ArgumentParser()
# set up parser in some way
args = parser.parse_args(cmdlinesrc.rest) 
prologic commented 9 years ago

Hmm I tend to think it would be more useful to have a deeper integration with argparse rather than trying to hand-parse the command line arguments from sys.argv?

staffanm commented 9 years ago

Undoubtedly, for command-line driven applications. LayeredConfig's initial use case was an app that was primarily driven by a config file, and so the ability to use command line args was added solely to override config file settings on a particular invocation. But the command line parsing is very rudimentary compared to argparse.

Since argparse has it's own handling of defaults and typing, do you have any suggestions on what a deeper integration would look like?

prologic commented 9 years ago

I'll have a think about it. I like the idea of your proejct and would love to see this get some traction. I've done similar things with many of my projects; See:

staffanm commented 9 years ago

Great! Your examples were very helpful in getting a better idea on how argparse typically could be used with other sources of configurations, thanks!

prologic commented 9 years ago

Heh no problems. The whole reprconf stuff is borrowed from CherryPy but I've cobbled together this kind of layered config in many of my apps both for personal and work use in this way. A truly integrated solution "layredconfig" on PyPi would be a nice addition :)

staffanm commented 9 years ago

Hey, I took a stab at implementing deeper argparse integration in the form of an optional parameter to Commandline, which accepts a configured argparse.ArgumentParser object. It's not ready for release yet, but I've pushed it to a feature branch (https://github.com/staffanm/layeredconfig/tree/feature/argparse with new docs at http://layeredconfig.readthedocs.org/en/feature-argparse/argparse.html). If you have the time, I'd be very thankful if you would take a look at the docs in particular and see if they make sense.

prologic commented 9 years ago

Cool I'll take a look :)

staffanm commented 9 years ago

I just pushed 0.2.0 which includes this argparse integration, as well as some other config sources as well. Let me know if you find it useful!

prologic commented 9 years ago

Good work :)

James Mills / prologic

E: prologic@shortcircuit.net.au W: prologic.shortcircuit.net.au

On Sun, Nov 23, 2014 at 7:08 AM, Staffan Malmgren notifications@github.com wrote:

I just pushed 0.2.0 which includes this argparse integration, as well as some other config sources as well. Let me know if you find it useful!

— Reply to this email directly or view it on GitHub https://github.com/staffanm/layeredconfig/issues/1#issuecomment-64095536 .