Skip to content

Commit cc1cab9

Browse files
Merge branch 'main' into subparser-usage
2 parents d7b156a + 70c27ce commit cc1cab9

File tree

3 files changed

+26
-7
lines changed

3 files changed

+26
-7
lines changed

Lib/argparse.py

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -467,16 +467,12 @@ def _get_actions_usage_parts_with_split(self, actions, groups, opt_count=None):
467467
# produce all arg strings
468468
elif not action.option_strings:
469469
default = self._get_default_metavar_for_positional(action)
470-
part = (
471-
t.summary_action
472-
+ self._format_args(action, default)
473-
+ t.reset
474-
)
475-
470+
part = self._format_args(action, default)
476471
# if it's in a group, strip the outer []
477472
if action in group_actions:
478473
if part[0] == '[' and part[-1] == ']':
479474
part = part[1:-1]
475+
part = t.summary_action + part + t.reset
480476

481477
# produce the first way to invoke the option in brackets
482478
else:

Lib/test/test_argparse.py

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7371,7 +7371,28 @@ def test_argparse_color(self):
73717371
),
73727372
)
73737373

7374-
def test_argparse_color_usage(self):
7374+
def test_argparse_color_mutually_exclusive_group_usage(self):
7375+
parser = argparse.ArgumentParser(color=True, prog="PROG")
7376+
group = parser.add_mutually_exclusive_group()
7377+
group.add_argument('--foo', action='store_true', help='FOO')
7378+
group.add_argument('--spam', help='SPAM')
7379+
group.add_argument('badger', nargs='*', help='BADGER')
7380+
7381+
prog = self.theme.prog
7382+
heading = self.theme.heading
7383+
long = self.theme.summary_long_option
7384+
short = self.theme.summary_short_option
7385+
label = self.theme.summary_label
7386+
pos = self.theme.summary_action
7387+
reset = self.theme.reset
7388+
7389+
self.assertEqual(parser.format_usage(),
7390+
f"{heading}usage: {reset}{prog}PROG{reset} [{short}-h{reset}] "
7391+
f"[{long}--foo{reset} | "
7392+
f"{long}--spam {label}SPAM{reset} | "
7393+
f"{pos}badger ...{reset}]\n")
7394+
7395+
def test_argparse_color_custom_usage(self):
73757396
# Arrange
73767397
parser = argparse.ArgumentParser(
73777398
add_help=False,
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fix usage formatting for positional arguments in mutually exclusive groups in :mod:`argparse`.
2+
in :mod:`argparse`.

0 commit comments

Comments
 (0)