summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKevin Van Brunt <kmvanbrunt@gmail.com>2018-09-26 17:12:04 -0400
committerKevin Van Brunt <kmvanbrunt@gmail.com>2018-09-26 17:12:04 -0400
commit32512dc80da639b621dcac26467c5b1e75db2af0 (patch)
tree3d0195970d451ede5fb91260cecc9f2b4df1ddc3
parent58ba2390dc823b95b8d97f67d518b4a50e22be89 (diff)
downloadcmd2-git-32512dc80da639b621dcac26467c5b1e75db2af0.tar.gz
Converted help to argparse
-rw-r--r--cmd2/cmd2.py43
-rw-r--r--tests/conftest.py2
-rw-r--r--tests/test_argparse.py2
-rw-r--r--tests/test_cmd2.py2
-rw-r--r--tests/test_completion.py2
-rw-r--r--tests/test_pyscript.py8
6 files changed, 34 insertions, 25 deletions
diff --git a/cmd2/cmd2.py b/cmd2/cmd2.py
index d0a055d3..fe1cb560 100644
--- a/cmd2/cmd2.py
+++ b/cmd2/cmd2.py
@@ -124,9 +124,10 @@ HELP_CATEGORY = 'help_category'
HELP_SUMMARY = 'help_summary'
INTERNAL_COMMAND_EPILOG = ("Notes:\n"
- " This command is for internal use only and should never be called from the\n"
+ " This command is for internal use and is not intended to be called from the\n"
" command line.")
+
def categorize(func: Union[Callable, Iterable], category: str) -> None:
"""Categorize a function.
@@ -2273,7 +2274,7 @@ class Cmd(cmd.Cmd):
self.aliases.clear()
self.poutput("All aliases deleted")
elif not args.name:
- self.do_help(['alias', 'delete'])
+ self.do_help('alias delete')
else:
# Get rid of duplicates
aliases_to_delete = utils.remove_duplicates(args.name)
@@ -2462,7 +2463,7 @@ class Cmd(cmd.Cmd):
self.macros.clear()
self.poutput("All macros deleted")
elif not args.name:
- self.do_help(['macro', 'delete'])
+ self.do_help('macro delete')
else:
# Get rid of duplicates
macros_to_delete = utils.remove_duplicates(args.name)
@@ -2585,28 +2586,37 @@ class Cmd(cmd.Cmd):
# No subcommand was provided, so call help
self.macro_parser.print_help()
- @with_argument_list
- def do_help(self, arglist: List[str]) -> None:
- """ List available commands with "help" or detailed help with "help cmd" """
- if not arglist or (len(arglist) == 1 and arglist[0] in ('--verbose', '-v')):
- verbose = len(arglist) == 1 and arglist[0] in ('--verbose', '-v')
- self._help_menu(verbose)
- else:
+ help_parser = ACArgumentParser()
+ help_parser.add_argument('command', help="command to retrieve help for", nargs="?")
+ help_parser.add_argument('subcommands', help="used to retrieve help on a specific subcommand",
+ nargs=argparse.REMAINDER)
+ help_parser.add_argument('-v', '--verbose', action='store_true',
+ help="print a list of all commands with descriptions of each")
+
+ @with_argparser(help_parser)
+ def do_help(self, args: argparse.Namespace) -> None:
+ """List available commands or provide detailed help for a specific command"""
+ if not args.command or args.verbose:
+ self._help_menu(args.verbose)
+
+ elif args.command:
# Getting help for a specific command
- funcname = self._func_named(arglist[0])
+ funcname = self._func_named(args.command)
if funcname:
# Check to see if this function was decorated with an argparse ArgumentParser
func = getattr(self, funcname)
if hasattr(func, 'argparser'):
completer = AutoCompleter(getattr(func, 'argparser'), cmd2_app=self)
- self.poutput(completer.format_help(arglist))
+ tokens = [args.command]
+ tokens.extend(args.subcommands)
+ self.poutput(completer.format_help(tokens))
else:
# No special behavior needed, delegate to cmd base class do_help()
- cmd.Cmd.do_help(self, funcname[3:])
+ super().do_help(args.command)
else:
# This could be a help topic
- cmd.Cmd.do_help(self, arglist[0])
+ super().do_help(args.command)
def _help_menu(self, verbose: bool=False) -> None:
"""Show a list of commands which help can be displayed for.
@@ -2935,8 +2945,7 @@ class Cmd(cmd.Cmd):
sys.displayhook = sys.__displayhook__
sys.excepthook = sys.__excepthook__
- py_description = "Invoke Python command or shell"
- py_parser = ACArgumentParser(description=py_description)
+ py_parser = ACArgumentParser()
py_parser.add_argument('command', help="command to run", nargs='?')
py_parser.add_argument('remainder', help="remainder of command", nargs=argparse.REMAINDER)
@@ -3109,7 +3118,7 @@ Paths or arguments that contain spaces must be enclosed in quotes
"""
if not arglist:
self.perror("pyscript command requires at least 1 argument ...", traceback_war=False)
- self.do_help(['pyscript'])
+ self.do_help('pyscript')
return
script_path = os.path.expanduser(arglist[0])
diff --git a/tests/conftest.py b/tests/conftest.py
index b6f8587f..4083c50e 100644
--- a/tests/conftest.py
+++ b/tests/conftest.py
@@ -38,7 +38,7 @@ Documented commands (type help <topic>):
================================================================================
alias Manage aliases
edit Edit a file in a text editor
-help List available commands with "help" or detailed help with "help cmd"
+help List available commands or provide detailed help for a specific command
history View, run, edit, save, or clear previously entered commands
load Run commands in script file that is encoded as either ASCII or UTF-8 text
macro Manage macros
diff --git a/tests/test_argparse.py b/tests/test_argparse.py
index 0939859a..fdd16bcc 100644
--- a/tests/test_argparse.py
+++ b/tests/test_argparse.py
@@ -217,7 +217,7 @@ class SubcommandApp(cmd2.Cmd):
func(self, args)
else:
# No subcommand was provided, so call help
- self.do_help(['base'])
+ self.do_help('base')
@pytest.fixture
def subcommand_app():
diff --git a/tests/test_cmd2.py b/tests/test_cmd2.py
index 1b31854a..8011ba30 100644
--- a/tests/test_cmd2.py
+++ b/tests/test_cmd2.py
@@ -1251,7 +1251,7 @@ diddly This command does diddly
Other
================================================================================
alias Manage aliases
-help List available commands with "help" or detailed help with "help cmd"
+help List available commands or provide detailed help for a specific command
history View, run, edit, save, or clear previously entered commands
load Run commands in script file that is encoded as either ASCII or UTF-8 text
macro Manage macros
diff --git a/tests/test_completion.py b/tests/test_completion.py
index 40299954..945f68ca 100644
--- a/tests/test_completion.py
+++ b/tests/test_completion.py
@@ -835,7 +835,7 @@ class SubcommandsWithUnknownExample(cmd2.Cmd):
func(self, args)
else:
# No subcommand was provided, so call help
- self.do_help(['base'])
+ self.do_help('base')
@pytest.fixture
diff --git a/tests/test_pyscript.py b/tests/test_pyscript.py
index c7769c7a..36e48598 100644
--- a/tests/test_pyscript.py
+++ b/tests/test_pyscript.py
@@ -18,16 +18,16 @@ class PyscriptExample(Cmd):
def _do_media_movies(self, args) -> None:
if not args.command:
- self.do_help(['media movies'])
+ self.do_help('media movies')
else:
self.poutput('media movies ' + str(args.__dict__))
def _do_media_shows(self, args) -> None:
if not args.command:
- self.do_help(['media shows'])
+ self.do_help('media shows')
if not args.command:
- self.do_help(['media shows'])
+ self.do_help('media shows')
else:
self.poutput('media shows ' + str(args.__dict__))
@@ -72,7 +72,7 @@ class PyscriptExample(Cmd):
func(self, args)
else:
# No subcommand was provided, so call help
- self.do_help(['media'])
+ self.do_help('media')
foo_parser = argparse_completer.ACArgumentParser(prog='foo')
foo_parser.add_argument('-c', dest='counter', action='count')