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/getopt.rst

    r2 r391  
    1 
    2 :mod:`getopt` --- Parser for command line options
    3 =================================================
     1:mod:`getopt` --- C-style parser for command line options
     2=========================================================
    43
    54.. module:: getopt
     
    76              names.
    87
     8**Source code:** :source:`Lib/getopt.py`
     9
     10--------------
     11
     12.. note::
     13   The :mod:`getopt` module is a parser for command line options whose API is
     14   designed to be familiar to users of the C :c:func:`getopt` function. Users who
     15   are unfamiliar with the C :c:func:`getopt` function or who would like to write
     16   less code and get better help and error messages should consider using the
     17   :mod:`argparse` module instead.
    918
    1019This module helps scripts to parse the command line arguments in ``sys.argv``.
    11 It supports the same conventions as the Unix :cfunc:`getopt` function (including
     20It supports the same conventions as the Unix :c:func:`getopt` function (including
    1221the special meanings of arguments of the form '``-``' and '``--``').  Long
    1322options similar to those supported by GNU software may be used as well via an
    1423optional third argument.
    15 
    16 A more convenient, flexible, and powerful alternative is the
    17 :mod:`optparse` module.
    1824
    1925This module provides two functions and an
     
    2733   means ``sys.argv[1:]``. *options* is the string of option letters that the
    2834   script wants to recognize, with options that require an argument followed by a
    29    colon (``':'``; i.e., the same format that Unix :cfunc:`getopt` uses).
     35   colon (``':'``; i.e., the same format that Unix :c:func:`getopt` uses).
    3036
    3137   .. note::
    3238
    33       Unlike GNU :cfunc:`getopt`, after a non-option argument, all further
     39      Unlike GNU :c:func:`getopt`, after a non-option argument, all further
    3440      arguments are considered also non-options. This is similar to the way
    3541      non-GNU Unix systems work.
    3642
    3743   *long_options*, if specified, must be a list of strings with the names of the
    38    long options which should be supported.  The leading ``'-``\ ``-'``
     44   long options which should be supported.  The leading ``'--'``
    3945   characters should not be included in the option name.  Long options which
    4046   require an argument should be followed by an equal sign (``'='``).  Optional
     
    4349   long as they provide a prefix of the option name that matches exactly one of
    4450   the accepted options.  For example, if *long_options* is ``['foo', 'frob']``,
    45    the option :option:`--fo` will match as :option:`--foo`, but :option:`--f`
     51   the option ``--fo`` will match as ``--foo``, but ``--f``
    4652   will not match uniquely, so :exc:`GetoptError` will be raised.
    4753
     
    5157   option-and-value pair returned has the option as its first element, prefixed
    5258   with a hyphen for short options (e.g., ``'-x'``) or two hyphens for long
    53    options (e.g., ``'-``\ ``-long-option'``), and the option argument as its
     59   options (e.g., ``'--long-option'``), and the option argument as its
    5460   second element, or an empty string if the option has no argument.  The
    5561   options occur in the list in the same order in which they were found, thus
     
    6470   non-option argument is encountered.
    6571
    66    If the first character of the option string is '+', or if the environment
     72   If the first character of the option string is ``'+'``, or if the environment
    6773   variable :envvar:`POSIXLY_CORRECT` is set, then option processing stops as
    6874   soon as a non-option argument is encountered.
     
    121127       try:
    122128           opts, args = getopt.getopt(sys.argv[1:], "ho:v", ["help", "output="])
    123        except getopt.GetoptError, err:
     129       except getopt.GetoptError as err:
    124130           # print help information and exit:
    125131           print str(err) # will print something like "option -a not recognized"
     
    143149       main()
    144150
     151Note that an equivalent command line interface could be produced with less code
     152and more informative help and error messages by using the :mod:`argparse` module::
     153
     154   import argparse
     155
     156   if __name__ == '__main__':
     157       parser = argparse.ArgumentParser()
     158       parser.add_argument('-o', '--output')
     159       parser.add_argument('-v', dest='verbose', action='store_true')
     160       args = parser.parse_args()
     161       # ... do something with args.output ...
     162       # ... do something with args.verbose ..
    145163
    146164.. seealso::
    147165
    148    Module :mod:`optparse`
    149       More object-oriented command line option parsing.
     166   Module :mod:`argparse`
     167      Alternative command line option and argument parsing library.
    150168
Note: See TracChangeset for help on using the changeset viewer.