Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork33.7k
GH-124478: Cleanup argparse documentation#124877
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Merged
Uh oh!
There was an error while loading.Please reload this page.
Merged
Changes fromall commits
Commits
Show all changes
24 commits Select commitHold shift + click to select a range
484ca7f Progress on paring things down
savannahostrowski47a411a More deduping
savannahostrowski9845e44 Move details about upgrading from optparse to a separate doc
savannahostrowski5aae0a0 Updates
savannahostrowskie275d46 resolve merge conflict
savannahostrowski5f14ee3 remove table
savannahostrowski3c055d4 Apply suggestions from code review
savannahostrowski1cb7d1f Move optparse to howto
savannahostrowskibfb28af Appease linter
savannahostrowski936c808 Address PR comments
savannahostrowskia5baef9 Address PR comments
savannahostrowski782a601 Fix indentation
savannahostrowski903c702 Fix indentation
savannahostrowskidb7adf3 Fix indentation
savannahostrowski52f7bbe Update TOC
savannahostrowski300ed64 Update argparse-optparse.rst
savannahostrowski3273f03 Update argparse.rst
savannahostrowskif4395d1 Address feedback
savannahostrowski537684b Run pre-commit
savannahostrowskif1e5e71 Remove formatting on params
savannahostrowski743bfb5 Merge branch 'main' into gh-124478
savannahostrowskid9f261c Merge branch 'main' into gh-124478
savannahostrowski1c46505 Formatting for parameters
savannahostrowski8fcdb3a Merge branch 'main' into gh-124478
savannahostrowskiFile filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,55 @@ | ||
| .. currentmodule:: argparse | ||
| .. _upgrading-optparse-code: | ||
| ========================== | ||
| Upgrading optparse code | ||
| ========================== | ||
| Originally, the :mod:`argparse` module had attempted to maintain compatibility | ||
| with :mod:`optparse`. However, :mod:`optparse` was difficult to extend | ||
| transparently, particularly with the changes required to support | ||
| ``nargs=`` specifiers and better usage messages. When most everything in | ||
| :mod:`optparse` had either been copy-pasted over or monkey-patched, it no | ||
| longer seemed practical to try to maintain the backwards compatibility. | ||
| The :mod:`argparse` module improves on the :mod:`optparse` | ||
| module in a number of ways including: | ||
| * Handling positional arguments. | ||
| * Supporting subcommands. | ||
| * Allowing alternative option prefixes like ``+`` and ``/``. | ||
| * Handling zero-or-more and one-or-more style arguments. | ||
| * Producing more informative usage messages. | ||
| * Providing a much simpler interface for custom ``type`` and ``action``. | ||
| A partial upgrade path from :mod:`optparse` to :mod:`argparse`: | ||
| * Replace all :meth:`optparse.OptionParser.add_option` calls with | ||
| :meth:`ArgumentParser.add_argument` calls. | ||
| * Replace ``(options, args) = parser.parse_args()`` with ``args = | ||
| parser.parse_args()`` and add additional :meth:`ArgumentParser.add_argument` | ||
| calls for the positional arguments. Keep in mind that what was previously | ||
| called ``options``, now in the :mod:`argparse` context is called ``args``. | ||
| * Replace :meth:`optparse.OptionParser.disable_interspersed_args` | ||
| by using :meth:`~ArgumentParser.parse_intermixed_args` instead of | ||
| :meth:`~ArgumentParser.parse_args`. | ||
| * Replace callback actions and the ``callback_*`` keyword arguments with | ||
| ``type`` or ``action`` arguments. | ||
| * Replace string names for ``type`` keyword arguments with the corresponding | ||
| type objects (e.g. int, float, complex, etc). | ||
| * Replace :class:`optparse.Values` with :class:`Namespace` and | ||
| :exc:`optparse.OptionError` and :exc:`optparse.OptionValueError` with | ||
| :exc:`ArgumentError`. | ||
| * Replace strings with implicit arguments such as ``%default`` or ``%prog`` with | ||
| the standard Python syntax to use dictionaries to format strings, that is, | ||
| ``%(default)s`` and ``%(prog)s``. | ||
| * Replace the OptionParser constructor ``version`` argument with a call to | ||
| ``parser.add_argument('--version', action='version', version='<the version>')``. |
Oops, something went wrong.
Uh oh!
There was an error while loading.Please reload this page.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.