diff -r d958c74ddb0c -r b48113329f02 Lib/argparse.py --- a/Lib/argparse.py Mon Apr 13 16:44:05 2015 -0500 +++ b/Lib/argparse.py Wed Apr 15 12:08:38 2015 -0400 @@ -483,6 +483,9 @@ return self._fill_text(text, text_width, indent) + '\n\n' def _format_action(self, action): + if action.help is SUPPRESS: + return '' + # determine the required width and the entry label help_position = min(self._action_max_length + 2, self._max_help_position) @@ -557,7 +560,14 @@ if action.metavar is not None: result = action.metavar elif action.choices is not None: - choice_strs = [str(choice) for choice in action.choices] + if isinstance(action.choices, _collections.Mapping): + choice_strs = [] + for choice, subaction in action.choices.items(): + if not getattr(subaction, '_is_help_suppressed', False): + choice_strs.append(str(choice)) + else: + choice_strs = [str(choice) for choice in action.choices] + result = '{%s}' % ','.join(choice_strs) else: result = default_metavar @@ -1084,6 +1094,7 @@ aliases = kwargs.pop('aliases', ()) # create a pseudo-action to hold the choice help + help = None if 'help' in kwargs: help = kwargs.pop('help') choice_action = self._ChoicesPseudoAction(name, aliases, help) @@ -1091,6 +1102,8 @@ # create the parser and add it to the map parser = self._parser_class(**kwargs) + if help is SUPPRESS: + parser._is_help_suppressed = True self._name_parser_map[name] = parser # make parser available under aliases also @@ -1629,6 +1642,7 @@ self._positionals = add_group(_('positional arguments')) self._optionals = add_group(_('optional arguments')) self._subparsers = None + self._is_help_suppressed = False # register types def identity(string): diff -r d958c74ddb0c -r b48113329f02 Lib/test/test_argparse.py --- a/Lib/test/test_argparse.py Mon Apr 13 16:44:05 2015 -0500 +++ b/Lib/test/test_argparse.py Wed Apr 15 12:08:38 2015 -0400 @@ -2013,6 +2013,58 @@ {1,2} additional text ''')) + def test_subparser_help_suppressed(self): + parser = argparse.ArgumentParser(prog='PROG', + description='main description') + subparsers = parser.add_subparsers(title='subcommands', + description='subcommands description', + help='subcommands help') + parser1 = subparsers.add_parser('1', help='help for command 1') + parser2 = subparsers.add_parser('2', help='help for command 2') + parser3 = subparsers.add_parser('3', aliases=('3alias',), help=argparse.SUPPRESS) + parser4 = subparsers.add_parser('4', help=argparse.SUPPRESS) + + expected_usage = 'usage: PROG [-h] {1,2} ...\n' + self.assertEqual(parser.format_usage(), expected_usage) + self.assertEqual(parser.format_help(), expected_usage + textwrap.dedent('''\ + + main description + + optional arguments: + -h, --help show this help message and exit + + subcommands: + subcommands description + + {1,2} subcommands help + 1 help for command 1 + 2 help for command 2 + ''')) + + def test_all_subparsers_help_suppressed(self): + parser = argparse.ArgumentParser(prog='PROG', + description='main description') + subparsers = parser.add_subparsers(title='subcommands', + description='subcommands description', + help='subcommands help') + parser1 = subparsers.add_parser('1', help=argparse.SUPPRESS) + parser2 = subparsers.add_parser('2', aliases=('2alias',), help=argparse.SUPPRESS) + + expected_usage = 'usage: PROG [-h] {} ...\n' + self.assertEqual(parser.format_usage(), expected_usage) + self.assertEqual(parser.format_help(), expected_usage + textwrap.dedent('''\ + + main description + + optional arguments: + -h, --help show this help message and exit + + subcommands: + subcommands description + + {} subcommands help + ''')) + def _test_subparser_help(self, args_str, expected_help): with self.assertRaises(ArgumentParserError) as cm: self.parser.parse_args(args_str.split())