title: Parse config files, environment, and command-line arguments, to get a single collection of options
body: <p>Python's standard library has modules for <strong>configuration file parsing</strong> (<a href="http://docs.python.org/3/library/configparser.html">configparser</a>), <strong>environment variable reading</strong> (<a href="http://docs.python.org/3/library/os.html#os.environ">os.environ</a>), and <strong>command-line argument parsing</strong> (<a href="http://docs.python.org/3/library/argparse.html">argparse</a>). I want to write a program that does all those, and also:</p>
  
  <ul>
  <li><p>Has a <strong>cascade of option values</strong>:</p>
  
  <ul>
  <li>default option values, overridden by</li>
  <li>config file options, overridden by</li>
  <li>environment variables, overridden by</li>
  <li>command-line options.</li>
  </ul></li>
  <li><p>Allows one or more <strong>configuration file locations specified on the command line</strong> with e.g. <code>--config-file foo.conf</code>, and reads that (either instead of, or additional to, the usual configuration file). This must still obey the above cascade.</p></li>
  <li><p>Allows <strong>option definitions in a single place</strong> to determine the parsing behaviour for configuration files and the command line.</p></li>
  <li><p>Unifies the parsed options into a <strong>single collection of option values</strong> for the rest of the program to access without caring where they came from.</p></li>
  </ul>
  
  <p>Everything I need is apparently in the Python standard library, but they don't work together smoothly.</p>
  
  <p>How can I achieve this with minimum deviation from the Python standard library?</p>