diff options
-rw-r--r-- | cmd2/argparse_custom.py | 13 | ||||
-rw-r--r-- | tests/test_argparse_completer.py | 42 |
2 files changed, 51 insertions, 4 deletions
diff --git a/cmd2/argparse_custom.py b/cmd2/argparse_custom.py index 4faddcab..f7dbc8a3 100644 --- a/cmd2/argparse_custom.py +++ b/cmd2/argparse_custom.py @@ -94,7 +94,7 @@ Tab Completion: as dynamic. Therefore it is up to the developer to validate if the user has typed an acceptable value for these arguments. - The following functions exist in cases where you may want to manually add choice providing function/methods to + The following functions exist in cases where you may want to manually a add choice-providing function/method to an existing argparse action. For instance, in __init__() of a custom action class. set_choices_function(action, func) @@ -116,6 +116,13 @@ Tab Completion: their values. All tokens are stored in the dictionary as the raw strings provided on the command line. It is up to the developer to determine if the user entered the correct argument type (e.g. int) and validate their values. +CompletionError Class: + Raised during tab-completion operations to report any sort of error you want printed by the AutoCompleter + + Example use cases + - Reading a database to retrieve a tab completion data set failed + - A previous command line argument that determines the data set being completed is invalid + CompletionItem Class: This class was added to help in cases where uninformative data is being tab completed. For instance, tab completing ID numbers isn't very helpful to a user without context. Returning a list of CompletionItems @@ -226,8 +233,8 @@ class CompletionError(Exception): Raised during tab-completion operations to report any sort of error you want printed by the AutoCompleter Example use cases - 1. Reading a database to retrieve a tab completion data set failed - 2. A previous command line argument that determines the data set being completed is invalid + - Reading a database to retrieve a tab completion data set failed + - A previous command line argument that determines the data set being completed is invalid """ pass diff --git a/tests/test_argparse_completer.py b/tests/test_argparse_completer.py index 788a7e59..308a4d95 100644 --- a/tests/test_argparse_completer.py +++ b/tests/test_argparse_completer.py @@ -9,7 +9,7 @@ from typing import List import pytest import cmd2 -from cmd2 import with_argparser, Cmd2ArgumentParser, CompletionItem +from cmd2 import with_argparser, Cmd2ArgumentParser, CompletionError, CompletionItem from cmd2.utils import StdSim, basic_complete from .conftest import run_cmd, complete_tester @@ -211,6 +211,27 @@ class AutoCompleteTester(cmd2.Cmd): pass ############################################################################################################ + # Begin code related to CompletionError + ############################################################################################################ + def completer_raise_error(self, text: str, line: str, begidx: int, endidx: int) -> List[str]: + """Raises CompletionError""" + raise CompletionError('completer broke something') + + def choice_raise_error(self) -> List[str]: + """Raises CompletionError""" + raise CompletionError('choice broke something') + + comp_error_parser = Cmd2ArgumentParser() + comp_error_parser.add_argument('completer', help='positional arg', + completer_method=completer_raise_error) + comp_error_parser.add_argument('--choice', help='flag arg', + choices_method=choice_raise_error) + + @with_argparser(comp_error_parser) + def do_raise_completion_error(self, args: argparse.Namespace) -> None: + pass + + ############################################################################################################ # Begin code related to receiving arg_tokens ############################################################################################################ arg_tokens_parser = Cmd2ArgumentParser() @@ -723,6 +744,25 @@ Hint: ''' +@pytest.mark.parametrize('args, text', [ + # Exercise a flag arg and choices function that raises a CompletionError + ('--choice ', 'choice'), + + # Exercise a positional arg and completer that raises a CompletionError + ('', 'completer') +]) +def test_completion_error(ac_app, capsys, args, text): + line = 'raise_completion_error {} {}'.format(args, text) + endidx = len(line) + begidx = endidx - len(text) + + first_match = complete_tester(text, line, begidx, endidx, ac_app) + out, err = capsys.readouterr() + + assert first_match is None + assert "{} broke something".format(text) in out + + @pytest.mark.parametrize('command_and_args, completions', [ # Exercise a choices function that receives arg_tokens dictionary ('arg_tokens choice subcmd', ['choice', 'subcmd']), |