annotate example/optparse_undefined.py @ 145:a95e7f218bd2 default tip

python3.5
author Jeff Hammel <k0scist@gmail.com>
date Sun, 19 Feb 2017 20:42:57 -0800
parents cae2a1e1b9da
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
138
cae2a1e1b9da from config
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
1 #!/usr/bin/env python
cae2a1e1b9da from config
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
2
cae2a1e1b9da from config
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
3 """
cae2a1e1b9da from config
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
4 illustrates usage of the undefined pattern from e.g.
cae2a1e1b9da from config
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
5 http://k0s.org/mozilla/hg/bzconsole/file/tip/bzconsole/command.py
cae2a1e1b9da from config
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
6
cae2a1e1b9da from config
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
7 This is useful for scenarios similar to:
cae2a1e1b9da from config
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
8 - you have base configuration in a file
cae2a1e1b9da from config
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
9 - you have an OptionParser to read options in the command line
cae2a1e1b9da from config
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
10 - the CLI options should overwrite the file configuration iff
cae2a1e1b9da from config
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
11 the user specifies an option
cae2a1e1b9da from config
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
12 """
cae2a1e1b9da from config
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
13
cae2a1e1b9da from config
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
14 from optparse import OptionParser
cae2a1e1b9da from config
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
15
cae2a1e1b9da from config
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
16 class Undefined(object):
cae2a1e1b9da from config
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
17 def __init__(self, default):
cae2a1e1b9da from config
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
18 self.default=default
cae2a1e1b9da from config
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
19
cae2a1e1b9da from config
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
20 class UndefinedOptionParser(OptionParser):
cae2a1e1b9da from config
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
21
cae2a1e1b9da from config
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
22 def add_option(self, *args, **kwargs):
cae2a1e1b9da from config
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
23 kwargs['default'] = Undefined(kwargs.get('default'))
cae2a1e1b9da from config
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
24 OptionParser.add_option(self, *args, **kwargs)
cae2a1e1b9da from config
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
25
cae2a1e1b9da from config
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
26 def parse_args(self, *args, **kwargs):
cae2a1e1b9da from config
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
27 options, args = OptionParser.parse_args(self, *args, **kwargs)
cae2a1e1b9da from config
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
28 return options, args
cae2a1e1b9da from config
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
29
cae2a1e1b9da from config
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
30 if __name__ == '__main__':
cae2a1e1b9da from config
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
31
cae2a1e1b9da from config
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
32 myparser = UndefinedOptionParser()
cae2a1e1b9da from config
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
33 myparser.add_option("--foo", dest='foo', default='hello')
cae2a1e1b9da from config
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
34 myparser.add_option("--bar", dest='bar', default='goodbye')
cae2a1e1b9da from config
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
35 myparser.add_option("--baz", dest='baz', default='helloagain')
cae2a1e1b9da from config
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
36 myparser.add_option("--fleem", dest='fleem', default='aufwiedersehen')
cae2a1e1b9da from config
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
37
cae2a1e1b9da from config
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
38 myconfiguration = {'foo': 'hi',
cae2a1e1b9da from config
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
39 'bar': 'ciao'}
cae2a1e1b9da from config
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
40 options, args = myparser.parse_args(['--foo', 'hello', '--baz', 'hola'])
cae2a1e1b9da from config
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
41 for key, value in options.__dict__.items():
cae2a1e1b9da from config
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
42 # XXX ideally you would move this to parse_args
cae2a1e1b9da from config
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
43 if isinstance(value, Undefined):
cae2a1e1b9da from config
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
44 options.__dict__[key] = myconfiguration.get(key, value.default)
cae2a1e1b9da from config
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
45
cae2a1e1b9da from config
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
46 assert options.foo == 'hello'
cae2a1e1b9da from config
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
47 assert options.bar == 'ciao'
cae2a1e1b9da from config
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
48 assert options.baz == 'hola'
cae2a1e1b9da from config
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
49 assert options.fleem == 'aufwiedersehen'