python:optparse
Differences
This shows you the differences between two versions of the page.
| Next revision | Previous revision | ||
| python:optparse [2010/03/15 12:04] – created tkbletsc | python:optparse [2011/06/05 17:40] (current) – tkbletsc | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| <code python> | <code python> | ||
| + | from optparse import OptionParser | ||
| + | |||
| things = { | things = { | ||
| ' | ' | ||
| Line 6: | Line 8: | ||
| } | } | ||
| parser = OptionParser(" | parser = OptionParser(" | ||
| + | |||
| + | # uncomment if you need to ignore options after the first argument, e.g. " | ||
| + | # parser.disable_interspersed_args() | ||
| parser.add_option(" | parser.add_option(" | ||
| Line 19: | Line 24: | ||
| sys.exit(0) | sys.exit(0) | ||
| - | bits = options.bits | + | num = options.number |
| thing = things[option.thing] | thing = things[option.thing] | ||
| if options.verbose: | if options.verbose: | ||
| else: | else: | ||
| + | </ | ||
| + | |||
| + | Shorter: | ||
| + | < | ||
| + | from optparse import OptionParser | ||
| + | |||
| + | parser = OptionParser(" | ||
| + | |||
| + | parser.add_option(" | ||
| + | parser.add_option(" | ||
| + | parser.add_option(" | ||
| + | parser.add_option(" | ||
| + | |||
| + | (options, args) = parser.parse_args() | ||
| + | |||
| + | if (len(args)< | ||
| + | parser.print_help() | ||
| + | sys.exit(0) | ||
| + | |||
| </ | </ | ||
python/optparse.1268679893.txt.gz · Last modified: by tkbletsc
