Index: Lib/optparse.py =================================================================== --- Lib/optparse.py (revision 79952) +++ Lib/optparse.py (working copy) @@ -393,8 +393,36 @@ def format_heading(self, heading): return "%s\n%s\n" % (heading, "=-"[self.level] * len(heading)) + +class OptionsOnlyHelpFormatter(HelpFormatter): + + def __init__(self, indent_increment=0, max_help_position=24, width=None, + short_first=0): + HelpFormatter.__init__(self, indent_increment, max_help_position, width, + short_first) + def format_option(self, option): + if option.takes_value(): + short_opts = ['%s=' % sopt for sopt in option._short_opts] + long_opts = ['%s=' % lopt for lopt in option._long_opts] + else: + short_opts = option._short_opts + long_opts = option._long_opts + opts = ', '.join(short_opts + long_opts) + opts = "%*s%s\n" % (self.current_indent, "", opts) + return opts + + def format_usage(self, usage): + return '' + + def format_heading(self, heading): + return '' + + def format_description(self, description): + return '' + + def _parse_num(val, type): if val[:2].lower() == "0x": # hexadecimal radix = 16 @@ -487,6 +515,7 @@ "count", "callback", "help", + "interface", "version") # The set of actions that involve storing a value somewhere; @@ -807,6 +836,9 @@ elif action == "help": parser.print_help() parser.exit() + elif action == 'interface': + parser.print_interface() + parser.exit() elif action == "version": parser.print_version() parser.exit() @@ -1068,9 +1100,13 @@ def format_help(self, formatter): result = [] if self.description: - result.append(self.format_description(formatter)) + description = self.format_description(formatter) + if description: + result.append(description) if self.option_list: - result.append(self.format_option_help(formatter)) + option_help = self.format_option_help(formatter) + if option_help: + result.append(option_help) return "\n".join(result) @@ -1232,13 +1268,18 @@ self.add_option("-h", "--help", action="help", help=_("show this help message and exit")) + + def _add_interface(self): + self.add_option("--help-options", action="interface", + help=_("show interface of this program")) def _add_version_option(self): self.add_option("--version", action="version", help=_("show program's version number and exit")) - def _populate_option_list(self, option_list, add_help=True): + def _populate_option_list(self, option_list, add_help=True, + add_interface=True): if self.standard_option_list: self.add_options(self.standard_option_list) if option_list: @@ -1247,6 +1288,8 @@ self._add_version_option() if add_help: self._add_help_option() + if add_interface: + self._add_interface() def _init_parsing_state(self): # These are set in parse_args() for the convenience of callbacks. @@ -1633,7 +1676,7 @@ result.append(self.format_option_help(formatter)) result.append(self.format_epilog(formatter)) return "".join(result) - + def print_help(self, file=None): """print_help(file : file = stdout) @@ -1643,7 +1686,21 @@ if file is None: file = sys.stdout file.write(self.format_help()) + + def format_interface(self): + formatter = OptionsOnlyHelpFormatter() + result = [] + if self.option_list: + result.append(OptionContainer.format_option_help(self, formatter)) + for group in self.option_groups: + result.append(group.format_help(formatter)) + return "".join(result) + def print_interface(self, file=None): + if file is None: + file = sys.stdout + file.write(self.format_interface()) + # class OptionParser Index: Lib/argparse.py =================================================================== --- Lib/argparse.py (revision 79952) +++ Lib/argparse.py (working copy) @@ -646,7 +646,23 @@ if action.option_strings or action.nargs in defaulting_nargs: help += ' (default: %(default)s)' return help + +class InterfaceFormatter(HelpFormatter): + + def __init__(self): + super(InterfaceFormatter, self).__init__(None, indent_increment=2, + max_help_position=24, + width=None) + + def _format_action(self, action): + if action.required: + return '' + if action.takes_value(): + strings = [string + '=' for string in action.option_strings] + return ', '.join(strings) + '\n' + else: + return ', '.join(action.option_strings) + '\n' # ===================== # Options and Arguments @@ -766,6 +782,9 @@ self.required = required self.help = help self.metavar = metavar + + def takes_value(self): + return self.nargs != 0 def _get_kwargs(self): names = [ @@ -978,8 +997,21 @@ def __call__(self, parser, namespace, values, option_string=None): parser.print_help() parser.exit() + +class _InterfaceAction(Action): + + def __init__(self, option_strings, dest=SUPPRESS, default=SUPPRESS, + help=None): + super(_InterfaceAction, self).__init__(option_strings=option_strings, + dest=dest, default=default, + nargs=0, help=help) + + def __call__(self, parser, namespace, values, option_string=None): + parser.print_interface() + parser.exit() + class _VersionAction(Action): def __init__(self, @@ -1168,6 +1200,7 @@ self.register('action', 'append_const', _AppendConstAction) self.register('action', 'count', _CountAction) self.register('action', 'help', _HelpAction) + self.register('action', 'interface', _InterfaceAction) self.register('action', 'version', _VersionAction) self.register('action', 'parsers', _SubParsersAction) @@ -1521,7 +1554,8 @@ fromfile_prefix_chars=None, argument_default=None, conflict_handler='error', - add_help=True): + add_help=True, + add_interface=True): if version is not None: import warnings @@ -1548,6 +1582,7 @@ self.formatter_class = formatter_class self.fromfile_prefix_chars = fromfile_prefix_chars self.add_help = add_help + self.add_interface = add_interface add_group = self.add_argument_group self._positionals = add_group(_('positional arguments')) @@ -1565,6 +1600,10 @@ self.add_argument( '-h', '--help', action='help', default=SUPPRESS, help=_('show this help message and exit')) + if self.add_interface: + self.add_argument( + '--help-options', action='interface', default=SUPPRESS, + help=_('show interface of this program')) if self.version: self.add_argument( '-v', '--version', action='version', default=SUPPRESS, @@ -2247,7 +2286,13 @@ # determine help from format above return formatter.format_help() - + + def format_interface(self): + formatter = InterfaceFormatter() + for action_group in self._action_groups: + formatter.add_arguments(action_group._group_actions) + return formatter.format_help() + def format_version(self): import warnings warnings.warn( @@ -2273,6 +2318,11 @@ if file is None: file = _sys.stdout self._print_message(self.format_help(), file) + + def print_interface(self, file=None): + if file is None: + file = _sys.stdout + self._print_message(self.format_interface(), file) def print_version(self, file=None): import warnings Index: Lib/test/test_optparse.py =================================================================== --- Lib/test/test_optparse.py (revision 79952) +++ Lib/test/test_optparse.py (working copy) @@ -526,8 +526,9 @@ self.assertHelp(parser, expected_usage + "\n" + "Options:\n" - " --version show program's version number and exit\n" - " -h, --help show this help message and exit\n") + " --version show program's version number and exit\n" + " -h, --help show this help message and exit\n" + " --help-options show interface of this program\n") finally: sys.argv[:] = save_argv @@ -537,6 +538,7 @@ usage="%prog arg arg") parser.remove_option("-h") parser.remove_option("--version") + parser.remove_option("--help-options") expected_usage = "Usage: thingy arg arg\n" self.assertUsage(parser, expected_usage) self.assertVersion(parser, "thingy 0.1") @@ -551,6 +553,7 @@ Options: -h, --help show this help message and exit + --help-options show interface of this program """ self.file_help = "read from FILE [default: %default]" self.expected_help_file = self.help_prefix + \ @@ -1113,6 +1116,7 @@ # format_help() doesn't crash. parser = OptionParser(usage=SUPPRESS_USAGE) parser.remove_option("-h") + parser.remove_option("--help-options") parser.add_option("-t", "--test", action="callback", callback=lambda: None, type="string", help="foo") @@ -1325,9 +1329,10 @@ def test_conflict_resolve_help(self): self.assertOutput(["-h"], """\ Options: - --verbose increment verbosity - -h, --help show this help message and exit - -v, --version show version + --verbose increment verbosity + -h, --help show this help message and exit + --help-options show interface of this program + -v, --version show version """) def test_conflict_resolve_short_opt(self): @@ -1364,8 +1369,9 @@ def test_conflict_override_help(self): self.assertOutput(["-h"], """\ Options: - -h, --help show this help message and exit - -n, --dry-run dry run mode + -h, --help show this help message and exit + --help-options show interface of this program + -n, --dry-run dry run mode """) def test_conflict_override_args(self): @@ -1384,6 +1390,7 @@ evil spirits that cause trouble and mayhem) --foo=FOO store FOO in the foo list for later fooing -h, --help show this help message and exit + --help-options show interface of this program """ _expected_help_long_opts_first = """\ @@ -1395,6 +1402,7 @@ evil spirits that cause trouble and mayhem) --foo=FOO store FOO in the foo list for later fooing --help, -h show this help message and exit + --help-options show interface of this program """ _expected_help_title_formatter = """\ @@ -1409,6 +1417,7 @@ evil spirits that cause trouble and mayhem) --foo=FOO store FOO in the foo list for later fooing --help, -h show this help message and exit +--help-options show interface of this program """ _expected_help_short_lines = """\ @@ -1422,9 +1431,12 @@ --foo=FOO store FOO in the foo list for later fooing -h, --help show this help message and exit + --help-options show interface of this program """ -class TestHelp(BaseTest): + +class BaseHelpTest(BaseTest): + def setUp(self): self.parser = self.make_parser(80) @@ -1448,6 +1460,9 @@ with support.EnvironmentVarGuard() as env: env['COLUMNS'] = str(columns) return InterceptingOptionParser(option_list=options) + + +class TestHelp(BaseHelpTest): def assertHelpEquals(self, expected_output): save_argv = sys.argv[:] @@ -1487,8 +1502,9 @@ self.parser.add_option("-a", action="store_true", help="ol\u00E9!") expect = """\ Options: - -h, --help show this help message and exit - -a ol\u00E9! + -h, --help show this help message and exit + --help-options show interface of this program + -a ol\u00E9! """ self.assertHelpEquals(expect) @@ -1499,7 +1515,8 @@ ol\u00E9! Options: - -h, --help show this help message and exit + -h, --help show this help message and exit + --help-options show interface of this program """ self.assertHelpEquals(expect) @@ -1527,6 +1544,7 @@ evil spirits that cause trouble and mayhem) --foo=FOO store FOO in the foo list for later fooing -h, --help show this help message and exit + --help-options show interface of this program Dangerous Options: Caution: use of these options is at your own risk. It is believed @@ -1539,8 +1557,49 @@ self.parser.epilog = "Please report bugs to /dev/null." self.assertHelpEquals(expect + "\nPlease report bugs to /dev/null.\n") + +class TestHelpOptions(BaseHelpTest): + def assertHelpOptionsEquals(self, expected_output): + save_argv = sys.argv[:] + try: + # Make optparse believe bar.py is being executed. + sys.argv[0] = os.path.join("foo", "bar.py") + self.assertOutput(["--help-options"], expected_output) + finally: + sys.argv[:] = save_argv + + def test_help_options_basic(self): + expect = """\ +-a= +-b=, --boo= +--foo= +-h, --help +--help-options +""" + self.assertHelpOptionsEquals(expect) + + def test_help_options_groups(self): + + group = OptionGroup( + self.parser, "Dangerous Options", + "Caution: use of these options is at your own risk. " + "It is believed that some of them bite.") + group.add_option("-g", action="store_true", help="Group option.") + self.parser.add_option_group(group) + + expect = """\ +-a= +-b=, --boo= +--foo= +-h, --help +--help-options +-g +""" + self.assertHelpOptionsEquals(expect) + + class TestMatchAbbrev(BaseTest): def test_match_abbrev(self): self.assertEqual(_match_abbrev("--f", Index: Lib/test/test_argparse.py =================================================================== --- Lib/test/test_argparse.py (revision 79952) +++ Lib/test/test_argparse.py (working copy) @@ -416,7 +416,8 @@ class TestOptionalsAlternatePrefixChars(ParserTestCase): """Test an Optional with a double-dash option string""" - parser_signature = Sig(prefix_chars='+:/', add_help=False) + parser_signature = Sig(prefix_chars='+:/', add_help=False, + add_interface=False) argument_signatures = [ Sig('+f', action='store_true'), Sig('::bar'), @@ -1721,39 +1722,41 @@ def test_help(self): self.assertEqual(self.parser.format_usage(), - 'usage: PROG [-h] [--foo] bar {1,2} ...\n') + 'usage: PROG [-h] [--help-options] [--foo] bar {1,2} ...\n') self.assertEqual(self.parser.format_help(), textwrap.dedent('''\ - usage: PROG [-h] [--foo] bar {1,2} ... + usage: PROG [-h] [--help-options] [--foo] bar {1,2} ... main description positional arguments: - bar bar help - {1,2} command help + bar bar help + {1,2} command help optional arguments: - -h, --help show this help message and exit - --foo foo help + -h, --help show this help message and exit + --help-options show interface of this program + --foo foo help ''')) def test_parser_command_help(self): self.assertEqual(self.command_help_parser.format_usage(), - 'usage: PROG [-h] [--foo] bar {1,2} ...\n') + 'usage: PROG [-h] [--help-options] [--foo] bar {1,2} ...\n') self.assertEqual(self.command_help_parser.format_help(), textwrap.dedent('''\ - usage: PROG [-h] [--foo] bar {1,2} ... + usage: PROG [-h] [--help-options] [--foo] bar {1,2} ... main description positional arguments: - bar bar help - {1,2} command help - 1 1 help - 2 2 help + bar bar help + {1,2} command help + 1 1 help + 2 2 help optional arguments: - -h, --help show this help message and exit - --foo foo help + -h, --help show this help message and exit + --help-options show interface of this program + --foo foo help ''')) def test_subparser_title_help(self): @@ -1767,23 +1770,24 @@ parser1 = subparsers.add_parser('1') parser2 = subparsers.add_parser('2') self.assertEqual(parser.format_usage(), - 'usage: PROG [-h] [--foo] bar {1,2} ...\n') + 'usage: PROG [-h] [--help-options] [--foo] bar {1,2} ...\n') self.assertEqual(parser.format_help(), textwrap.dedent('''\ - usage: PROG [-h] [--foo] bar {1,2} ... + usage: PROG [-h] [--help-options] [--foo] bar {1,2} ... main description positional arguments: - bar bar help + bar bar help optional arguments: - -h, --help show this help message and exit - --foo foo help + -h, --help show this help message and exit + --help-options show interface of this program + --foo foo help subcommands: command help - {1,2} additional text + {1,2} additional text ''')) def _test_subparser_help(self, args_str, expected_help): @@ -1798,30 +1802,32 @@ def test_subparser1_help(self): self._test_subparser_help('5.0 1 -h', textwrap.dedent('''\ - usage: PROG bar 1 [-h] [-w W] {a,b,c} + usage: PROG bar 1 [-h] [--help-options] [-w W] {a,b,c} 1 description positional arguments: - {a,b,c} x help + {a,b,c} x help optional arguments: - -h, --help show this help message and exit - -w W w help + -h, --help show this help message and exit + --help-options show interface of this program + -w W w help ''')) def test_subparser2_help(self): self._test_subparser_help('5.0 2 -h', textwrap.dedent('''\ - usage: PROG bar 2 [-h] [-y {1,2,3}] [z [z ...]] + usage: PROG bar 2 [-h] [--help-options] [-y {1,2,3}] [z [z ...]] 2 description positional arguments: - z z help + z z help optional arguments: - -h, --help show this help message and exit - -y {1,2,3} y help + -h, --help show this help message and exit + --help-options show interface of this program + -y {1,2,3} y help ''')) # ============ @@ -1874,26 +1880,31 @@ self.assertRaises(ArgumentParserError, *args, **kwargs) def setUp(self): - self.wxyz_parent = ErrorRaisingArgumentParser(add_help=False) + self.wxyz_parent = ErrorRaisingArgumentParser(add_help=False, + add_interface=False) self.wxyz_parent.add_argument('--w') x_group = self.wxyz_parent.add_argument_group('x') x_group.add_argument('-y') self.wxyz_parent.add_argument('z') - self.abcd_parent = ErrorRaisingArgumentParser(add_help=False) + self.abcd_parent = ErrorRaisingArgumentParser(add_help=False, + add_interface=False) self.abcd_parent.add_argument('a') self.abcd_parent.add_argument('-b') c_group = self.abcd_parent.add_argument_group('c') c_group.add_argument('--d') - self.w_parent = ErrorRaisingArgumentParser(add_help=False) + self.w_parent = ErrorRaisingArgumentParser(add_help=False, + add_interface=False) self.w_parent.add_argument('--w') - self.z_parent = ErrorRaisingArgumentParser(add_help=False) + self.z_parent = ErrorRaisingArgumentParser(add_help=False, + add_interface=False) self.z_parent.add_argument('z') # parents with mutually exclusive groups - self.ab_mutex_parent = ErrorRaisingArgumentParser(add_help=False) + self.ab_mutex_parent = ErrorRaisingArgumentParser(add_help=False, + add_interface=False) group = self.ab_mutex_parent.add_mutually_exclusive_group() group.add_argument('-a', action='store_true') group.add_argument('-b', action='store_true') @@ -1912,7 +1923,8 @@ def test_single_granparent_mutex(self): parents = [self.ab_mutex_parent] - parser = ErrorRaisingArgumentParser(add_help=False, parents=parents) + parser = ErrorRaisingArgumentParser(add_help=False, add_interface=False, + parents=parents) parser = ErrorRaisingArgumentParser(parents=[parser]) self._test_mutex_ab(parser.parse_args) @@ -1992,14 +2004,15 @@ parser = ErrorRaisingArgumentParser(parents=parents) parser_help = parser.format_help() self.assertEqual(parser_help, textwrap.dedent('''\ - usage: {} [-h] [-b B] [--d D] [--w W] [-y Y] a z + usage: {0} [-h] [--help-options] [-b B] [--d D] [--w W] [-y Y] a z positional arguments: a z optional arguments: - -h, --help show this help message and exit + -h, --help show this help message and exit + --help-options show interface of this program -b B --w W @@ -2008,10 +2021,10 @@ x: -y Y - '''.format(self.main_program))) + '''.format(self.main_program, ' ' * len(self.main_program)))) def test_groups_parents(self): - parent = ErrorRaisingArgumentParser(add_help=False) + parent = ErrorRaisingArgumentParser(add_help=False, add_interface=False) g = parent.add_argument_group(title='g', description='gd') g.add_argument('-w') g.add_argument('-x') @@ -2025,10 +2038,11 @@ parser_help = parser.format_help() self.assertEqual(parser_help, textwrap.dedent('''\ - usage: {} [-h] [-w W] [-x X] [-y Y | -z Z] + usage: {} [-h] [--help-options] [-w W] [-x X] [-y Y | -z Z] optional arguments: - -h, --help show this help message and exit + -h, --help show this help message and exit + --help-options show interface of this program -y Y -z Z @@ -2131,17 +2145,18 @@ ] usage_when_not_required = '''\ - usage: PROG [-h] [--bar BAR | --baz [BAZ]] + usage: PROG [-h] [--help-options] [--bar BAR | --baz [BAZ]] ''' usage_when_required = '''\ - usage: PROG [-h] (--bar BAR | --baz [BAZ]) + usage: PROG [-h] [--help-options] (--bar BAR | --baz [BAZ]) ''' help = '''\ optional arguments: - -h, --help show this help message and exit - --bar BAR bar help - --baz [BAZ] baz help + -h, --help show this help message and exit + --help-options show interface of this program + --bar BAR bar help + --baz [BAZ] baz help ''' @@ -2170,21 +2185,22 @@ ] usage_when_not_required = '''\ - usage: PROG [-h] [--abcde ABCDE] [--fghij FGHIJ] + usage: PROG [-h] [--help-options] [--abcde ABCDE] [--fghij FGHIJ] [--klmno KLMNO | --pqrst PQRST] ''' usage_when_required = '''\ - usage: PROG [-h] [--abcde ABCDE] [--fghij FGHIJ] + usage: PROG [-h] [--help-options] [--abcde ABCDE] [--fghij FGHIJ] (--klmno KLMNO | --pqrst PQRST) ''' help = '''\ optional arguments: - -h, --help show this help message and exit - --abcde ABCDE abcde help - --fghij FGHIJ fghij help - --klmno KLMNO klmno help - --pqrst PQRST pqrst help + -h, --help show this help message and exit + --help-options show interface of this program + --abcde ABCDE abcde help + --fghij FGHIJ fghij help + --klmno KLMNO klmno help + --pqrst PQRST pqrst help ''' @@ -2208,16 +2224,17 @@ ] usage_when_not_required = '''\ - usage: PROG [-h] [-y] + usage: PROG [-h] [--help-options] [-y] ''' usage_when_required = '''\ - usage: PROG [-h] -y + usage: PROG [-h] [--help-options] -y ''' help = '''\ optional arguments: - -h, --help show this help message and exit - -y y help + -h, --help show this help message and exit + --help-options show interface of this program + -y y help ''' @@ -2248,12 +2265,13 @@ ] usage_when_required = usage_when_not_required = '''\ - usage: PROG [-h] + usage: PROG [-h] [--help-options] ''' help = '''\ optional arguments: - -h, --help show this help message and exit + -h, --help show this help message and exit + --help-options show interface of this program ''' @@ -2285,20 +2303,21 @@ ] usage_when_not_required = '''\ - usage: PROG [-h] [--foo | --spam SPAM | badger [badger ...]] + usage: PROG [-h] [--help-options] [--foo | --spam SPAM | badger [badger ...]] ''' usage_when_required = '''\ - usage: PROG [-h] (--foo | --spam SPAM | badger [badger ...]) + usage: PROG [-h] [--help-options] (--foo | --spam SPAM | badger [badger ...]) ''' help = '''\ positional arguments: - badger BADGER + badger BADGER optional arguments: - -h, --help show this help message and exit - --foo FOO - --spam SPAM SPAM + -h, --help show this help message and exit + --help-options show interface of this program + --foo FOO + --spam SPAM SPAM ''' @@ -2330,17 +2349,18 @@ ] usage_when_required = usage_when_not_required = '''\ - usage: PROG [-h] [-x] [-a] [-b] [-y] [-c] + usage: PROG [-h] [--help-options] [-x] [-a] [-b] [-y] [-c] ''' help = '''\ optional arguments: - -h, --help show this help message and exit - -x x help - -a a help - -b b help - -y y help - -c c help + -h, --help show this help message and exit + --help-options show interface of this program + -x x help + -a a help + -b b help + -y y help + -c c help ''' @@ -2370,19 +2390,20 @@ ] usage_when_required = usage_when_not_required = '''\ - usage: PROG [-h] [-y] [-b] [-c] x [a] + usage: PROG [-h] [--help-options] [-y] [-b] [-c] x [a] ''' help = '''\ positional arguments: - x x help - a a help + x x help + a a help optional arguments: - -h, --help show this help message and exit - -y y help - -b b help - -c c help + -h, --help show this help message and exit + --help-options show interface of this program + -y y help + -b b help + -c c help ''' # ================================================= @@ -2394,7 +2415,8 @@ def get_parser(self, required=None): parent = super(MEPBase, self).get_parser(required=required) parser = ErrorRaisingArgumentParser( - prog=parent.prog, add_help=False, parents=[parent]) + prog=parent.prog, add_help=False, add_interface=False, + parents=[parent]) return parser @@ -2483,7 +2505,7 @@ parser.parse_args('a'.split())) def test_set_defaults_parents(self): - parent = ErrorRaisingArgumentParser(add_help=False) + parent = ErrorRaisingArgumentParser(add_help=False, add_interface=False) parent.set_defaults(x='foo') parser = ErrorRaisingArgumentParser(parents=[parent]) self.assertEqual(NS(x='foo'), parser.parse_args([])) @@ -2673,21 +2695,22 @@ ] argument_group_signatures = [] usage = '''\ - usage: PROG [-h] [-v] [-x] [--y Y] foo bar + usage: PROG [-h] [--help-options] [-v] [-x] [--y Y] foo bar ''' help = usage + '''\ DESCRIPTION positional arguments: - foo FOO HELP - bar BAR HELP + foo FOO HELP + bar BAR HELP optional arguments: - -h, --help show this help message and exit - -v, --version show program's version number and exit - -x X HELP - --y Y Y HELP + -h, --help show this help message and exit + --help-options show interface of this program + -v, --version show program's version number and exit + -x X HELP + --y Y Y HELP EPILOG ''' @@ -2713,27 +2736,28 @@ Sig('-z', nargs='+', help='Z HELP')]), ] usage = '''\ - usage: PROG [-h] [-v] [-x] [--y Y] [-z Z [Z ...]] foo bar baz + usage: PROG [-h] [--help-options] [-v] [-x] [--y Y] [-z Z [Z ...]] foo bar baz ''' help = usage + '''\ DESCRIPTION positional arguments: - foo FOO HELP - bar BAR HELP + foo FOO HELP + bar BAR HELP optional arguments: - -h, --help show this help message and exit - -v, --version show program's version number and exit - -x X HELP - --y Y Y HELP + -h, --help show this help message and exit + --help-options show interface of this program + -v, --version show program's version number and exit + -x X HELP + --y Y Y HELP GROUP TITLE: GROUP DESCRIPTION - baz BAZ HELP - -z Z [Z ...] Z HELP + baz BAZ HELP + -z Z [Z ...] Z HELP EPILOG ''' @@ -2766,6 +2790,7 @@ optional arguments: -h, --help show this help message and exit + --help-options show interface of this program -x X HELP --y Y Y HELP ''' @@ -2800,7 +2825,7 @@ 'multiple lines')]), ] usage = '''\ - usage: PROG [-h] [-x XX] [-a] yyy + usage: PROG [-h] [--help-options] [-x XX] [-a] yyy ''' help = usage + '''\ @@ -2809,18 +2834,19 @@ lines when wrapped positional arguments: - yyy normal y help + yyy normal y help optional arguments: - -h, --help show this help message and exit - -x XX oddly formatted -x help + -h, --help show this help message and exit + --help-options show interface of this program + -x XX oddly formatted -x help title: oddly formatted group description - -a oddly formatted -a help again, so long that it should \ -be wrapped - over multiple lines + -a oddly formatted -a help again, so long that it \ +should be + wrapped over multiple lines ''' version = '' @@ -2838,7 +2864,7 @@ Sig('-a', action='store_true', help='AHHH HHA' * 10)]), ] usage = '''\ - usage: PROG [-h] [-x XX] [-a] yyy + usage: PROG [-h] [--help-options] [-x XX] [-a] yyy ''' help = usage + '''\ @@ -2847,20 +2873,23 @@ DD DD DD DD D positional arguments: - yyy YH YHYH YHYH YHYH YHYH YHYH YHYH YHYH YHYH YHYH YHYH \ -YHYH YHYH - YHYH YHYH YHYH YHYH YHYH YHYH YHYH YH + yyy YH YHYH YHYH YHYH YHYH YHYH YHYH YHYH YHYH YHYH YHYH \ +YHYH + YHYH YHYH YHYH YHYH YHYH YHYH YHYH YHYH YH optional arguments: - -h, --help show this help message and exit - -x XX XHH HXXHH HXXHH HXXHH HXXHH HXXHH HXXHH HXXHH HXXHH \ + -h, --help show this help message and exit + --help-options show interface of this program + -x XX XHH HXXHH HXXHH HXXHH HXXHH HXXHH HXXHH HXXHH \ HXXHH HXXHH - HXXHH HXXHH HXXHH HXXHH HXXHH HXXHH HXXHH HXXHH HXXHH HX + HXXHH HXXHH HXXHH HXXHH HXXHH HXXHH HXXHH HXXHH \ +HXXHH HXXHH + HX ALPHAS: - -a AHHH HHAAHHH HHAAHHH HHAAHHH HHAAHHH HHAAHHH HHAAHHH \ -HHAAHHH - HHAAHHH HHAAHHH HHA + -a AHHH HHAAHHH HHAAHHH HHAAHHH HHAAHHH HHAAHHH \ +HHAAHHH HHAAHHH + HHAAHHH HHAAHHH HHA ''' version = '' @@ -2896,6 +2925,7 @@ optional arguments: -h, --help show this help message and exit + --help-options show interface of this program -v, --version show program's version number and exit -x XXXXXXXXXXXXXXXXXXXXXXXXX XH XHXH XHXH XHXH XHXH XHXH XHXH XHXH XHXH \ @@ -2939,7 +2969,8 @@ ]) ] usage = '''\ - usage: PROG [-h] [-w W [W ...]] [-x [X [X ...]]] [-y [Y]] [-z Z Z Z] + usage: PROG [-h] [--help-options] [-w W [W ...]] [-x [X [X ...]]] [-y [Y]] + [-z Z Z Z] a b b [c] [d [d ...]] e [e ...] ''' help = usage + '''\ @@ -2951,6 +2982,7 @@ optional arguments: -h, --help show this help message and exit + --help-options show interface of this program -w W [W ...] w -x [X [X ...]] x @@ -2966,7 +2998,7 @@ class TestHelpOnlyUserGroups(HelpTestCase): """Test basic usage messages""" - parser_signature = Sig(prog='PROG', add_help=False) + parser_signature = Sig(prog='PROG', add_help=False, add_interface=False) argument_signatures = [] argument_group_signatures = [ (Sig('xxxx'), [ @@ -3007,7 +3039,7 @@ argument_group_signatures = [] usage = '''\ usage: PPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPP - [-h] [-w W] [-x X] a b + [-h] [--help-options] [-w W] [-x X] a b ''' help = usage + '''\ @@ -3016,7 +3048,8 @@ b optional arguments: - -h, --help show this help message and exit + -h, --help show this help message and exit + --help-options show interface of this program -w W -x X ''' @@ -3038,9 +3071,9 @@ argument_group_signatures = [] usage = '''\ usage: PPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPP - [-h] [-w WWWWWWWWWWWWWWWWWWWWWWWWW] \ -[-x XXXXXXXXXXXXXXXXXXXXXXXXX] - [-y YYYYYYYYYYYYYYYYYYYYYYYYY] [-z ZZZZZZZZZZZZZZZZZZZZZZZZZ] + [-h] [--help-options] [-w WWWWWWWWWWWWWWWWWWWWWWWWW] + [-x XXXXXXXXXXXXXXXXXXXXXXXXX] [-y YYYYYYYYYYYYYYYYYYYYYYYYY] + [-z ZZZZZZZZZZZZZZZZZZZZZZZZZ] a b ''' help = usage + '''\ @@ -3051,6 +3084,7 @@ optional arguments: -h, --help show this help message and exit + --help-options show interface of this program -w WWWWWWWWWWWWWWWWWWWWWWWWW -x XXXXXXXXXXXXXXXXXXXXXXXXX -y YYYYYYYYYYYYYYYYYYYYYYYYY @@ -3062,7 +3096,7 @@ class TestHelpUsageLongProgPositionalsWrap(HelpTestCase): """Test usage messages where the prog is long and the positionals wrap""" - parser_signature = Sig(prog='P' * 60, add_help=False) + parser_signature = Sig(prog='P' * 60, add_help=False, add_interface=False) argument_signatures = [ Sig('a' * 25), Sig('b' * 25), @@ -3099,10 +3133,9 @@ ] argument_group_signatures = [] usage = '''\ - usage: PROG [-h] [-w WWWWWWWWWWWWWWWWWWWWWWWWW] \ -[-x XXXXXXXXXXXXXXXXXXXXXXXXX] - [-y YYYYYYYYYYYYYYYYYYYYYYYYY] \ -[-z ZZZZZZZZZZZZZZZZZZZZZZZZZ] + usage: PROG [-h] [--help-options] [-w WWWWWWWWWWWWWWWWWWWWWWWWW] + [-x XXXXXXXXXXXXXXXXXXXXXXXXX] [-y YYYYYYYYYYYYYYYYYYYYYYYYY] + [-z ZZZZZZZZZZZZZZZZZZZZZZZZZ] a b c ''' help = usage + '''\ @@ -3114,6 +3147,7 @@ optional arguments: -h, --help show this help message and exit + --help-options show interface of this program -w WWWWWWWWWWWWWWWWWWWWWWWWW -x XXXXXXXXXXXXXXXXXXXXXXXXX -y YYYYYYYYYYYYYYYYYYYYYYYYY @@ -3136,7 +3170,7 @@ ] argument_group_signatures = [] usage = '''\ - usage: PROG [-h] [-x X] [-y Y] [-z Z] + usage: PROG [-h] [--help-options] [-x X] [-y Y] [-z Z] aaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbbbbbbbbbbbbb ccccccccccccccccccccccccc ''' @@ -3149,6 +3183,7 @@ optional arguments: -h, --help show this help message and exit + --help-options show interface of this program -x X -y Y -z Z @@ -3170,9 +3205,8 @@ ] argument_group_signatures = [] usage = '''\ - usage: PROG [-h] [-x XXXXXXXXXXXXXXXXXXXXXXXXX] \ -[-y YYYYYYYYYYYYYYYYYYYYYYYYY] - [-z ZZZZZZZZZZZZZZZZZZZZZZZZZ] + usage: PROG [-h] [--help-options] [-x XXXXXXXXXXXXXXXXXXXXXXXXX] + [-y YYYYYYYYYYYYYYYYYYYYYYYYY] [-z ZZZZZZZZZZZZZZZZZZZZZZZZZ] aaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbbbbbbbbbbbbb ccccccccccccccccccccccccc ''' @@ -3185,6 +3219,7 @@ optional arguments: -h, --help show this help message and exit + --help-options show interface of this program -x XXXXXXXXXXXXXXXXXXXXXXXXX -y YYYYYYYYYYYYYYYYYYYYYYYYY -z ZZZZZZZZZZZZZZZZZZZZZZZZZ @@ -3203,14 +3238,14 @@ ] argument_group_signatures = [] usage = '''\ - usage: PROG [-h] [-x XXXXXXXXXXXXXXXXXXXXXXXXX] \ -[-y YYYYYYYYYYYYYYYYYYYYYYYYY] - [-z ZZZZZZZZZZZZZZZZZZZZZZZZZ] + usage: PROG [-h] [--help-options] [-x XXXXXXXXXXXXXXXXXXXXXXXXX] + [-y YYYYYYYYYYYYYYYYYYYYYYYYY] [-z ZZZZZZZZZZZZZZZZZZZZZZZZZ] ''' help = usage + '''\ optional arguments: -h, --help show this help message and exit + --help-options show interface of this program -x XXXXXXXXXXXXXXXXXXXXXXXXX -y YYYYYYYYYYYYYYYYYYYYYYYYY -z ZZZZZZZZZZZZZZZZZZZZZZZZZ @@ -3221,7 +3256,7 @@ class TestHelpUsagePositionalsOnlyWrap(HelpTestCase): """Test usage messages where there are only positionals and they wrap""" - parser_signature = Sig(prog='PROG', add_help=False) + parser_signature = Sig(prog='PROG', add_help=False, add_interface=False) argument_signatures = [ Sig('a' * 25), Sig('b' * 25), @@ -3265,25 +3300,27 @@ ]) ] usage = ('''\ - usage: PROG [-h] [-x X] [-y] [--foo {a,b,c}] [--bar BBB] [-a A] [-b B] + usage: PROG [-h] [--help-options] [-x X] [-y] [--foo {a,b,c}] [--bar BBB] + [-a A] [-b B] spam badger ''') help = usage + '''\ positional arguments: - spam spam PROG None - badger badger PROG 0.5 + spam spam PROG None + badger badger PROG 0.5 optional arguments: - -h, --help show this help message and exit - -x X x PROG None int % - -y y PROG 42 XXX - --foo {a,b,c} foo PROG None a, b, c - --bar BBB bar PROG baz bar + -h, --help show this help message and exit + --help-options show interface of this program + -x X x PROG None int % + -y y PROG 42 XXX + --foo {a,b,c} foo PROG None a, b, c + --bar BBB bar PROG baz bar group: - -a A a PROG None - -b B b PROG -1 + -a A a PROG None + -b B b PROG -1 ''' version = '' @@ -3300,7 +3337,8 @@ help = usage + '''\ optional arguments: - -h, --help show this help message and exit + -h, --help show this help message and exit + --help-options show interface of this program ''' version = '' @@ -3308,7 +3346,7 @@ class TestHelpVariableExpansionNoArguments(HelpTestCase): """Test that variables are expanded properly with no arguments""" - parser_signature = Sig(prog='PROG', add_help=False) + parser_signature = Sig(prog='PROG', add_help=False, add_interface=False) argument_signatures = [] argument_group_signatures = [] usage = ('''\ @@ -3329,11 +3367,12 @@ argument_group_signatures = [] help = '''\ positional arguments: - spam spam help + spam spam help optional arguments: - -h, --help show this help message and exit - --foo FOO foo help + -h, --help show this help message and exit + --help-options show interface of this program + --foo FOO foo help ''' usage = '' version = '' @@ -3342,7 +3381,7 @@ class TestHelpSuppressOptional(HelpTestCase): """Test that optional arguments can be suppressed in help messages""" - parser_signature = Sig(prog='PROG', add_help=False) + parser_signature = Sig(prog='PROG', add_help=False, add_interface=False) argument_signatures = [ Sig('--foo', help=argparse.SUPPRESS), Sig('spam', help='spam help'), @@ -3371,16 +3410,17 @@ (Sig('group'), [Sig('--bar', help=argparse.SUPPRESS)]), ] usage = '''\ - usage: PROG [-h] [--foo FOO] spam + usage: PROG [-h] [--help-options] [--foo FOO] spam ''' help = usage + '''\ positional arguments: - spam spam help + spam spam help optional arguments: - -h, --help show this help message and exit - --foo FOO foo help + -h, --help show this help message and exit + --help-options show interface of this program + --foo FOO foo help ''' version = '' @@ -3395,13 +3435,14 @@ ] argument_group_signatures = [] usage = '''\ - usage: PROG [-h] [--foo FOO] + usage: PROG [-h] [--help-options] [--foo FOO] ''' help = usage + '''\ optional arguments: - -h, --help show this help message and exit - --foo FOO foo help + -h, --help show this help message and exit + --help-options show interface of this program + --foo FOO foo help ''' version = '' @@ -3415,13 +3456,14 @@ ] argument_group_signatures = [] usage = '''\ - usage: PROG [-h] --foo FOO + usage: PROG [-h] [--help-options] --foo FOO ''' help = usage + '''\ optional arguments: - -h, --help show this help message and exit - --foo FOO foo help + -h, --help show this help message and exit + --help-options show interface of this program + --foo FOO foo help ''' version = '' @@ -3429,7 +3471,8 @@ class TestHelpAlternatePrefixChars(HelpTestCase): """Test that options display with different prefix characters""" - parser_signature = Sig(prog='PROG', prefix_chars='^;', add_help=False) + parser_signature = Sig(prog='PROG', prefix_chars='^;', add_help=False, + add_interface=False) argument_signatures = [ Sig('^^foo', action='store_true', help='foo help'), Sig(';b', ';;bar', help='bar help'), @@ -3450,7 +3493,7 @@ class TestHelpNoHelpOptional(HelpTestCase): """Test that the --help argument can be suppressed help messages""" - parser_signature = Sig(prog='PROG', add_help=False) + parser_signature = Sig(prog='PROG', add_help=False, add_interface=False) argument_signatures = [ Sig('--foo', help='foo help'), Sig('spam', help='spam help'), @@ -3480,17 +3523,18 @@ ] argument_group_signatures = [] usage = '''\ - usage: PROG [-h] [-v] [--foo FOO] spam + usage: PROG [-h] [--help-options] [-v] [--foo FOO] spam ''' help = usage + '''\ positional arguments: - spam spam help + spam spam help optional arguments: - -h, --help show this help message and exit - -v, --version show program's version number and exit - --foo FOO foo help + -h, --help show this help message and exit + --help-options show interface of this program + -v, --version show program's version number and exit + --foo FOO foo help ''' version = '''\ 1.0 @@ -3507,7 +3551,7 @@ ] argument_group_signatures = [] usage = '''\ - usage: PROG [-h] [--foo FOO] spam + usage: PROG [-h] [--help-options] [--foo FOO] spam ''' help = usage + '''\ @@ -3515,7 +3559,8 @@ spam optional arguments: - -h, --help show this help message and exit + -h, --help show this help message and exit + --help-options show interface of this program --foo FOO ''' version = '' @@ -3533,13 +3578,14 @@ ] argument_group_signatures = [] usage = '''\ - usage: PROG [-h] [-w W1 [W2 ...]] [-x [X1 [X2 ...]]] [-y Y1 Y2 Y3] \ -[-z [Z1]] + usage: PROG [-h] [--help-options] [-w W1 [W2 ...]] [-x [X1 [X2 ...]]] + [-y Y1 Y2 Y3] [-z [Z1]] ''' help = usage + '''\ optional arguments: -h, --help show this help message and exit + --help-options show interface of this program -w W1 [W2 ...] w -x [X1 [X2 ...]] x -y Y1 Y2 Y3 y @@ -3570,7 +3616,7 @@ [Sig('--bar', help='bar help')]), ] usage = '''\ - usage: PROG [-h] [--foo FOO] [--bar BAR] spam + usage: PROG [-h] [--help-options] [--foo FOO] [--bar BAR] spam ''' help = usage + '''\ @@ -3580,19 +3626,20 @@ here positional arguments: - spam spam help + spam spam help optional arguments: - -h, --help show this help message and exit - --foo FOO foo help should also - appear as given here + -h, --help show this help message and exit + --help-options show interface of this program + --foo FOO foo help should also + appear as given here title: This text should be indented exactly like it is here - --bar BAR bar help + --bar BAR bar help ''' version = '' @@ -3619,7 +3666,7 @@ [Sig('--bar', help='bar help')]), ] usage = '''\ - usage: PROG [-h] [--foo FOO] [--bar BAR] spam + usage: PROG [-h] [--help-options] [--foo FOO] [--bar BAR] spam ''' help = usage + '''\ @@ -3629,18 +3676,19 @@ here positional arguments: - spam spam help + spam spam help optional arguments: - -h, --help show this help message and exit - --foo FOO foo help should not retain this odd formatting + -h, --help show this help message and exit + --help-options show interface of this program + --foo FOO foo help should not retain this odd formatting title: This text should be indented exactly like it is here - --bar BAR bar help + --bar BAR bar help ''' version = '' @@ -3663,25 +3711,27 @@ [Sig('--baz', type=int, default=42, help='baz help')]), ] usage = '''\ - usage: PROG [-h] [--foo FOO] [--bar] [--baz BAZ] spam [badger] + usage: PROG [-h] [--help-options] [--foo FOO] [--bar] [--baz BAZ] + spam [badger] ''' help = usage + '''\ description positional arguments: - spam spam help - badger badger help (default: wooden) + spam spam help + badger badger help (default: wooden) optional arguments: - -h, --help show this help message and exit - --foo FOO foo help - oh and by the way, None - --bar bar help (default: False) + -h, --help show this help message and exit + --help-options show interface of this program + --foo FOO foo help - oh and by the way, None + --bar bar help (default: False) title: description - --baz BAZ baz help (default: 42) + --baz BAZ baz help (default: 42) ''' version = '' @@ -3872,20 +3922,22 @@ parser.add_argument('-x', help='OLD X') parser.add_argument('-x', help='NEW X') self.assertEqual(parser.format_help(), textwrap.dedent('''\ - usage: PROG [-h] [-x X] + usage: PROG [-h] [--help-options] [-x X] optional arguments: - -h, --help show this help message and exit - -x X NEW X + -h, --help show this help message and exit + --help-options show interface of this program + -x X NEW X ''')) parser.add_argument('--spam', metavar='OLD_SPAM') parser.add_argument('--spam', metavar='NEW_SPAM') self.assertEqual(parser.format_help(), textwrap.dedent('''\ - usage: PROG [-h] [-x X] [--spam NEW_SPAM] + usage: PROG [-h] [--help-options] [-x X] [--spam NEW_SPAM] optional arguments: -h, --help show this help message and exit + --help-options show interface of this program -x X NEW X --spam NEW_SPAM ''')) @@ -4103,7 +4155,8 @@ def spam(string): raise argparse.ArgumentTypeError('spam!') - parser = ErrorRaisingArgumentParser(prog='PROG', add_help=False) + parser = ErrorRaisingArgumentParser(prog='PROG', add_help=False, + add_interface=False) parser.add_argument('x', type=spam) try: parser.parse_args(['XXX'])