-
-
Notifications
You must be signed in to change notification settings - Fork 30.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
…128191) * Remove getopt and optparse deprecation notices * Add new docs sections for command line app helper libraries * Add guidance on choosing a CLI parsing library to the optparse docs * Link to the new guidance from the argparse and getopt docs * Reword intro in docs section for superseded stdlib modules * Reframe the optparse->argparse guide as a migration guide rather than as an upgrade guide --------- (cherry picked from commit 831b6de) Co-authored-by: Alyssa Coghlan <[email protected]> Co-authored-by: Serhiy Storchaka <[email protected]>
- Loading branch information
1 parent
09d15aa
commit 6f3c2c8
Showing
12 changed files
with
266 additions
and
65 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
.. _cmdlinelibs: | ||
|
||
******************************** | ||
Command Line Interface Libraries | ||
******************************** | ||
|
||
The modules described in this chapter assist with implementing | ||
command line and terminal interfaces for applications. | ||
|
||
Here's an overview: | ||
|
||
.. toctree:: | ||
:maxdepth: 1 | ||
|
||
argparse.rst | ||
optparse.rst | ||
getpass.rst | ||
fileinput.rst | ||
curses.rst | ||
curses.ascii.rst | ||
curses.panel.rst |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,7 +14,6 @@ in this chapter is: | |
|
||
pathlib.rst | ||
os.path.rst | ||
fileinput.rst | ||
stat.rst | ||
filecmp.rst | ||
tempfile.rst | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,25 +3,135 @@ | |
|
||
.. module:: optparse | ||
:synopsis: Command-line option parsing library. | ||
:deprecated: | ||
|
||
.. moduleauthor:: Greg Ward <[email protected]> | ||
.. sectionauthor:: Greg Ward <[email protected]> | ||
|
||
**Source code:** :source:`Lib/optparse.py` | ||
|
||
.. deprecated:: 3.2 | ||
The :mod:`optparse` module is :term:`soft deprecated` and will not be | ||
developed further; development will continue with the :mod:`argparse` | ||
module. | ||
|
||
-------------- | ||
|
||
.. _choosing-an-argument-parser: | ||
|
||
Choosing an argument parsing library | ||
------------------------------------ | ||
|
||
The standard library includes three argument parsing libraries: | ||
|
||
* :mod:`getopt`: a module that closely mirrors the procedural C ``getopt`` API. | ||
Included in the standard library since before the initial Python 1.0 release. | ||
* :mod:`optparse`: a declarative replacement for ``getopt`` that | ||
provides equivalent functionality without requiring each application | ||
to implement its own procedural option parsing logic. Included | ||
in the standard library since the Python 2.3 release. | ||
* :mod:`argparse`: a more opinionated alternative to ``optparse`` that | ||
provides more functionality by default, at the expense of reduced application | ||
flexibility in controlling exactly how arguments are processed. Included in | ||
the standard library since the Python 2.7 and Python 3.2 releases. | ||
|
||
In the absence of more specific argument parsing design constraints, :mod:`argparse` | ||
is the recommended choice for implementing command line applications, as it offers | ||
the highest level of baseline functionality with the least application level code. | ||
|
||
:mod:`getopt` is retained almost entirely for backwards compatibility reasons. | ||
However, it also serves a niche use case as a tool for prototyping and testing | ||
command line argument handling in ``getopt``-based C applications. | ||
|
||
:mod:`optparse` should be considered as an alternative to :mod:`argparse` in the | ||
following cases: | ||
|
||
* an application is already using :mod:`optparse` and doesn't want to risk the | ||
subtle behavioural changes that may arise when migrating to :mod:`argparse` | ||
* the application requires additional control over the way options and | ||
positional parameters are interleaved on the command line (including | ||
the ability to disable the interleaving feature completely) | ||
* the application requires additional control over the incremental parsing | ||
of command line elements (while ``argparse`` does support this, the | ||
exact way it works in practice is undesirable for some use cases) | ||
* the application requires additional control over the handling of options | ||
which accept parameter values that may start with ``-`` (such as delegated | ||
options to be passed to invoked subprocesses) | ||
* the application requires some other command line parameter processing | ||
behavior which ``argparse`` does not support, but which can be implemented | ||
in terms of the lower level interface offered by ``optparse`` | ||
|
||
These considerations also mean that :mod:`optparse` is likely to provide a | ||
better foundation for library authors writing third party command line | ||
argument processing libraries. | ||
|
||
As a concrete example, consider the following two command line argument | ||
parsing configurations, the first using ``optparse``, and the second | ||
using ``argparse``: | ||
|
||
.. testcode:: | ||
|
||
import optparse | ||
|
||
if __name__ == '__main__': | ||
parser = optparse.OptionParser() | ||
parser.add_option('-o', '--output') | ||
parser.add_option('-v', dest='verbose', action='store_true') | ||
opts, args = parser.parse_args() | ||
process(args, output=opts.output, verbose=opts.verbose) | ||
|
||
.. testcode:: | ||
|
||
import argparse | ||
|
||
if __name__ == '__main__': | ||
parser = argparse.ArgumentParser() | ||
parser.add_argument('-o', '--output') | ||
parser.add_argument('-v', dest='verbose', action='store_true') | ||
parser.add_argument('rest', nargs='*') | ||
args = parser.parse_args() | ||
process(args.rest, output=args.output, verbose=args.verbose) | ||
|
||
The most obvious difference is that in the ``optparse`` version, the non-option | ||
arguments are processed separately by the application after the option processing | ||
is complete. In the ``argparse`` version, positional arguments are declared and | ||
processed in the same way as the named options. | ||
|
||
However, the ``argparse`` version will also handle some parameter combination | ||
differently from the way the ``optparse`` version would handle them. | ||
For example (amongst other differences): | ||
|
||
* supplying ``-o -v`` gives ``output="-v"`` and ``verbose=False`` | ||
when using ``optparse``, but a usage error with ``argparse`` | ||
(complaining that no value has been supplied for ``-o/--output``, | ||
since ``-v`` is interpreted as meaning the verbosity flag) | ||
* similarly, supplying ``-o --`` gives ``output="--"`` and ``args=()`` | ||
when using ``optparse``, but a usage error with ``argparse`` | ||
(also complaining that no value has been supplied for ``-o/--output``, | ||
since ``--`` is interpreted as terminating the option processing | ||
and treating all remaining values as positional arguments) | ||
* supplying ``-o=foo`` gives ``output="=foo"`` when using ``optparse``, | ||
but gives ``output="foo"`` with ``argparse`` (since ``=`` is special | ||
cased as an alternative separator for option parameter values) | ||
|
||
Whether these differing behaviors in the ``argparse`` version are | ||
considered desirable or a problem will depend on the specific command line | ||
application use case. | ||
|
||
.. seealso:: | ||
|
||
:pypi:`click` is a third party argument processing library (originally | ||
based on ``optparse``), which allows command line applications to be | ||
developed as a set of decorated command implementation functions. | ||
|
||
Other third party libraries, such as :pypi:`typer` or :pypi:`msgspec-click`, | ||
allow command line interfaces to be specified in ways that more effectively | ||
integrate with static checking of Python type annotations. | ||
|
||
|
||
Introduction | ||
------------ | ||
|
||
:mod:`optparse` is a more convenient, flexible, and powerful library for parsing | ||
command-line options than the old :mod:`getopt` module. :mod:`optparse` uses a | ||
more declarative style of command-line parsing: you create an instance of | ||
:class:`OptionParser`, populate it with options, and parse the command | ||
line. :mod:`optparse` allows users to specify options in the conventional | ||
command-line options than the minimalist :mod:`getopt` module. | ||
:mod:`optparse` uses a more declarative style of command-line parsing: | ||
you create an instance of :class:`OptionParser`, | ||
populate it with options, and parse the command line. | ||
:mod:`optparse` allows users to specify options in the conventional | ||
GNU/POSIX syntax, and additionally generates usage and help messages for you. | ||
|
||
Here's an example of using :mod:`optparse` in a simple script:: | ||
|
@@ -82,10 +192,11 @@ Background | |
---------- | ||
|
||
:mod:`optparse` was explicitly designed to encourage the creation of programs | ||
with straightforward, conventional command-line interfaces. To that end, it | ||
supports only the most common command-line syntax and semantics conventionally | ||
used under Unix. If you are unfamiliar with these conventions, read this | ||
section to acquaint yourself with them. | ||
with straightforward command-line interfaces that follow the conventions | ||
established by the :c:func:`!getopt` family of functions available to C developers. | ||
To that end, it supports only the most common command-line syntax and semantics | ||
conventionally used under Unix. If you are unfamiliar with these conventions, | ||
reading this section will allow you to acquaint yourself with them. | ||
|
||
|
||
.. _optparse-terminology: | ||
|
Oops, something went wrong.