diff options
author | Todd Leonhardt <todd.leonhardt@gmail.com> | 2018-01-17 13:48:13 -0500 |
---|---|---|
committer | Todd Leonhardt <todd.leonhardt@gmail.com> | 2018-01-17 13:48:13 -0500 |
commit | b034f6d2de92a784853ddeef4e51b26148056691 (patch) | |
tree | 2d692d7e37265164410eaebad44759ab50928192 | |
parent | 91bc999d022a486334b9054859e2ef6f03ca9666 (diff) | |
download | cmd2-git-b034f6d2de92a784853ddeef4e51b26148056691.tar.gz |
Improved how new argparse-based decorators provide help
Now "help command_name" and "command_name -h" provide exactly the same text.
The function docstring for the "do_*" command sets and overrides the ArgumentParser "description" if the docstring is not empty.
-rwxr-xr-x | cmd2.py | 51 | ||||
-rwxr-xr-x | examples/argparse_example.py | 2 | ||||
-rw-r--r-- | tests/conftest.py | 2 | ||||
-rw-r--r-- | tests/test_argparse.py | 6 |
4 files changed, 28 insertions, 33 deletions
@@ -288,13 +288,10 @@ def with_argparser_and_unknown_args(argparser): argparser.prog = func.__name__[3:] # put the help message in the method docstring - funcdoc = func.__doc__ - if funcdoc: - funcdoc += '\n' - else: - # if it's None, make it an empty string - funcdoc = '' - cmd_wrapper.__doc__ = '{}{}'.format(funcdoc, argparser.format_help()) + if func.__doc__: + argparser.description = func.__doc__ + + cmd_wrapper.__doc__ = argparser.format_help() return cmd_wrapper return arg_decorator @@ -315,13 +312,10 @@ def with_argument_parser(argparser): argparser.prog = func.__name__[3:] # put the help message in the method docstring - funcdoc = func.__doc__ - if funcdoc: - funcdoc += '\n' - else: - # if it's None, make it an empty string - funcdoc = '' - cmd_wrapper.__doc__ = '{}{}'.format(funcdoc, argparser.format_help()) + if func.__doc__: + argparser.description = func.__doc__ + + cmd_wrapper.__doc__ = argparser.format_help() return cmd_wrapper return arg_decorator @@ -1341,7 +1335,7 @@ class Cmd(cmd.Cmd): else: raise LookupError("Parameter '%s' not supported (type 'show' for list of parameters)." % param) - set_parser = argparse.ArgumentParser(description='show or set value of a parameter') + set_parser = argparse.ArgumentParser() set_parser.add_argument('-a', '--all', action='store_true', help='display read-only settings as well') set_parser.add_argument('-l', '--long', action='store_true', help='describe function of parameter') set_parser.add_argument('settable', nargs='*', help='[param_name] [value]') @@ -1693,13 +1687,11 @@ Paths or arguments that contain spaces must be enclosed in quotes exit_msg = 'Leaving IPython, back to {}'.format(sys.argv[0]) embed(banner1=banner, exit_msg=exit_msg) - history_parser = argparse.ArgumentParser( - description='run, edit, and save previously entered commands', - formatter_class=argparse.RawTextHelpFormatter, - ) + history_parser = argparse.ArgumentParser(formatter_class=argparse.RawTextHelpFormatter) history_parser_group = history_parser.add_mutually_exclusive_group() history_parser_group.add_argument('-r', '--run', action='store_true', help='run selected history items') - history_parser_group.add_argument('-e', '--edit', action='store_true', help='edit and then run selected history items') + history_parser_group.add_argument('-e', '--edit', action='store_true', + help='edit and then run selected history items') history_parser_group.add_argument('-o', '--output-file', metavar='FILE', help='output to file') history_parser.add_argument('-s', '--script', action='store_true', help='script format; no separation lines') _history_arg_help = """empty all history items @@ -1711,8 +1703,8 @@ a..b, a:b, a:, ..b items by indices (inclusive) @with_argument_parser(history_parser) def do_history(self, args): - # If an argument was supplied, then retrieve partial contents of the - # history + """View, run, edit, and save previously entered commands.""" + # If an argument was supplied, then retrieve partial contents of the history cowardly_refuse_to_run = False if args.arg: # If a character indicating a slice is present, retrieve @@ -1735,7 +1727,8 @@ a..b, a:b, a:, ..b items by indices (inclusive) if args.run: if cowardly_refuse_to_run: self.perror("Cowardly refusing to run all previously entered commands.", traceback_war=False) - self.perror("If this is what you want to do, specify '1:' as the range of history.", traceback_war=False) + self.perror("If this is what you want to do, specify '1:' as the range of history.", + traceback_war=False) else: for runme in history: self.pfeedback(runme) @@ -1744,20 +1737,20 @@ a..b, a:b, a:, ..b items by indices (inclusive) elif args.edit: fd, fname = tempfile.mkstemp(suffix='.txt', text=True) with os.fdopen(fd, 'w') as fobj: - for cmd in history: - fobj.write('{}\n'.format(cmd)) + for command in history: + fobj.write('{}\n'.format(command)) try: os.system('"{}" "{}"'.format(self.editor, fname)) self.do_load(fname) - except: + except Exception: raise finally: os.remove(fname) elif args.output_file: try: with open(os.path.expanduser(args.output_file), 'w') as fobj: - for cmd in history: - fobj.write('{}\n'.format(cmd)) + for command in history: + fobj.write('{}\n'.format(command)) plural = 's' if len(history) > 1 else '' self.pfeedback('{} command{} saved to {}'.format(len(history), plural, args.output_file)) except Exception as e: @@ -1770,7 +1763,6 @@ a..b, a:b, a:, ..b items by indices (inclusive) else: self.poutput(hi.pr()) - @with_argument_list def do_edit(self, arglist): """Edit a file or command in a text editor. @@ -1876,7 +1868,6 @@ Script should contain one command per line, just like command would be typed in self._script_dir.append(os.path.dirname(expanded_path)) - @staticmethod def is_text_file(file_path): """ diff --git a/examples/argparse_example.py b/examples/argparse_example.py index ae45411c..48adca52 100755 --- a/examples/argparse_example.py +++ b/examples/argparse_example.py @@ -64,7 +64,7 @@ class CmdLineApp(Cmd): do_say = do_speak # now "say" is a synonym for "speak" do_orate = do_speak # another synonym, but this one takes multi-line input - tag_parser = argparse.ArgumentParser(description='create a html tag') + tag_parser = argparse.ArgumentParser() tag_parser.add_argument('tag', nargs=1, help='tag') tag_parser.add_argument('content', nargs='+', help='content to surround with tag') diff --git a/tests/conftest.py b/tests/conftest.py index 387322b1..021af193 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -21,7 +21,7 @@ edit help history load py pyscript quit set shell shortcuts # Help text for the history command HELP_HISTORY = """usage: history [-h] [-r | -e | -o FILE] [-s] [arg] -run, edit, and save previously entered commands +View, run, edit, and save previously entered commands. positional arguments: arg empty all history items diff --git a/tests/test_argparse.py b/tests/test_argparse.py index fea9bebb..e144514a 100644 --- a/tests/test_argparse.py +++ b/tests/test_argparse.py @@ -140,10 +140,14 @@ def test_argparse_quoted_arguments_posix_multiple(argparse_app): def test_argparse_help_docstring(argparse_app): out = run_cmd(argparse_app, 'help say') - assert out[0] == 'Repeat what you tell me to.' + assert out[0].startswith('usage: say') + assert out[1] == '' + assert out[2] == 'Repeat what you tell me to.' def test_argparse_help_description(argparse_app): out = run_cmd(argparse_app, 'help tag') + assert out[0].startswith('usage: tag') + assert out[1] == '' assert out[2] == 'create a html tag' def test_argparse_prog(argparse_app): |