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