summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorEric Lin <anselor@gmail.com>2021-08-19 17:03:42 -0400
committeranselor <anselor@gmail.com>2021-08-23 14:17:12 -0400
commit49805324772ec5fcd06217f4f0a241a9a8d07960 (patch)
treea1647a18085df118bbe12dc37c67104f2817024b /tests
parent6d771e96c0507d9ef4ad3eaaf4bc83669396e591 (diff)
downloadcmd2-git-49805324772ec5fcd06217f4f0a241a9a8d07960.tar.gz
* New function `set_default_command_completer_type()` allows developer to extend and modify the
behavior of `ArgparseCompleter`. * New function `register_argparse_argument_parameter()` allows developers to specify custom parameters to be passed to the argparse parser's `add_argument()` method. These parameters will become accessible in the resulting argparse Action object when modifying `ArgparseCompleter` behavior.
Diffstat (limited to 'tests')
-rw-r--r--tests/test_argparse_completer.py52
1 files changed, 52 insertions, 0 deletions
diff --git a/tests/test_argparse_completer.py b/tests/test_argparse_completer.py
index 6002a856..11745429 100644
--- a/tests/test_argparse_completer.py
+++ b/tests/test_argparse_completer.py
@@ -16,6 +16,8 @@ from cmd2 import (
Cmd2ArgumentParser,
CompletionError,
CompletionItem,
+ argparse_completer,
+ argparse_custom,
with_argparser,
)
from cmd2.utils import (
@@ -1144,3 +1146,53 @@ def test_complete_standalone(ac_app, flag, completions):
first_match = complete_tester(text, line, begidx, endidx, ac_app)
assert first_match is not None
assert ac_app.completion_matches == sorted(completions, key=ac_app.default_sort_key)
+
+
+class CustomCompleter(argparse_completer.ArgparseCompleter):
+
+ def _complete_flags(self, text: str, line: str, begidx: int, endidx: int, matched_flags: List[str]) -> List[str]:
+ """Override so arguments with 'always_complete' set to True will always be completed"""
+ for flag in matched_flags:
+ action = self._flag_to_action[flag]
+ if action.get_always_complete() is True:
+ matched_flags.remove(flag)
+ return super(CustomCompleter, self)._complete_flags(text, line, begidx, endidx, matched_flags)
+
+
+argparse_custom.register_argparse_argument_parameter('always_complete', bool)
+
+
+class CustomCompleterApp(cmd2.Cmd):
+ _parser = Cmd2ArgumentParser(description="Testing manually wrapping")
+ _parser.add_argument('--myflag', always_complete=True, nargs=1)
+
+ @with_argparser(_parser)
+ def do_mycommand(self, cmd: 'CustomCompleterApp', args: argparse.Namespace) -> None:
+ """Test command that will be manually wrapped to use argparse"""
+ print(args)
+
+@pytest.fixture
+def custom_completer_app():
+
+ argparse_completer.set_default_command_completer_type(CustomCompleter)
+ app = CustomCompleterApp()
+ app.stdout = StdSim(app.stdout)
+ yield app
+ argparse_completer.set_default_command_completer_type(argparse_completer.ArgparseCompleter)
+
+@pytest.mark.parametrize(
+ 'command_and_args, text, output_contains, first_match',
+ [
+ ('mycommand', '--my', '', '--myflag '),
+ ('mycommand --myflag 5', '--my', '', '--myflag '),
+ ],
+)
+def test_custom_completer_type(custom_completer_app, command_and_args, text, output_contains, first_match, capsys):
+ line = '{} {}'.format(command_and_args, text)
+ endidx = len(line)
+ begidx = endidx - len(text)
+
+ assert first_match == complete_tester(text, line, begidx, endidx, custom_completer_app)
+
+ out, err = capsys.readouterr()
+ assert output_contains in out