Ignore:
Timestamp:
Mar 19, 2014, 11:31:01 PM (11 years ago)
Author:
dmik
Message:

python: Merge vendor 2.7.6 to trunk.

Location:
python/trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • python/trunk

  • python/trunk/Doc/library/optparse.rst

    r2 r391  
    1 :mod:`optparse` --- More powerful command line option parser
    2 ============================================================
     1:mod:`optparse` --- Parser for command line options
     2===================================================
    33
    44.. module:: optparse
    5    :synopsis: More convenient, flexible, and powerful command-line parsing library.
     5   :synopsis: Command-line option parsing library.
     6   :deprecated:
    67.. moduleauthor:: Greg Ward <gward@python.net>
    7 
     8.. sectionauthor:: Greg Ward <gward@python.net>
    89
    910.. versionadded:: 2.3
    1011
    11 .. sectionauthor:: Greg Ward <gward@python.net>
    12 
     12.. deprecated:: 2.7
     13   The :mod:`optparse` module is deprecated and will not be developed further;
     14   development will continue with the :mod:`argparse` module.
     15
     16**Source code:** :source:`Lib/optparse.py`
     17
     18--------------
    1319
    1420:mod:`optparse` is a more convenient, flexible, and powerful library for parsing
     
    6066.. code-block:: text
    6167
    62    usage: <yourscript> [options]
    63 
    64    options:
     68   Usage: <yourscript> [options]
     69
     70   Options:
    6571     -h, --help            show this help message and exit
    6672     -f FILE, --file=FILE  write report to FILE
     
    103109   execution of a program.  There are many different syntaxes for options; the
    104110   traditional Unix syntax is a hyphen ("-") followed by a single letter,
    105    e.g. ``"-x"`` or ``"-F"``.  Also, traditional Unix syntax allows multiple
    106    options to be merged into a single argument, e.g.  ``"-x -F"`` is equivalent
    107    to ``"-xF"``.  The GNU project introduced ``"--"`` followed by a series of
    108    hyphen-separated words, e.g.  ``"--file"`` or ``"--dry-run"``.  These are the
     111   e.g. ``-x`` or ``-F``.  Also, traditional Unix syntax allows multiple
     112   options to be merged into a single argument, e.g. ``-x -F`` is equivalent
     113   to ``-xF``.  The GNU project introduced ``--`` followed by a series of
     114   hyphen-separated words, e.g. ``--file`` or ``--dry-run``.  These are the
    109115   only two option syntaxes provided by :mod:`optparse`.
    110116
    111117   Some other option syntaxes that the world has seen include:
    112118
    113    * a hyphen followed by a few letters, e.g. ``"-pf"`` (this is *not* the same
     119   * a hyphen followed by a few letters, e.g. ``-pf`` (this is *not* the same
    114120     as multiple options merged into a single argument)
    115121
    116    * a hyphen followed by a whole word, e.g. ``"-file"`` (this is technically
     122   * a hyphen followed by a whole word, e.g. ``-file`` (this is technically
    117123     equivalent to the previous syntax, but they aren't usually seen in the same
    118124     program)
    119125
    120126   * a plus sign followed by a single letter, or a few letters, or a word, e.g.
    121      ``"+f"``, ``"+rgb"``
    122 
    123    * a slash followed by a letter, or a few letters, or a word, e.g. ``"/f"``,
    124      ``"/file"``
     127     ``+f``, ``+rgb``
     128
     129   * a slash followed by a letter, or a few letters, or a word, e.g. ``/f``,
     130     ``/file``
    125131
    126132   These option syntaxes are not supported by :mod:`optparse`, and they never
     
    150156   people want an "optional option arguments" feature, meaning that some options
    151157   will take an argument if they see it, and won't if they don't.  This is
    152    somewhat controversial, because it makes parsing ambiguous: if ``"-a"`` takes
    153    an optional argument and ``"-b"`` is another option entirely, how do we
    154    interpret ``"-ab"``?  Because of this ambiguity, :mod:`optparse` does not
     158   somewhat controversial, because it makes parsing ambiguous: if ``-a`` takes
     159   an optional argument and ``-b`` is another option entirely, how do we
     160   interpret ``-ab``?  Because of this ambiguity, :mod:`optparse` does not
    155161   support this feature.
    156162
     
    164170   "required option" is self-contradictory in English.  :mod:`optparse` doesn't
    165171   prevent you from implementing required options, but doesn't give you much
    166    help at it either.  See ``examples/required_1.py`` and
    167    ``examples/required_2.py`` in the :mod:`optparse` source distribution for two
    168    ways to implement required options with :mod:`optparse`.
     172   help at it either.
    169173
    170174For example, consider this hypothetical command-line::
    171175
    172    prog -v --report /tmp/report.txt foo bar
    173 
    174 ``"-v"`` and ``"--report"`` are both options.  Assuming that :option:`--report`
    175 takes one argument, ``"/tmp/report.txt"`` is an option argument.  ``"foo"`` and
    176 ``"bar"`` are positional arguments.
     176   prog -v --report report.txt foo bar
     177
     178``-v`` and ``--report`` are both options.  Assuming that ``--report``
     179takes one argument, ``report.txt`` is an option argument.  ``foo`` and
     180``bar`` are positional arguments.
    177181
    178182
     
    259263                     attr=value, ...)
    260264
    261 Each option has one or more option strings, such as ``"-f"`` or ``"--file"``,
     265Each option has one or more option strings, such as ``-f`` or ``--file``,
    262266and several option attributes that tell :mod:`optparse` what to expect and what
    263267to do when it encounters that option on the command line.
     
    272276string overall.
    273277
    274 The option strings passed to :meth:`add_option` are effectively labels for the
     278The option strings passed to :meth:`OptionParser.add_option` are effectively
     279labels for the
    275280option defined by that call.  For brevity, we will frequently refer to
    276281*encountering an option* on the command line; in reality, :mod:`optparse`
     
    288293
    289294* ``options``, an object containing values for all of your options---e.g. if
    290   ``"--file"`` takes a single string argument, then ``options.file`` will be the
     295  ``--file`` takes a single string argument, then ``options.file`` will be the
    291296  filename supplied by the user, or ``None`` if the user did not supply that
    292297  option
     
    334339   (options, args) = parser.parse_args(args)
    335340
    336 When :mod:`optparse` sees the option string ``"-f"``, it consumes the next
    337 argument, ``"foo.txt"``, and stores it in ``options.filename``.  So, after this
     341When :mod:`optparse` sees the option string ``-f``, it consumes the next
     342argument, ``foo.txt``, and stores it in ``options.filename``.  So, after this
    338343call to :meth:`parse_args`, ``options.filename`` is ``"foo.txt"``.
    339344
     
    347352
    348353Let's parse another fake command-line.  This time, we'll jam the option argument
    349 right up against the option: since ``"-n42"`` (one argument) is equivalent to
    350 ``"-n 42"`` (two arguments), the code ::
     354right up against the option: since ``-n42`` (one argument) is equivalent to
     355``-n 42`` (two arguments), the code ::
    351356
    352357   (options, args) = parser.parse_args(["-n42"])
    353358   print options.num
    354359
    355 will print ``"42"``.
     360will print ``42``.
    356361
    357362If you don't specify a type, :mod:`optparse` assumes ``string``.  Combined with
     
    363368If you don't supply a destination, :mod:`optparse` figures out a sensible
    364369default from the option strings: if the first long option string is
    365 ``"--foo-bar"``, then the default destination is ``foo_bar``.  If there are no
     370``--foo-bar``, then the default destination is ``foo_bar``.  If there are no
    366371long option strings, :mod:`optparse` looks at the first short option string: the
    367 default destination for ``"-f"`` is ``f``.
     372default destination for ``-f`` is ``f``.
    368373
    369374:mod:`optparse` also includes built-in ``long`` and ``complex`` types.  Adding
     
    379384---are quite common.  :mod:`optparse` supports them with two separate actions,
    380385``store_true`` and ``store_false``.  For example, you might have a ``verbose``
    381 flag that is turned on with ``"-v"`` and off with ``"-q"``::
     386flag that is turned on with ``-v`` and off with ``-q``::
    382387
    383388   parser.add_option("-v", action="store_true", dest="verbose")
     
    388393see below.)
    389394
    390 When :mod:`optparse` encounters ``"-v"`` on the command line, it sets
    391 ``options.verbose`` to ``True``; when it encounters ``"-q"``,
     395When :mod:`optparse` encounters ``-v`` on the command line, it sets
     396``options.verbose`` to ``True``; when it encounters ``-q``,
    392397``options.verbose`` is set to ``False``.
    393398
     
    429434
    430435First, consider the verbose/quiet example.  If we want :mod:`optparse` to set
    431 ``verbose`` to ``True`` unless ``"-q"`` is seen, then we can do this::
     436``verbose`` to ``True`` unless ``-q`` is seen, then we can do this::
    432437
    433438   parser.add_option("-v", action="store_true", dest="verbose", default=True)
     
    487492                          "or expert [default: %default]")
    488493
    489 If :mod:`optparse` encounters either ``"-h"`` or ``"--help"`` on the
     494If :mod:`optparse` encounters either ``-h`` or ``--help`` on the
    490495command-line, or if you just call :meth:`parser.print_help`, it prints the
    491496following to standard output:
     
    493498.. code-block:: text
    494499
    495    usage: <yourscript> [options] arg1 arg2
    496 
    497    options:
     500   Usage: <yourscript> [options] arg1 arg2
     501
     502   Options:
    498503     -h, --help            show this help message and exit
    499504     -v, --verbose         make lots of noise [default]
     
    514519     usage = "usage: %prog [options] arg1 arg2"
    515520
    516   :mod:`optparse` expands ``"%prog"`` in the usage string to the name of the
     521  :mod:`optparse` expands ``%prog`` in the usage string to the name of the
    517522  current program, i.e. ``os.path.basename(sys.argv[0])``.  The expanded string
    518523  is then printed before the detailed option help.
    519524
    520525  If you don't supply a usage string, :mod:`optparse` uses a bland but sensible
    521   default: ``"usage: %prog [options]"``, which is fine if your script doesn't
     526  default: ``"Usage: %prog [options]"``, which is fine if your script doesn't
    522527  take any positional arguments.
    523528
     
    532537
    533538  Here, "MODE" is called the meta-variable: it stands for the argument that the
    534   user is expected to supply to :option:`-m`/:option:`--mode`.  By default,
     539  user is expected to supply to ``-m``/``--mode``.  By default,
    535540  :mod:`optparse` converts the destination variable name to uppercase and uses
    536541  that for the meta-variable.  Sometimes, that's not what you want---for
    537   example, the :option:`--filename` option explicitly sets ``metavar="FILE"``,
     542  example, the ``--filename`` option explicitly sets ``metavar="FILE"``,
    538543  resulting in this automatically-generated option description::
    539544
     
    541546
    542547  This is important for more than just saving space, though: the manually
    543   written help text uses the meta-variable "FILE" to clue the user in that
    544   there's a connection between the semi-formal syntax "-f FILE" and the informal
     548  written help text uses the meta-variable ``FILE`` to clue the user in that
     549  there's a connection between the semi-formal syntax ``-f FILE`` and the informal
    545550  semantic description "write output to FILE". This is a simple but effective
    546551  way to make your help text a lot clearer and more useful for end users.
     
    552557   ``None``), ``%default`` expands to ``none``.
    553558
     559Grouping Options
     560++++++++++++++++
     561
    554562When dealing with many options, it is convenient to group these options for
    555563better help output.  An :class:`OptionParser` can contain several option groups,
    556564each of which can contain several options.
    557565
    558 Continuing with the parser defined above, adding an :class:`OptionGroup` to a
    559 parser is easy::
     566An option group is obtained using the class :class:`OptionGroup`:
     567
     568.. class:: OptionGroup(parser, title, description=None)
     569
     570   where
     571
     572   * parser is the :class:`OptionParser` instance the group will be insterted in
     573     to
     574   * title is the group title
     575   * description, optional, is a long description of the group
     576
     577:class:`OptionGroup` inherits from :class:`OptionContainer` (like
     578:class:`OptionParser`) and so the :meth:`add_option` method can be used to add
     579an option to the group.
     580
     581Once all the options are declared, using the :class:`OptionParser` method
     582:meth:`add_option_group` the group is added to the previously defined parser.
     583
     584Continuing with the parser defined in the previous section, adding an
     585:class:`OptionGroup` to a parser is easy::
    560586
    561587    group = OptionGroup(parser, "Dangerous Options",
     
    569595.. code-block:: text
    570596
    571     usage:  [options] arg1 arg2
    572 
    573     options:
    574       -h, --help           show this help message and exit
    575       -v, --verbose        make lots of noise [default]
    576       -q, --quiet          be vewwy quiet (I'm hunting wabbits)
    577       -fFILE, --file=FILE  write output to FILE
    578       -mMODE, --mode=MODE  interaction mode: one of 'novice', 'intermediate'
    579                            [default], 'expert'
    580 
    581       Dangerous Options:
    582       Caution: use of these options is at your own risk.  It is believed that
    583       some of them bite.
    584       -g                 Group option.
     597   Usage: <yourscript> [options] arg1 arg2
     598
     599   Options:
     600     -h, --help            show this help message and exit
     601     -v, --verbose         make lots of noise [default]
     602     -q, --quiet           be vewwy quiet (I'm hunting wabbits)
     603     -f FILE, --filename=FILE
     604                           write output to FILE
     605     -m MODE, --mode=MODE  interaction mode: novice, intermediate, or
     606                           expert [default: intermediate]
     607
     608     Dangerous Options:
     609       Caution: use these options at your own risk.  It is believed that some
     610       of them bite.
     611
     612       -g                  Group option.
     613
     614A bit more complete example might involve using more than one group: still
     615extending the previous example::
     616
     617    group = OptionGroup(parser, "Dangerous Options",
     618                        "Caution: use these options at your own risk.  "
     619                        "It is believed that some of them bite.")
     620    group.add_option("-g", action="store_true", help="Group option.")
     621    parser.add_option_group(group)
     622
     623    group = OptionGroup(parser, "Debug Options")
     624    group.add_option("-d", "--debug", action="store_true",
     625                     help="Print debug information")
     626    group.add_option("-s", "--sql", action="store_true",
     627                     help="Print all SQL statements executed")
     628    group.add_option("-e", action="store_true", help="Print every action done")
     629    parser.add_option_group(group)
     630
     631that results in the following output:
     632
     633.. code-block:: text
     634
     635   Usage: <yourscript> [options] arg1 arg2
     636
     637   Options:
     638     -h, --help            show this help message and exit
     639     -v, --verbose         make lots of noise [default]
     640     -q, --quiet           be vewwy quiet (I'm hunting wabbits)
     641     -f FILE, --filename=FILE
     642                           write output to FILE
     643     -m MODE, --mode=MODE  interaction mode: novice, intermediate, or expert
     644                           [default: intermediate]
     645
     646     Dangerous Options:
     647       Caution: use these options at your own risk.  It is believed that some
     648       of them bite.
     649
     650       -g                  Group option.
     651
     652     Debug Options:
     653       -d, --debug         Print debug information
     654       -s, --sql           Print all SQL statements executed
     655       -e                  Print every action done
     656
     657Another interesting method, in particular when working programmatically with
     658option groups is:
     659
     660.. method:: OptionParser.get_option_group(opt_str)
     661
     662   Return the :class:`OptionGroup` to which the short or long option
     663   string *opt_str* (e.g. ``'-o'`` or ``'--option'``) belongs. If
     664   there's no such :class:`OptionGroup`, return ``None``.
    585665
    586666.. _optparse-printing-version-string:
     
    595675   parser = OptionParser(usage="%prog [-f] [-q]", version="%prog 1.0")
    596676
    597 ``"%prog"`` is expanded just like it is in ``usage``.  Apart from that,
     677``%prog`` is expanded just like it is in ``usage``.  Apart from that,
    598678``version`` can contain anything you like.  When you supply it, :mod:`optparse`
    599 automatically adds a ``"--version"`` option to your parser. If it encounters
     679automatically adds a ``--version`` option to your parser. If it encounters
    600680this option on the command line, it expands your ``version`` string (by
    601 replacing ``"%prog"``), prints it to stdout, and exits.
     681replacing ``%prog``), prints it to stdout, and exits.
    602682
    603683For example, if your script is called ``/usr/bin/foo``::
     
    612692   Print the version message for the current program (``self.version``) to
    613693   *file* (default stdout).  As with :meth:`print_usage`, any occurrence
    614    of ``"%prog"`` in ``self.version`` is replaced with the name of the current
     694   of ``%prog`` in ``self.version`` is replaced with the name of the current
    615695   program.  Does nothing if ``self.version`` is empty or undefined.
    616696
     
    635715Handling user errors is much more important, since they are guaranteed to happen
    636716no matter how stable your code is.  :mod:`optparse` can automatically detect
    637 some user errors, such as bad option arguments (passing ``"-n 4x"`` where
    638 :option:`-n` takes an integer argument), missing arguments (``"-n"`` at the end
    639 of the command line, where :option:`-n` takes an argument of any type).  Also,
     717some user errors, such as bad option arguments (passing ``-n 4x`` where
     718``-n`` takes an integer argument), missing arguments (``-n`` at the end
     719of the command line, where ``-n`` takes an argument of any type).  Also,
    640720you can call :func:`OptionParser.error` to signal an application-defined error
    641721condition::
     
    650730error status 2.
    651731
    652 Consider the first example above, where the user passes ``"4x"`` to an option
     732Consider the first example above, where the user passes ``4x`` to an option
    653733that takes an integer::
    654734
    655735   $ /usr/bin/foo -n 4x
    656    usage: foo [options]
     736   Usage: foo [options]
    657737
    658738   foo: error: option -n: invalid integer value: '4x'
     
    661741
    662742   $ /usr/bin/foo -n
    663    usage: foo [options]
     743   Usage: foo [options]
    664744
    665745   foo: error: -n option requires an argument
     
    743823      A version string to print when the user supplies a version option. If you
    744824      supply a true value for ``version``, :mod:`optparse` automatically adds a
    745       version option with the single option string ``"--version"``.  The
    746       substring ``"%prog"`` is expanded the same as for ``usage``.
     825      version option with the single option string ``--version``.  The
     826      substring ``%prog`` is expanded the same as for ``usage``.
    747827
    748828   ``conflict_handler`` (default: ``"error"``)
     
    763843
    764844   ``add_help_option`` (default: ``True``)
    765       If true, :mod:`optparse` will add a help option (with option strings ``"-h"``
    766       and ``"--help"``) to the parser.
     845      If true, :mod:`optparse` will add a help option (with option strings ``-h``
     846      and ``--help``) to the parser.
    767847
    768848   ``prog``
    769       The string to use when expanding ``"%prog"`` in ``usage`` and ``version``
     849      The string to use when expanding ``%prog`` in ``usage`` and ``version``
    770850      instead of ``os.path.basename(sys.argv[0])``.
    771851
    772 
     852   ``epilog`` (default: ``None``)
     853      A paragraph of help text to print after the option help.
    773854
    774855.. _optparse-populating-parser:
     
    810891
    811892Each Option instance represents a set of synonymous command-line option strings,
    812 e.g. :option:`-f` and :option:`--file`.  You can specify any number of short or
     893e.g. ``-f`` and ``--file``.  You can specify any number of short or
    813894long option strings, but you must specify at least one overall option string.
    814895
     
    816897:meth:`add_option` method of :class:`OptionParser`.
    817898
    818 .. method:: OptionParser.add_option(opt_str[, ...], attr=value, ...)
     899.. method:: OptionParser.add_option(option)
     900            OptionParser.add_option(*opt_str, attr=value, ...)
    819901
    820902   To define an option with only a short option string::
     
    9731055
    9741056   Help text to print for this option when listing all available options after
    975    the user supplies a :attr:`~Option.help` option (such as ``"--help"``).  If
     1057   the user supplies a :attr:`~Option.help` option (such as ``--help``).  If
    9761058   no help text is supplied, the option will be listed without help text.  To
    9771059   hide this option, use the special value :data:`optparse.SUPPRESS_HELP`.
     
    10111093
    10121094  If :attr:`~Option.dest` is not supplied, :mod:`optparse` derives a destination
    1013   from the first long option string (e.g., ``"--foo-bar"`` implies
     1095  from the first long option string (e.g., ``--foo-bar`` implies
    10141096  ``foo_bar``). If there are no long option strings, :mod:`optparse` derives a
    1015   destination from the first short option string (e.g., ``"-f"`` implies ``f``).
     1097  destination from the first short option string (e.g., ``-f`` implies ``f``).
    10161098
    10171099  Example::
     
    10441126                       action="store_const", const=2, dest="verbose")
    10451127
    1046   If ``"--noisy"`` is seen, :mod:`optparse` will set  ::
     1128  If ``--noisy`` is seen, :mod:`optparse` will set  ::
    10471129
    10481130     options.verbose = 2
     
    10791161     parser.add_option("-t", "--tracks", action="append", type="int")
    10801162
    1081   If ``"-t3"`` is seen on the command-line, :mod:`optparse` does the equivalent
     1163  If ``-t3`` is seen on the command-line, :mod:`optparse` does the equivalent
    10821164  of::
    10831165
     
    10851167     options.tracks.append(int("3"))
    10861168
    1087   If, a little later on, ``"--tracks=4"`` is seen, it does::
     1169  If, a little later on, ``--tracks=4`` is seen, it does::
    10881170
    10891171     options.tracks.append(int("4"))
     1172
     1173  The ``append`` action calls the ``append`` method on the current value of the
     1174  option.  This means that any default value specified must have an ``append``
     1175  method.  It also means that if the default value is non-empty, the default
     1176  elements will be present in the parsed value for the option, with any values
     1177  from the command line appended after those default values::
     1178
     1179     >>> parser.add_option("--files", action="append", default=['~/.mypkg/defaults'])
     1180     >>> opts, args = parser.parse_args(['--files', 'overrides.mypkg'])
     1181     >>> opts.files
     1182     ['~/.mypkg/defaults', 'overrides.mypkg']
    10901183
    10911184* ``"append_const"`` [required: :attr:`~Option.const`; relevant:
     
    11071200     parser.add_option("-v", action="count", dest="verbosity")
    11081201
    1109   The first time ``"-v"`` is seen on the command line, :mod:`optparse` does the
     1202  The first time ``-v`` is seen on the command line, :mod:`optparse` does the
    11101203  equivalent of::
    11111204
     
    11131206     options.verbosity += 1
    11141207
    1115   Every subsequent occurrence of ``"-v"`` results in  ::
     1208  Every subsequent occurrence of ``-v`` results in  ::
    11161209
    11171210     options.verbosity += 1
     
    11561249     parser.add_option("--secret", help=SUPPRESS_HELP)
    11571250
    1158   If :mod:`optparse` sees either ``"-h"`` or ``"--help"`` on the command line,
     1251  If :mod:`optparse` sees either ``-h`` or ``--help`` on the command line,
    11591252  it will print something like the following help message to stdout (assuming
    11601253  ``sys.argv[0]`` is ``"foo.py"``):
     
    11621255  .. code-block:: text
    11631256
    1164      usage: foo.py [options]
    1165 
    1166      options:
     1257     Usage: foo.py [options]
     1258
     1259     Options:
    11671260       -h, --help        Show this help message and exit
    11681261       -v                Be moderately verbose
     
    12131306
    12141307``"choice"`` options are a subtype of ``"string"`` options.  The
    1215 :attr:`~Option.choices`` option attribute (a sequence of strings) defines the
     1308:attr:`~Option.choices` option attribute (a sequence of strings) defines the
    12161309set of allowed option arguments.  :func:`optparse.check_choice` compares
    12171310user-supplied option arguments against this master list and raises
     
    12351328
    12361329``values``
    1237    object to store option arguments in (default: a new instance of
    1238    :class:`optparse.Values`)
     1330   a :class:`optparse.Values` object to store option arguments in (default: a
     1331   new instance of :class:`Values`) -- if you give an existing object, the
     1332   option defaults will not be initialized on it
    12391333
    12401334and the return values are
     
    12691363.. method:: OptionParser.disable_interspersed_args()
    12701364
    1271    Set parsing to stop on the first non-option.  For example, if ``"-a"`` and
    1272    ``"-b"`` are both simple options that take no arguments, :mod:`optparse`
     1365   Set parsing to stop on the first non-option.  For example, if ``-a`` and
     1366   ``-b`` are both simple options that take no arguments, :mod:`optparse`
    12731367   normally accepts this syntax::
    12741368
     
    13001394
    13011395   Return true if the OptionParser has an option with option string *opt_str*
    1302    (e.g., ``"-q"`` or ``"--verbose"``).
     1396   (e.g., ``-q`` or ``--verbose``).
    13031397
    13041398.. method:: OptionParser.remove_option(opt_str)
     
    13531447
    13541448At this point, :mod:`optparse` detects that a previously-added option is already
    1355 using the ``"-n"`` option string.  Since ``conflict_handler`` is ``"resolve"``,
    1356 it resolves the situation by removing ``"-n"`` from the earlier option's list of
    1357 option strings.  Now ``"--dry-run"`` is the only way for the user to activate
     1449using the ``-n`` option string.  Since ``conflict_handler`` is ``"resolve"``,
     1450it resolves the situation by removing ``-n`` from the earlier option's list of
     1451option strings.  Now ``--dry-run`` is the only way for the user to activate
    13581452that option.  If the user asks for help, the help message will reflect that::
    13591453
    1360    options:
     1454   Options:
    13611455     --dry-run     do no harm
    13621456     [...]
     
    13711465   parser.add_option("--dry-run", ..., help="new dry-run option")
    13721466
    1373 At this point, the original :option:`-n/--dry-run` option is no longer
     1467At this point, the original ``-n``/``--dry-run`` option is no longer
    13741468accessible, so :mod:`optparse` removes it, leaving this help text::
    13751469
    1376    options:
     1470   Options:
    13771471     [...]
    13781472     -n, --noisy   be noisy
     
    14091503
    14101504   Print the usage message for the current program (``self.usage``) to *file*
    1411    (default stdout).  Any occurrence of the string ``"%prog"`` in ``self.usage``
     1505   (default stdout).  Any occurrence of the string ``%prog`` in ``self.usage``
    14121506   is replaced with the name of the current program.  Does nothing if
    14131507   ``self.usage`` is empty or not defined.
     
    14731567``callback`` is a function (or other callable object), so you must have already
    14741568defined ``my_callback()`` when you create this callback option. In this simple
    1475 case, :mod:`optparse` doesn't even know if :option:`-c` takes any arguments,
     1569case, :mod:`optparse` doesn't even know if ``-c`` takes any arguments,
    14761570which usually means that the option takes no arguments---the mere presence of
    1477 :option:`-c` on the command-line is all it needs to know.  In some
     1571``-c`` on the command-line is all it needs to know.  In some
    14781572circumstances, though, you might want your callback to consume an arbitrary
    14791573number of command-line arguments.  This is where writing callbacks gets tricky;
     
    15281622   is the option string seen on the command-line that's triggering the callback.
    15291623   (If an abbreviated long option was used, ``opt_str`` will be the full,
    1530    canonical option string---e.g. if the user puts ``"--foo"`` on the
    1531    command-line as an abbreviation for ``"--foobar"``, then ``opt_str`` will be
     1624   canonical option string---e.g. if the user puts ``--foo`` on the
     1625   command-line as an abbreviation for ``--foobar``, then ``opt_str`` will be
    15321626   ``"--foobar"``.)
    15331627
     
    16041698^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    16051699
    1606 Here's a slightly more interesting example: record the fact that ``"-a"`` is
    1607 seen, but blow up if it comes after ``"-b"`` in the command-line.  ::
     1700Here's a slightly more interesting example: record the fact that ``-a`` is
     1701seen, but blow up if it comes after ``-b`` in the command-line.  ::
    16081702
    16091703   def check_order(option, opt_str, value, parser):
     
    16221716
    16231717If you want to re-use this callback for several similar options (set a flag, but
    1624 blow up if ``"-b"`` has already been seen), it needs a bit of work: the error
     1718blow up if ``-b`` has already been seen), it needs a bit of work: the error
    16251719message and the flag that it sets must be generalized.  ::
    16261720
     
    16921786conventional Unix command-line parsing that :mod:`optparse` normally handles for
    16931787you.  In particular, callbacks should implement the conventional rules for bare
    1694 ``"--"`` and ``"-"`` arguments:
    1695 
    1696 * either ``"--"`` or ``"-"`` can be option arguments
    1697 
    1698 * bare ``"--"`` (if not the argument to some option): halt command-line
    1699   processing and discard the ``"--"``
    1700 
    1701 * bare ``"-"`` (if not the argument to some option): halt command-line
    1702   processing but keep the ``"-"`` (append it to ``parser.largs``)
     1788``--`` and ``-`` arguments:
     1789
     1790* either ``--`` or ``-`` can be option arguments
     1791
     1792* bare ``--`` (if not the argument to some option): halt command-line
     1793  processing and discard the ``--``
     1794
     1795* bare ``-`` (if not the argument to some option): halt command-line
     1796  processing but keep the ``-`` (append it to ``parser.largs``)
    17031797
    17041798If you want an option that takes a variable number of arguments, there are
     
    17711865
    17721866   where ``option`` is an :class:`Option` instance, ``opt`` is an option string
    1773    (e.g., ``"-f"``), and ``value`` is the string from the command line that must
     1867   (e.g., ``-f``), and ``value`` is the string from the command line that must
    17741868   be checked and converted to your desired type.  ``check_mytype()`` should
    17751869   return an object of the hypothetical type ``mytype``.  The value returned by
     
    18831977and appending it to an existing list, ``"extend"`` will take multiple values in
    18841978a single comma-delimited string, and extend an existing list with them.  That
    1885 is, if ``"--names"`` is an ``"extend"`` option of type ``"string"``, the command
     1979is, if ``--names`` is an ``"extend"`` option of type ``"string"``, the command
    18861980line ::
    18871981
Note: See TracChangeset for help on using the changeset viewer.