[Python-Dev] PEP 389: argparse - new command line parsing module (original) (raw)
m h sesquile at gmail.com
Mon Sep 28 17:23:22 CEST 2009
- Previous message: [Python-Dev] PEP 389: argparse - new command line parsing module
- Next message: [Python-Dev] PEP 389: argparse - new command line parsing module
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
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:
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.parse_args ... opt_name - string of opt name you want to access ... """ ... # get from cmd line ... value = Unset() ... if opt and opt_name: ... try: ... value = opt.getattr(opt_name) ... except AttributeError, e: ... pass ... if not isinstance(value, Unset): ... return value ... # get from ENV ... if env_name: ... try: ... value = os.environ[env_name] ... except KeyError, e: ... pass ... if not isinstance(value, Unset): ... return value ... # get from config file ... if cfg and cfg_section and cfg_name: ... try: ... value = cfg.get(cfg_section, cfg_name) ... 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
env_name
, config_key
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
- Previous message: [Python-Dev] PEP 389: argparse - new command line parsing module
- Next message: [Python-Dev] PEP 389: argparse - new command line parsing module
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]