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