Parse config file and command-line arguments, to get a single collection of options

Ben Finney ben at benfinney.id.au
Thu May 26 00:38:31 EDT 2011


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?


(For anyone interested in gaining StackOverflow points, I'm also asking
this as a question there so feel free to post answers on that site
<URL:http://stackoverflow.com/questions/6133517/parse-config-file-and-command-line-arguments-to-get-a-single-collection-of-optio>.)

-- 
 \         “Apologize, v. To lay the foundation for a future offense.” |
  `\                   —Ambrose Bierce, _The Devil's Dictionary_, 1906 |
_o__)                                                                  |
Ben Finney



More information about the Python-list mailing list