Mercurial > hg > ConfigOptionParser
annotate example.py @ 0:3081763b099b
initial commit of ConfigOptionParser
author | Jeff Hammel <jhammel@mozilla.com> |
---|---|
date | Thu, 20 May 2010 08:47:35 -0700 |
parents | |
children |
rev | line source |
---|---|
0
3081763b099b
initial commit of ConfigOptionParser
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
1 #!/usr/bin/env python |
3081763b099b
initial commit of ConfigOptionParser
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
2 |
3081763b099b
initial commit of ConfigOptionParser
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
3 """ |
3081763b099b
initial commit of ConfigOptionParser
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
4 Example script to illustrate ConfigOptionParser. |
3081763b099b
initial commit of ConfigOptionParser
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
5 Run from the command line and give some arguments to see how this works. |
3081763b099b
initial commit of ConfigOptionParser
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
6 |
3081763b099b
initial commit of ConfigOptionParser
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
7 Read values from .ini file: |
3081763b099b
initial commit of ConfigOptionParser
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
8 python example.py -c example.ini |
3081763b099b
initial commit of ConfigOptionParser
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
9 |
3081763b099b
initial commit of ConfigOptionParser
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
10 Read values from .ini file but override the setting for the foo |
3081763b099b
initial commit of ConfigOptionParser
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
11 python example.py -c example.ini -f blah |
3081763b099b
initial commit of ConfigOptionParser
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
12 |
3081763b099b
initial commit of ConfigOptionParser
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
13 Override/add variables from the command line: |
3081763b099b
initial commit of ConfigOptionParser
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
14 python example.py -c example.ini blah=bleem fargo=bah -f blah |
3081763b099b
initial commit of ConfigOptionParser
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
15 """ |
3081763b099b
initial commit of ConfigOptionParser
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
16 |
3081763b099b
initial commit of ConfigOptionParser
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
17 import sys |
3081763b099b
initial commit of ConfigOptionParser
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
18 from configoptionparser import ConfigOptionParser |
3081763b099b
initial commit of ConfigOptionParser
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
19 |
3081763b099b
initial commit of ConfigOptionParser
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
20 parser = ConfigOptionParser(dict_section='variables') |
3081763b099b
initial commit of ConfigOptionParser
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
21 parser.add_option('-f', dest='foo') |
3081763b099b
initial commit of ConfigOptionParser
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
22 parser.add_option('--baz', default='the baz default string') |
3081763b099b
initial commit of ConfigOptionParser
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
23 parser.add_option('--fleem') |
3081763b099b
initial commit of ConfigOptionParser
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
24 parser.add_option('--list', dest='list', action='append') |
3081763b099b
initial commit of ConfigOptionParser
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
25 parser.add_option('--verbose', dest='verbose', action='store_true', |
3081763b099b
initial commit of ConfigOptionParser
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
26 default=False) |
3081763b099b
initial commit of ConfigOptionParser
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
27 |
3081763b099b
initial commit of ConfigOptionParser
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
28 def main(args=sys.argv[1:]): |
3081763b099b
initial commit of ConfigOptionParser
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
29 options, args = parser.parse_args(args) |
3081763b099b
initial commit of ConfigOptionParser
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
30 for key in sorted(options.__dict__): |
3081763b099b
initial commit of ConfigOptionParser
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
31 print '%s: %s' % (key, options.__dict__[key]) |
3081763b099b
initial commit of ConfigOptionParser
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
32 print 'args: %s' % args |
3081763b099b
initial commit of ConfigOptionParser
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
33 |
3081763b099b
initial commit of ConfigOptionParser
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
34 if __name__ == '__main__': |
3081763b099b
initial commit of ConfigOptionParser
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
35 main() |