Mercurial > hg > config
view python/optparse_undefined.py @ 694:ebca6d85213a
File "/usr/lib/python3/dist-packages/IPython/config/__init__.py", line 16, in <module>
from .application import *
File "/usr/lib/python3/dist-packages/IPython/config/application.py", line 31, in <module>
from IPython.config.configurable import SingletonConfigurable
File "/usr/lib/python3/dist-packages/IPython/config/configurable.py", line 33, in <module>
from IPython.utils.text import indent, wrap_paragraphs
File "/usr/lib/python3/dist-packages/IPython/utils/text.py", line 28, in <module>
from IPython.external.path import path
File "/usr/lib/python3/dist-packages/IPython/external/path/__init__.py", line 2, in <module>
from path import *
File "/home/jhammel/python/path.py", line 25
print root(path)
^
author | Jeff Hammel <k0scist@gmail.com> |
---|---|
date | Wed, 09 Jul 2014 16:26:49 -0700 |
parents | 3efe894f9d79 |
children |
line wrap: on
line source
#!/usr/bin/env python """ illustrates usage of the undefined pattern from e.g. http://k0s.org/mozilla/hg/bzconsole/file/tip/bzconsole/command.py This is useful for scenarios similar to: - you have base configuration in a file - you have an OptionParser to read options in the command line - the CLI options should overwrite the file configuration iff the user specifies an option """ from optparse import OptionParser class Undefined(object): def __init__(self, default): self.default=default class UndefinedOptionParser(OptionParser): def add_option(self, *args, **kwargs): kwargs['default'] = Undefined(kwargs.get('default')) OptionParser.add_option(self, *args, **kwargs) def parse_args(self, *args, **kwargs): options, args = OptionParser.parse_args(self, *args, **kwargs) return options, args if __name__ == '__main__': myparser = UndefinedOptionParser() myparser.add_option("--foo", dest='foo', default='hello') myparser.add_option("--bar", dest='bar', default='goodbye') myparser.add_option("--baz", dest='baz', default='helloagain') myparser.add_option("--fleem", dest='fleem', default='aufwiedersehen') myconfiguration = {'foo': 'hi', 'bar': 'ciao'} options, args = myparser.parse_args(['--foo', 'hello', '--baz', 'hola']) for key, value in options.__dict__.items(): # XXX ideally you would move this to parse_args if isinstance(value, Undefined): options.__dict__[key] = myconfiguration.get(key, value.default) assert options.foo == 'hello' assert options.bar == 'ciao' assert options.baz == 'hola' assert options.fleem == 'aufwiedersehen'