diff options
Diffstat (limited to 'Doc/library/optparse.rst')
-rw-r--r-- | Doc/library/optparse.rst | 32 |
1 files changed, 16 insertions, 16 deletions
diff --git a/Doc/library/optparse.rst b/Doc/library/optparse.rst index 417b3bb..dfb43a1 100644 --- a/Doc/library/optparse.rst +++ b/Doc/library/optparse.rst @@ -27,7 +27,7 @@ GNU/POSIX syntax, and additionally generates usage and help messages for you. Here's an example of using :mod:`optparse` in a simple script:: from optparse import OptionParser - [...] + ... parser = OptionParser() parser.add_option("-f", "--file", dest="filename", help="write report to FILE", metavar="FILE") @@ -254,7 +254,7 @@ First, you need to import the OptionParser class; then, early in the main program, create an OptionParser instance:: from optparse import OptionParser - [...] + ... parser = OptionParser() Then you can start defining options. The basic syntax is:: @@ -721,7 +721,7 @@ you can call :func:`OptionParser.error` to signal an application-defined error condition:: (options, args) = parser.parse_args() - [...] + ... if options.a and options.b: parser.error("options -a and -b are mutually exclusive") @@ -761,7 +761,7 @@ Putting it all together Here's what :mod:`optparse`\ -based scripts usually look like:: from optparse import OptionParser - [...] + ... def main(): usage = "usage: %prog [options] arg" parser = OptionParser(usage) @@ -771,13 +771,13 @@ Here's what :mod:`optparse`\ -based scripts usually look like:: action="store_true", dest="verbose") parser.add_option("-q", "--quiet", action="store_false", dest="verbose") - [...] + ... (options, args) = parser.parse_args() if len(args) != 1: parser.error("incorrect number of arguments") if options.verbose: print "reading %s..." % options.filename - [...] + ... if __name__ == "__main__": main() @@ -1412,7 +1412,7 @@ If you're not careful, it's easy to define options with conflicting option strings:: parser.add_option("-n", "--dry-run", ...) - [...] + ... parser.add_option("-n", "--noisy", ...) (This is particularly true if you've defined your own OptionParser subclass with @@ -1453,7 +1453,7 @@ that option. If the user asks for help, the help message will reflect that:: Options: --dry-run do no harm - [...] + ... -n, --noisy be noisy It's possible to whittle away the option strings for a previously-added option @@ -1468,7 +1468,7 @@ At this point, the original ``-n``/``--dry-run`` option is no longer accessible, so :mod:`optparse` removes it, leaving this help text:: Options: - [...] + ... -n, --noisy be noisy --dry-run new dry-run option @@ -1704,7 +1704,7 @@ seen, but blow up if it comes after ``-b`` in the command-line. :: if parser.values.b: raise OptionValueError("can't use -a after -b") parser.values.a = 1 - [...] + ... parser.add_option("-a", action="callback", callback=check_order) parser.add_option("-b", action="store_true", dest="b") @@ -1722,7 +1722,7 @@ message and the flag that it sets must be generalized. :: if parser.values.b: raise OptionValueError("can't use %s after -b" % opt_str) setattr(parser.values, option.dest, 1) - [...] + ... parser.add_option("-a", action="callback", callback=check_order, dest='a') parser.add_option("-b", action="store_true", dest="b") parser.add_option("-c", action="callback", callback=check_order, dest='c') @@ -1742,7 +1742,7 @@ should not be called when the moon is full, all you have to do is this:: raise OptionValueError("%s option invalid when moon is full" % opt_str) setattr(parser.values, option.dest, 1) - [...] + ... parser.add_option("--foo", action="callback", callback=check_moon, dest="foo") @@ -1765,7 +1765,7 @@ Here's an example that just emulates the standard ``"store"`` action:: def store_value(option, opt_str, value, parser): setattr(parser.values, option.dest, value) - [...] + ... parser.add_option("--foo", action="callback", callback=store_value, type="int", nargs=3, dest="foo") @@ -1827,9 +1827,9 @@ arguments:: del parser.rargs[:len(value)] setattr(parser.values, option.dest, value) - [...] - parser.add_option("-c", "--callback", dest="vararg_attr", - action="callback", callback=vararg_callback) + ... + parser.add_option("-c", "--callback", dest="vararg_attr", + action="callback", callback=vararg_callback) .. _optparse-extending-optparse: |