|
7 | 7 |
|
8 | 8 | **Source code:** :source:`Lib/getopt.py` |
9 | 9 |
|
10 | | -.. deprecated:: 3.13 |
11 | | - The :mod:`getopt` module is :term:`soft deprecated` and will not be |
12 | | - developed further; development will continue with the :mod:`argparse` |
13 | | - module. |
14 | | - |
15 | | -.. note:: |
16 | | - |
17 | | - The :mod:`getopt` module is a parser for command line options whose API is |
18 | | - designed to be familiar to users of the C :c:func:`!getopt` function. Users who |
19 | | - are unfamiliar with the C :c:func:`!getopt` function or who would like to write |
20 | | - less code and get better help and error messages should consider using the |
21 | | - :mod:`argparse` module instead. |
22 | | - |
23 | | --------------- |
24 | | - |
25 | 10 | This module helps scripts to parse the command line arguments in ``sys.argv``. |
26 | 11 | It supports the same conventions as the Unix :c:func:`!getopt` function (including |
27 | 12 | the special meanings of arguments of the form '``-``' and '``--``'). Long |
28 | 13 | options similar to those supported by GNU software may be used as well via an |
29 | 14 | optional third argument. |
30 | 15 |
|
| 16 | +The :mod:`getopt` module is a parser for command line options whose API is |
| 17 | +designed to be familiar to users of the C :c:func:`!getopt` function. Users who |
| 18 | +are unfamiliar with the C :c:func:`!getopt` function or who would like to write |
| 19 | +less code and get better help and error messages should consider using the |
| 20 | +:mod:`optparse` or :mod:`argparse` module instead. |
| 21 | + |
31 | 22 | This module provides two functions and an |
32 | 23 | exception: |
33 | 24 |
|
@@ -144,26 +135,41 @@ In a script, typical usage is something like this:: |
144 | 135 | output = a |
145 | 136 | else: |
146 | 137 | assert False, "unhandled option" |
147 | | - # ... |
| 138 | + process(args, output=output, verbose=verbose) |
148 | 139 |
|
149 | 140 | if __name__ == "__main__": |
150 | 141 | main() |
151 | 142 |
|
152 | 143 | Note that an equivalent command line interface could be produced with less code |
153 | | -and more informative help and error messages by using the :mod:`argparse` module:: |
| 144 | +and more informative help and error messages by using the :mod:`optparse` module:: |
| 145 | + |
| 146 | + import optparse |
| 147 | + |
| 148 | + if __name__ == '__main__': |
| 149 | + parser = optparse.OptionParser() |
| 150 | + parser.add_option('-o', '--output') |
| 151 | + parser.add_option('-v', dest='verbose', action='store_true') |
| 152 | + opts, args = parser.parse_args() |
| 153 | + process(args, output=opts.output, verbose=opts.verbose) |
| 154 | + |
| 155 | +A roughtly equivalent command line interface could also be produced by using |
| 156 | +the :mod:`argparse` module:: |
154 | 157 |
|
155 | 158 | import argparse |
156 | 159 |
|
157 | 160 | if __name__ == '__main__': |
158 | 161 | parser = argparse.ArgumentParser() |
159 | 162 | parser.add_argument('-o', '--output') |
160 | 163 | parser.add_argument('-v', dest='verbose', action='store_true') |
| 164 | + parser.add_argument('rest', nargs='*') |
161 | 165 | args = parser.parse_args() |
162 | | - # ... do something with args.output ... |
163 | | - # ... do something with args.verbose .. |
| 166 | + process(args.rest, output=args.output, verbose=args.verbose) |
164 | 167 |
|
165 | 168 | .. seealso:: |
166 | 169 |
|
| 170 | + Module :mod:`optparse` |
| 171 | + More object-oriented command line option parsing. |
| 172 | + |
167 | 173 | Module :mod:`argparse` |
168 | 174 | Alternative command line option and argument parsing library. |
169 | 175 |
|
0 commit comments