[Python-Dev] PEP 389: argparse - new command line parsing module (original) (raw)

Michael Foord fuzzyman at voidspace.org.uk
Mon Sep 28 17:26:52 CEST 2009


m h wrote:

Perhaps this is OT, but since command line parsing is part of configuration, I figure I'd throw it out there. My scripts often have configuration that the command line can override and I loosely follow the example hierarchy[0] listed in The Art of Unix Programming.

Some configuration I want in a config file (but I want to override from the command line) and sometimes it's very nice to use environment variables for configuration. So I do something like this:

Integration with command line options is an occasionally-requested feature for ConfigObj. I've always said I would be open to patches...

In other words, yes I think there is demand for it. Whether it belongs immediately in the standard library is another matter, but if you wrote a layer that unified ConfigParser and argparse I think you will find that people use it.

It is well outside the realm of this PEP however.

All the best,

Michael Foord

Chaining Configuration ----------------------

Ugly code to cascade configuration

class Unset(object): pass def cascadevalue(opt=None, optname=None, envname=None, cfg=None, cfgsection=None, cfgname=None, default=None): ... """ ... opt - result of OptionParser.parseargs ... optname - string of opt name you want to access ... """ ... # get from cmd line ... value = Unset() ... if opt and optname: ... try: ... value = opt.getattr(optname) ... except AttributeError, e: ... pass ... if not isinstance(value, Unset): ... return value ... # get from ENV ... if envname: ... try: ... value = os.environ[envname] ... except KeyError, e: ... pass ... if not isinstance(value, Unset): ... return value ... # get from config file ... if cfg and cfgsection and cfgname: ... try: ... value = cfg.get(cfgsection, cfgname) ... except ConfigParser.NoOptionError, e: ... pass ... if not isinstance(value, Unset): ... return value ... return default

cascadevalue(opt=opt, optname='author', cfg=cfg, cfgsection='Properties', cfgname='author') 'Matt' Does anyone else have interest in such functionality? Is it outside the realm of this PEP? Ideally I'd like to have something like envname, configkey named parameters for options, then pass an optional list of configs files to the parser instance. Then rather than looking solely at command line options, argparse could also pull values from config files, then env and finally the command line. cheers, matt harrison 0 - http://www.faqs.org/docs/artu/ch10s02.html


Python-Dev mailing list Python-Dev at python.org http://mail.python.org/mailman/listinfo/python-dev Unsubscribe: http://mail.python.org/mailman/options/python-dev/fuzzyman%40voidspace.org.uk

-- http://www.ironpythoninaction.com/ http://www.voidspace.org.uk/blog



More information about the Python-Dev mailing list