diff options
-rw-r--r-- | cmd2/argparse_completer.py | 4 | ||||
-rw-r--r-- | cmd2/cmd2.py | 6 | ||||
-rw-r--r-- | tests/test_argparse_completer.py | 65 |
3 files changed, 65 insertions, 10 deletions
diff --git a/cmd2/argparse_completer.py b/cmd2/argparse_completer.py index 0e9321db..b6d22dce 100644 --- a/cmd2/argparse_completer.py +++ b/cmd2/argparse_completer.py @@ -59,7 +59,7 @@ How to supply completion choice lists or functions for sub-commands: """ import argparse -import os +import shutil from typing import List, Union from . import utils @@ -504,7 +504,7 @@ class AutoCompleter(object): if item_width > token_width: token_width = item_width - term_size = os.get_terminal_size() + term_size = shutil.get_terminal_size() fill_width = int(term_size.columns * .6) - (token_width + 2) for item in completions: entry = '{: <{token_width}}{: <{fill_width}}'.format(item, item.description, diff --git a/cmd2/cmd2.py b/cmd2/cmd2.py index e6d4ae47..009b1359 100644 --- a/cmd2/cmd2.py +++ b/cmd2/cmd2.py @@ -1584,18 +1584,18 @@ class Cmd(cmd.Cmd): def _autocomplete_default(self, text: str, line: str, begidx: int, endidx: int, argparser: argparse.ArgumentParser) -> List[str]: - """Default completion function for argparse commands.""" + """Default completion function for argparse commands""" completer = AutoCompleter(argparser, self) tokens, _ = self.tokens_for_completion(line, begidx, endidx) return completer.complete_command(tokens, text, line, begidx, endidx) def get_all_commands(self) -> List[str]: - """Returns a list of all commands.""" + """Return a list of all commands""" return [name[len(COMMAND_FUNC_PREFIX):] for name in self.get_names() if name.startswith(COMMAND_FUNC_PREFIX) and callable(getattr(self, name))] def get_visible_commands(self) -> List[str]: - """Returns a list of commands that have not been hidden or disabled.""" + """Return a list of commands that have not been hidden or disabled""" commands = self.get_all_commands() # Remove the hidden commands diff --git a/tests/test_argparse_completer.py b/tests/test_argparse_completer.py index 839cdf7a..26bea794 100644 --- a/tests/test_argparse_completer.py +++ b/tests/test_argparse_completer.py @@ -79,9 +79,9 @@ class AutoCompleteTester(cmd2.Cmd): completer_parser = Cmd2ArgParser() # Flags args for completer command - completer_parser.add_argument("-f", "--function", help="a flag populated with a choices function", + completer_parser.add_argument("-f", "--function", help="a flag using a completer function", completer_function=completer_function) - completer_parser.add_argument("-m", "--method", help="a flag populated with a choices method", + completer_parser.add_argument("-m", "--method", help="a flag using a completer method", completer_method=completer_method) # Positional args for completer command @@ -102,12 +102,67 @@ def ac_app(): return app -def test_help_basic(ac_app): - out1, err1 = run_cmd(ac_app, 'choices -h') - out2, err2 = run_cmd(ac_app, 'help choices') +def test_help(ac_app): + out1, err1 = run_cmd(ac_app, 'alias -h') + out2, err2 = run_cmd(ac_app, 'help alias') assert out1 == out2 +def test_help_subcommand(ac_app): + out1, err1 = run_cmd(ac_app, 'alias create -h') + out2, err2 = run_cmd(ac_app, 'help alias create') + assert out1 == out2 + + +def test_complete_help(ac_app): + text = 'al' + line = 'help {}'.format(text) + endidx = len(line) + begidx = endidx - len(text) + + first_match = complete_tester(text, line, begidx, endidx, ac_app) + assert first_match is not None and ac_app.completion_matches == ['alias '] + + +def test_complete_help_subcommand(ac_app): + text = 'cre' + line = 'help alias {}'.format(text) + endidx = len(line) + begidx = endidx - len(text) + + first_match = complete_tester(text, line, begidx, endidx, ac_app) + assert first_match is not None and ac_app.completion_matches == ['create '] + + +@pytest.mark.parametrize('num_aliases, show_description', [ + # The number of completion results determines if the description field of CompletionItems gets displayed + # in the tab completions. The count must be greater than 1 and less than ac_app.max_completion_items, + # which defaults to 50. + (1, False), + (5, True), + (100, False), +]) +def test_completion_items(ac_app, num_aliases, show_description): + # Create aliases + for i in range(0, num_aliases): + run_cmd(ac_app, 'alias create fake{} help'.format(i)) + + assert len(ac_app.aliases) == num_aliases + + text = 'fake' + line = 'alias list {}'.format(text) + endidx = len(line) + begidx = endidx - len(text) + + first_match = complete_tester(text, line, begidx, endidx, ac_app) + assert first_match is not None + assert len(ac_app.completion_matches) == num_aliases + assert len(ac_app.display_matches) == num_aliases + + # If show_description is True, the alias's value will be in the display text + assert ('help' in ac_app.display_matches[0]) == show_description + + @pytest.mark.parametrize('text, completions', [ ('-', ['--function', '--help', '--list', '--method', '-f', '-h', '-l', '-m']), ('--', ['--function', '--help', '--list', '--method']), |