@@ -777,6 +777,8 @@ The add_argument() method
777
777
* dest _ - The name of the attribute to be added to the object returned by
778
778
:meth: `parse_args `.
779
779
780
+ * deprecated _ - Whether or not the usage of the argument is deprecated.
781
+
780
782
The following sections describe how each of these are used.
781
783
782
784
@@ -1439,6 +1441,35 @@ behavior::
1439
1441
>>> parser.parse_args('--foo XXX'.split())
1440
1442
Namespace(bar='XXX')
1441
1443
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
+
1442
1473
Action classes
1443
1474
^^^^^^^^^^^^^^
1444
1475
@@ -1842,7 +1873,8 @@ Sub-commands
1842
1873
1843
1874
{foo,bar} additional help
1844
1875
1845
- Furthermore, ``add_parser `` supports an additional ``aliases `` argument,
1876
+ Furthermore, :meth: `~_SubParsersAction.add_parser ` supports an additional
1877
+ *aliases * argument,
1846
1878
which allows multiple strings to refer to the same subparser. This example,
1847
1879
like ``svn ``, aliases ``co `` as a shorthand for ``checkout ``::
1848
1880
@@ -1853,6 +1885,20 @@ Sub-commands
1853
1885
>>> parser.parse_args(['co', 'bar'])
1854
1886
Namespace(foo='bar')
1855
1887
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
+
1856
1902
One particularly effective way of handling sub-commands is to combine the use
1857
1903
of the :meth: `add_subparsers ` method with calls to :meth: `set_defaults ` so
1858
1904
that each subparser knows which Python function it should execute. For
0 commit comments