Path: csiph.com!x330-a1.tempe.blueboxinc.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!border3.nntp.dca.giganews.com!border1.nntp.dca.giganews.com!nntp.giganews.com!news.alt.net!news.astraweb.com!border6.newsrouter.astraweb.com!not-for-mail From: Ben Finney Newsgroups: comp.lang.python Subject: Parse config file and command-line arguments, to get a single collection of options X-Public-Key-ID: 0xAC128405 X-Public-Key-Fingerprint: 517C F14B B2F3 98B0 CB35 4855 B8B2 4C06 AC12 8405 X-Public-Key-URL: http://www.benfinney.id.au/contact/bfinney-pubkey.asc X-Post-From: Ben Finney Date: Thu, 26 May 2011 14:38:31 +1000 Message-ID: <87k4deaxfc.fsf@benfinney.id.au> User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/23.3 (gnu/linux) Cancel-Lock: sha1:mOghOn1cJ1VCrjjaguZwnmJ0wAo= MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit Lines: 33 Organization: Unlimited download news at news.astraweb.com NNTP-Posting-Host: e35d83b7.news.astraweb.com X-Trace: DXC=@lFQ7FE;CD44K?0c7HD\@7L?0kYOcDh@:N7:H2`MmAU3S2kSQZDV^?kW3bEW9A[5UK?5NZ[SL`C\Kg3<]OBQ:]oZS4 Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:6294 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 .) -- \ “Apologize, v. To lay the foundation for a future offense.” | `\ —Ambrose Bierce, _The Devil's Dictionary_, 1906 | _o__) | Ben Finney