Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork32.3k
gh-83648: Support deprecation of options, arguments and subcommands in argparse#114086
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
Uh oh!
There was an error while loading.Please reload this page.
Changes from1 commit
0244cd8
16edd19
24396b9
54d7bba
7aa0d88
12d3bc9
5f72bc6
acf49d7
File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
…n argparse
- Loading branch information
Uh oh!
There was an error while loading.Please reload this page.
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -777,6 +777,8 @@ The add_argument() method | ||
* dest_ - The name of the attribute to be added to the object returned by | ||
:meth:`parse_args`. | ||
* deprecated_ - Whether or not the usage of the argument is deprecated. | ||
serhiy-storchaka marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
The following sections describe how each of these are used. | ||
@@ -1439,6 +1441,35 @@ behavior:: | ||
>>> parser.parse_args('--foo XXX'.split()) | ||
Namespace(bar='XXX') | ||
.. _deprecated:: | ||
deprecated | ||
^^^^^^^^^^ | ||
During projects lifecycle some arguments could be removed from the | ||
command line, before removing these arguments definitively you would inform | ||
your user that arguments are deprecated and will be removed. | ||
serhiy-storchaka marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
The ``deprecated`` keyword argument of | ||
:meth:`~ArgumentParser.add_argument`, whose value default to ``False``, | ||
specifies if the argument is deprecated and will be removed | ||
from the command-line available arguments in the future. | ||
serhiy-storchaka marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
For arguments, if ``deprecated`` is ``True`` then a warning will be | ||
printed to the standard error if the argument is given by user in the | ||
command line parameters:: | ||
serhiy-storchaka marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
>>> import argparse | ||
>>> parser = argparse.ArgumentParser(prog='snake.py') | ||
>>> parser.add_argument('--legs', default=0, deprecated=True) | ||
>>> parser.parse_args([]) | ||
Namespace(legs='0') | ||
>>> parser.parse_args(['--legs', '4']) | ||
snake.py: warning: usage of option '--legs' is deprecated | ||
Namespace(legs='4') | ||
.. versionchanged:: 3.13 | ||
Action classes | ||
^^^^^^^^^^^^^^ | ||
@@ -1842,7 +1873,8 @@ Sub-commands | ||
{foo,bar} additional help | ||
Furthermore, :meth:`~_SubParsersAction.add_parser` supports an additional | ||
*aliases* argument, | ||
which allows multiple strings to refer to the same subparser. This example, | ||
like ``svn``, aliases ``co`` as a shorthand for ``checkout``:: | ||
@@ -1853,6 +1885,20 @@ Sub-commands | ||
>>> parser.parse_args(['co', 'bar']) | ||
Namespace(foo='bar') | ||
:meth:`~_SubParsersAction.add_parser` supports also an additional | ||
*deprecated* argument, which allows to deprecate the subparser. | ||
>>> import argparse | ||
>>> parser = argparse.ArgumentParser(prog='chicken.py') | ||
>>> subparsers = parser.add_subparsers() | ||
>>> run = subparsers.add_parser('run') | ||
>>> fly = subparsers.add_parser('fly', deprecated=True) | ||
>>> parser.parse_args(['fly']) | ||
chicken.py: warning: usage of command 'fly' is deprecated | ||
Namespace() | ||
.. versionadded:: 3.13 | ||
One particularly effective way of handling sub-commands is to combine the use | ||
of the :meth:`add_subparsers` method with calls to :meth:`set_defaults` so | ||
that each subparser knows which Python function it should execute. For | ||
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -154,6 +154,14 @@ ast | ||
possible to obtain an optimized ``AST``. | ||
(Contributed by Irit Katriel in :gh:`108113`). | ||
argrapse | ||
serhiy-storchaka marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
-------- | ||
* Add parameter *deprecated* in methods | ||
:meth:`~argparse.ArgumentParser.add_argument` and :meth:`!add_parser` | ||
which allows to deprecate command-line options, positional arguments and | ||
subcommands. | ||
array | ||
----- | ||
Uh oh!
There was an error while loading.Please reload this page.