Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #6299
| From | Raymond Hettinger <python@rcn.com> |
|---|---|
| Newsgroups | comp.lang.python |
| Subject | Re: Parse config file and command-line arguments, to get a single collection of options |
| Date | 2011-05-25 22:15 -0700 |
| Organization | http://groups.google.com |
| Message-ID | <5872e7ac-de37-438f-a2da-147ff466f8f1@z15g2000prn.googlegroups.com> (permalink) |
| References | <87k4deaxfc.fsf@benfinney.id.au> |
On May 25, 9:38 pm, Ben Finney <b...@benfinney.id.au> wrote: > Howdy all, > > Python's standard library has modules for configuration file parsing > (configparser) and command-line argument parsing (optparse, argparse). I > want to write a program that does both, but also: > > * Has a cascade of options: default option values, overridden by config > file options, overridden by command-line options. > > * Reads a different, or even additional, configuration file if specified > on the command-line (e.g. --config-file foo.conf) and yet still obeys > the above cascade. > > * Allows a single definition of an option (e.g. logging level) to define > the same option for parsing from configuration files and the command > line. > > * Unifies the parsed options into a single collection for the rest of > the program to access without caring where they came from. > > How can I achieve this with minimum deviation from the Python standard > library? One thought is start with something like ChainMap, http://code.activestate.com/recipes/305268-chained-map-lookups/?in=user-178123 , or some variant to unify multiple mapping objects into a single prioritized collection. A mapping for command line args can be made by using vars() on an argparse namespace to create a dictionary. ConfigParser's mapping is accessible via its get() method. With a ChainMap style object you can add other option sources such as os.environ. This should get you started on your grand unified, do- everything-at-once vision with minimal deviation from the standard library. Raymond
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Parse config file and command-line arguments, to get a single collection of options Ben Finney <ben@benfinney.id.au> - 2011-05-26 14:38 +1000 Re: Parse config file and command-line arguments, to get a single collection of options Raymond Hettinger <python@rcn.com> - 2011-05-25 22:15 -0700 Re: Parse config file and command-line arguments, to get a single collection of options rzed <rzantow@gmail.com> - 2011-05-28 23:12 +0000
csiph-web