Skip to content

Commit a0ed99b

Browse files
brandtbucherrhettinger
authored andcommitted
bpo-38438: Simplify argparse "star nargs" usage. (GH-17106)
1 parent 84ac437 commit a0ed99b

File tree

6 files changed

+20
-15
lines changed

6 files changed

+20
-15
lines changed

Doc/library/argparse.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -449,7 +449,7 @@ default values to each of the argument help messages::
449449
>>> parser.add_argument('--foo', type=int, default=42, help='FOO!')
450450
>>> parser.add_argument('bar', nargs='*', default=[1, 2, 3], help='BAR!')
451451
>>> parser.print_help()
452-
usage: PROG [-h] [--foo FOO] [bar [bar ...]]
452+
usage: PROG [-h] [--foo FOO] [bar ...]
453453

454454
positional arguments:
455455
bar BAR! (default: [1, 2, 3])

Doc/library/pdb.rst

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -296,20 +296,20 @@ by the local file.
296296
Temporary breakpoint, which is removed automatically when it is first hit.
297297
The arguments are the same as for :pdbcmd:`break`.
298298

299-
.. pdbcommand:: cl(ear) [filename:lineno | bpnumber [bpnumber ...]]
299+
.. pdbcommand:: cl(ear) [filename:lineno | bpnumber ...]
300300

301301
With a *filename:lineno* argument, clear all the breakpoints at this line.
302302
With a space separated list of breakpoint numbers, clear those breakpoints.
303303
Without argument, clear all breaks (but first ask confirmation).
304304

305-
.. pdbcommand:: disable [bpnumber [bpnumber ...]]
305+
.. pdbcommand:: disable [bpnumber ...]
306306

307307
Disable the breakpoints given as a space separated list of breakpoint
308308
numbers. Disabling a breakpoint means it cannot cause the program to stop
309309
execution, but unlike clearing a breakpoint, it remains in the list of
310310
breakpoints and can be (re-)enabled.
311311

312-
.. pdbcommand:: enable [bpnumber [bpnumber ...]]
312+
.. pdbcommand:: enable [bpnumber ...]
313313

314314
Enable the breakpoints specified.
315315

Lib/argparse.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -591,7 +591,11 @@ def _format_args(self, action, default_metavar):
591591
elif action.nargs == OPTIONAL:
592592
result = '[%s]' % get_metavar(1)
593593
elif action.nargs == ZERO_OR_MORE:
594-
result = '[%s [%s ...]]' % get_metavar(2)
594+
metavar = get_metavar(1)
595+
if len(metavar) == 2:
596+
result = '[%s [%s ...]]' % metavar
597+
else:
598+
result = '[%s ...]' % metavar
595599
elif action.nargs == ONE_OR_MORE:
596600
result = '%s [%s ...]' % get_metavar(2)
597601
elif action.nargs == REMAINDER:

Lib/pydoc_data/topics.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3903,7 +3903,7 @@
39033903
'is\n'
39043904
' first hit. The arguments are the same as for "break".\n'
39053905
'\n'
3906-
'cl(ear) [filename:lineno | bpnumber [bpnumber ...]]\n'
3906+
'cl(ear) [filename:lineno | bpnumber ...]\n'
39073907
'\n'
39083908
' With a *filename:lineno* argument, clear all the breakpoints '
39093909
'at\n'
@@ -3913,7 +3913,7 @@
39133913
'first\n'
39143914
' ask confirmation).\n'
39153915
'\n'
3916-
'disable [bpnumber [bpnumber ...]]\n'
3916+
'disable [bpnumber ...]\n'
39173917
'\n'
39183918
' Disable the breakpoints given as a space separated list of\n'
39193919
' breakpoint numbers. Disabling a breakpoint means it cannot '
@@ -3922,7 +3922,7 @@
39223922
'breakpoint, it\n'
39233923
' remains in the list of breakpoints and can be (re-)enabled.\n'
39243924
'\n'
3925-
'enable [bpnumber [bpnumber ...]]\n'
3925+
'enable [bpnumber ...]\n'
39263926
'\n'
39273927
' Enable the breakpoints specified.\n'
39283928
'\n'

Lib/test/test_argparse.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2163,7 +2163,7 @@ def test_subparser1_help(self):
21632163

21642164
def test_subparser2_help(self):
21652165
self._test_subparser_help('5.0 2 -h', textwrap.dedent('''\
2166-
usage: PROG bar 2 [-h] [-y {1,2,3}] [z [z ...]]
2166+
usage: PROG bar 2 [-h] [-y {1,2,3}] [z ...]
21672167
21682168
2 description
21692169
@@ -2697,10 +2697,10 @@ def get_parser(self, required):
26972697
]
26982698

26992699
usage_when_not_required = '''\
2700-
usage: PROG [-h] [--foo | --spam SPAM | badger [badger ...]]
2700+
usage: PROG [-h] [--foo | --spam SPAM | badger ...]
27012701
'''
27022702
usage_when_required = '''\
2703-
usage: PROG [-h] (--foo | --spam SPAM | badger [badger ...])
2703+
usage: PROG [-h] (--foo | --spam SPAM | badger ...)
27042704
'''
27052705
help = '''\
27062706
@@ -3494,11 +3494,11 @@ class TestHelpUsage(HelpTestCase):
34943494
])
34953495
]
34963496
usage = '''\
3497-
usage: PROG [-h] [-w W [W ...]] [-x [X [X ...]]] [--foo | --no-foo]
3497+
usage: PROG [-h] [-w W [W ...]] [-x [X ...]] [--foo | --no-foo]
34983498
[--bar | --no-bar]
34993499
[-f | --foobar | --no-foobar | --barfoo | --no-barfoo] [-y [Y]]
35003500
[-z Z Z Z]
3501-
a b b [c] [d [d ...]] e [e ...]
3501+
a b b [c] [d ...] e [e ...]
35023502
'''
35033503
help = usage + '''\
35043504
@@ -3510,7 +3510,7 @@ class TestHelpUsage(HelpTestCase):
35103510
optional arguments:
35113511
-h, --help show this help message and exit
35123512
-w W [W ...] w
3513-
-x [X [X ...]] x
3513+
-x [X ...] x
35143514
--foo, --no-foo Whether to foo
35153515
--bar, --no-bar Whether to bar (default: True)
35163516
-f, --foobar, --no-foobar, --barfoo, --no-barfoo
@@ -5113,7 +5113,7 @@ def test_nargs_zeroormore_metavar_length0(self):
51135113
self.do_test_exception(nargs="*", metavar=tuple())
51145114

51155115
def test_nargs_zeroormore_metavar_length1(self):
5116-
self.do_test_exception(nargs="*", metavar=("1",))
5116+
self.do_test_no_exception(nargs="*", metavar=("1",))
51175117

51185118
def test_nargs_zeroormore_metavar_length2(self):
51195119
self.do_test_no_exception(nargs="*", metavar=("1", "2"))
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Simplify the :mod:`argparse` usage message for ``nargs="*"``.

0 commit comments

Comments
 (0)