mirror of
https://github.com/python/cpython.git
synced 2025-12-08 06:10:17 +00:00
gh-142332: Fix usage formatting for positional arguments in mutually exclusive groups in argparse (GH-142333)
This commit is contained in:
parent
5be3405e4e
commit
70c27ce94b
3 changed files with 26 additions and 7 deletions
|
|
@ -467,16 +467,12 @@ def _get_actions_usage_parts_with_split(self, actions, groups, opt_count=None):
|
||||||
# produce all arg strings
|
# produce all arg strings
|
||||||
elif not action.option_strings:
|
elif not action.option_strings:
|
||||||
default = self._get_default_metavar_for_positional(action)
|
default = self._get_default_metavar_for_positional(action)
|
||||||
part = (
|
part = self._format_args(action, default)
|
||||||
t.summary_action
|
|
||||||
+ self._format_args(action, default)
|
|
||||||
+ t.reset
|
|
||||||
)
|
|
||||||
|
|
||||||
# if it's in a group, strip the outer []
|
# if it's in a group, strip the outer []
|
||||||
if action in group_actions:
|
if action in group_actions:
|
||||||
if part[0] == '[' and part[-1] == ']':
|
if part[0] == '[' and part[-1] == ']':
|
||||||
part = part[1:-1]
|
part = part[1:-1]
|
||||||
|
part = t.summary_action + part + t.reset
|
||||||
|
|
||||||
# produce the first way to invoke the option in brackets
|
# produce the first way to invoke the option in brackets
|
||||||
else:
|
else:
|
||||||
|
|
|
||||||
|
|
@ -7361,7 +7361,28 @@ def test_argparse_color(self):
|
||||||
),
|
),
|
||||||
)
|
)
|
||||||
|
|
||||||
def test_argparse_color_usage(self):
|
def test_argparse_color_mutually_exclusive_group_usage(self):
|
||||||
|
parser = argparse.ArgumentParser(color=True, prog="PROG")
|
||||||
|
group = parser.add_mutually_exclusive_group()
|
||||||
|
group.add_argument('--foo', action='store_true', help='FOO')
|
||||||
|
group.add_argument('--spam', help='SPAM')
|
||||||
|
group.add_argument('badger', nargs='*', help='BADGER')
|
||||||
|
|
||||||
|
prog = self.theme.prog
|
||||||
|
heading = self.theme.heading
|
||||||
|
long = self.theme.summary_long_option
|
||||||
|
short = self.theme.summary_short_option
|
||||||
|
label = self.theme.summary_label
|
||||||
|
pos = self.theme.summary_action
|
||||||
|
reset = self.theme.reset
|
||||||
|
|
||||||
|
self.assertEqual(parser.format_usage(),
|
||||||
|
f"{heading}usage: {reset}{prog}PROG{reset} [{short}-h{reset}] "
|
||||||
|
f"[{long}--foo{reset} | "
|
||||||
|
f"{long}--spam {label}SPAM{reset} | "
|
||||||
|
f"{pos}badger ...{reset}]\n")
|
||||||
|
|
||||||
|
def test_argparse_color_custom_usage(self):
|
||||||
# Arrange
|
# Arrange
|
||||||
parser = argparse.ArgumentParser(
|
parser = argparse.ArgumentParser(
|
||||||
add_help=False,
|
add_help=False,
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,2 @@
|
||||||
|
Fix usage formatting for positional arguments in mutually exclusive groups in :mod:`argparse`.
|
||||||
|
in :mod:`argparse`.
|
||||||
Loading…
Add table
Add a link
Reference in a new issue