Skip to content

Commit 0244cd8

Browse files
pythongh-18208: Support deprecation of options, arguments and subcommands in argparse
1 parent ac10947 commit 0244cd8

File tree

5 files changed

+263
-27
lines changed

5 files changed

+263
-27
lines changed

Doc/library/argparse.rst

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -777,6 +777,8 @@ The add_argument() method
777777
* dest_ - The name of the attribute to be added to the object returned by
778778
:meth:`parse_args`.
779779

780+
* deprecated_ - Whether or not the usage of the argument is deprecated.
781+
780782
The following sections describe how each of these are used.
781783

782784

@@ -1439,6 +1441,35 @@ behavior::
14391441
>>> parser.parse_args('--foo XXX'.split())
14401442
Namespace(bar='XXX')
14411443

1444+
1445+
.. _deprecated::
1446+
1447+
deprecated
1448+
^^^^^^^^^^
1449+
1450+
During projects lifecycle some arguments could be removed from the
1451+
command line, before removing these arguments definitively you would inform
1452+
your user that arguments are deprecated and will be removed.
1453+
The ``deprecated`` keyword argument of
1454+
:meth:`~ArgumentParser.add_argument`, whose value default to ``False``,
1455+
specifies if the argument is deprecated and will be removed
1456+
from the command-line available arguments in the future.
1457+
For arguments, if ``deprecated`` is ``True`` then a warning will be
1458+
printed to the standard error if the argument is given by user in the
1459+
command line parameters::
1460+
1461+
>>> import argparse
1462+
>>> parser = argparse.ArgumentParser(prog='snake.py')
1463+
>>> parser.add_argument('--legs', default=0, deprecated=True)
1464+
>>> parser.parse_args([])
1465+
Namespace(legs='0')
1466+
>>> parser.parse_args(['--legs', '4'])
1467+
snake.py: warning: usage of option '--legs' is deprecated
1468+
Namespace(legs='4')
1469+
1470+
.. versionchanged:: 3.13
1471+
1472+
14421473
Action classes
14431474
^^^^^^^^^^^^^^
14441475

@@ -1842,7 +1873,8 @@ Sub-commands
18421873

18431874
{foo,bar} additional help
18441875

1845-
Furthermore, ``add_parser`` supports an additional ``aliases`` argument,
1876+
Furthermore, :meth:`~_SubParsersAction.add_parser` supports an additional
1877+
*aliases* argument,
18461878
which allows multiple strings to refer to the same subparser. This example,
18471879
like ``svn``, aliases ``co`` as a shorthand for ``checkout``::
18481880

@@ -1853,6 +1885,20 @@ Sub-commands
18531885
>>> parser.parse_args(['co', 'bar'])
18541886
Namespace(foo='bar')
18551887

1888+
:meth:`~_SubParsersAction.add_parser` supports also an additional
1889+
*deprecated* argument, which allows to deprecate the subparser.
1890+
1891+
>>> import argparse
1892+
>>> parser = argparse.ArgumentParser(prog='chicken.py')
1893+
>>> subparsers = parser.add_subparsers()
1894+
>>> run = subparsers.add_parser('run')
1895+
>>> fly = subparsers.add_parser('fly', deprecated=True)
1896+
>>> parser.parse_args(['fly'])
1897+
chicken.py: warning: usage of command 'fly' is deprecated
1898+
Namespace()
1899+
1900+
.. versionadded:: 3.13
1901+
18561902
One particularly effective way of handling sub-commands is to combine the use
18571903
of the :meth:`add_subparsers` method with calls to :meth:`set_defaults` so
18581904
that each subparser knows which Python function it should execute. For

Doc/whatsnew/3.13.rst

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,14 @@ ast
154154
possible to obtain an optimized ``AST``.
155155
(Contributed by Irit Katriel in :gh:`108113`).
156156

157+
argrapse
158+
--------
159+
160+
* Add parameter *deprecated* in methods
161+
:meth:`~argparse.ArgumentParser.add_argument` and :meth:`!add_parser`
162+
which allows to deprecate command-line options, positional arguments and
163+
subcommands.
164+
157165
array
158166
-----
159167

0 commit comments

Comments
 (0)