mirror of
https://github.com/python/cpython.git
synced 2025-12-08 06:10:17 +00:00
GH-64532: Include parent's required optional arguments in subparser usage (#142355)
This commit is contained in:
parent
70c27ce94b
commit
0ed56ed88f
3 changed files with 15 additions and 2 deletions
|
|
@ -2004,14 +2004,16 @@ def add_subparsers(self, **kwargs):
|
||||||
self._subparsers = self._positionals
|
self._subparsers = self._positionals
|
||||||
|
|
||||||
# prog defaults to the usage message of this parser, skipping
|
# prog defaults to the usage message of this parser, skipping
|
||||||
# optional arguments and with no "usage:" prefix
|
# non-required optional arguments and with no "usage:" prefix
|
||||||
if kwargs.get('prog') is None:
|
if kwargs.get('prog') is None:
|
||||||
# Create formatter without color to avoid storing ANSI codes in prog
|
# Create formatter without color to avoid storing ANSI codes in prog
|
||||||
formatter = self.formatter_class(prog=self.prog)
|
formatter = self.formatter_class(prog=self.prog)
|
||||||
formatter._set_color(False)
|
formatter._set_color(False)
|
||||||
positionals = self._get_positional_actions()
|
positionals = self._get_positional_actions()
|
||||||
|
required_optionals = [action for action in self._get_optional_actions()
|
||||||
|
if action.required]
|
||||||
groups = self._mutually_exclusive_groups
|
groups = self._mutually_exclusive_groups
|
||||||
formatter.add_usage(None, positionals, groups, '')
|
formatter.add_usage(None, required_optionals + positionals, groups, '')
|
||||||
kwargs['prog'] = formatter.format_help().strip()
|
kwargs['prog'] = formatter.format_help().strip()
|
||||||
|
|
||||||
# create the parsers action and add it to the positionals list
|
# create the parsers action and add it to the positionals list
|
||||||
|
|
|
||||||
|
|
@ -2770,6 +2770,16 @@ def test_optional_subparsers(self):
|
||||||
ret = parser.parse_args(())
|
ret = parser.parse_args(())
|
||||||
self.assertIsNone(ret.command)
|
self.assertIsNone(ret.command)
|
||||||
|
|
||||||
|
def test_subparser_help_with_parent_required_optional(self):
|
||||||
|
parser = ErrorRaisingArgumentParser(prog='PROG')
|
||||||
|
parser.add_argument('--foo', required=True)
|
||||||
|
parser.add_argument('--bar')
|
||||||
|
subparsers = parser.add_subparsers()
|
||||||
|
parser_sub = subparsers.add_parser('sub')
|
||||||
|
parser_sub.add_argument('arg')
|
||||||
|
self.assertEqual(parser_sub.format_usage(),
|
||||||
|
'usage: PROG --foo FOO sub [-h] arg\n')
|
||||||
|
|
||||||
def test_help(self):
|
def test_help(self):
|
||||||
self.assertEqual(self.parser.format_usage(),
|
self.assertEqual(self.parser.format_usage(),
|
||||||
'usage: PROG [-h] [--foo] bar {1,2,3} ...\n')
|
'usage: PROG [-h] [--foo] bar {1,2,3} ...\n')
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1 @@
|
||||||
|
Subparser help now includes required optional arguments from the parent parser in the usage, making it clearer what arguments are needed to run a subcommand. Patch by Savannah Ostrowski.
|
||||||
Loading…
Add table
Add a link
Reference in a new issue