Changeset 391 for python/trunk/Doc/library/getopt.rst
- Timestamp:
- Mar 19, 2014, 11:31:01 PM (11 years ago)
- Location:
- python/trunk
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
python/trunk
-
Property svn:mergeinfo
set to
/python/vendor/Python-2.7.6 merged eligible /python/vendor/current merged eligible
-
Property svn:mergeinfo
set to
-
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 ========================================================= 4 3 5 4 .. module:: getopt … … 7 6 names. 8 7 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. 9 18 10 19 This module helps scripts to parse the command line arguments in ``sys.argv``. 11 It supports the same conventions as the Unix :c func:`getopt` function (including20 It supports the same conventions as the Unix :c:func:`getopt` function (including 12 21 the special meanings of arguments of the form '``-``' and '``--``'). Long 13 22 options similar to those supported by GNU software may be used as well via an 14 23 optional third argument. 15 16 A more convenient, flexible, and powerful alternative is the17 :mod:`optparse` module.18 24 19 25 This module provides two functions and an … … 27 33 means ``sys.argv[1:]``. *options* is the string of option letters that the 28 34 script wants to recognize, with options that require an argument followed by a 29 colon (``':'``; i.e., the same format that Unix :c func:`getopt` uses).35 colon (``':'``; i.e., the same format that Unix :c:func:`getopt` uses). 30 36 31 37 .. note:: 32 38 33 Unlike GNU :c func:`getopt`, after a non-option argument, all further39 Unlike GNU :c:func:`getopt`, after a non-option argument, all further 34 40 arguments are considered also non-options. This is similar to the way 35 41 non-GNU Unix systems work. 36 42 37 43 *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 ``'--'`` 39 45 characters should not be included in the option name. Long options which 40 46 require an argument should be followed by an equal sign (``'='``). Optional … … 43 49 long as they provide a prefix of the option name that matches exactly one of 44 50 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`` 46 52 will not match uniquely, so :exc:`GetoptError` will be raised. 47 53 … … 51 57 option-and-value pair returned has the option as its first element, prefixed 52 58 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 its59 options (e.g., ``'--long-option'``), and the option argument as its 54 60 second element, or an empty string if the option has no argument. The 55 61 options occur in the list in the same order in which they were found, thus … … 64 70 non-option argument is encountered. 65 71 66 If the first character of the option string is '+', or if the environment72 If the first character of the option string is ``'+'``, or if the environment 67 73 variable :envvar:`POSIXLY_CORRECT` is set, then option processing stops as 68 74 soon as a non-option argument is encountered. … … 121 127 try: 122 128 opts, args = getopt.getopt(sys.argv[1:], "ho:v", ["help", "output="]) 123 except getopt.GetoptError ,err:129 except getopt.GetoptError as err: 124 130 # print help information and exit: 125 131 print str(err) # will print something like "option -a not recognized" … … 143 149 main() 144 150 151 Note that an equivalent command line interface could be produced with less code 152 and 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 .. 145 163 146 164 .. seealso:: 147 165 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. 150 168
Note:
See TracChangeset
for help on using the changeset viewer.