summaryrefslogtreecommitdiff
path: root/examples/read_input.py
diff options
context:
space:
mode:
authorKevin Van Brunt <kmvanbrunt@gmail.com>2020-09-01 11:40:57 -0400
committerKevin Van Brunt <kmvanbrunt@gmail.com>2020-09-01 11:40:57 -0400
commit7294ebcfdc01364da0c7b259e3985dff36bb4864 (patch)
treed95e76fb2d57d7fdd7b698d06b6e2dbd74ff5198 /examples/read_input.py
parent7457fd3e2880fd3abbe403de4a0086fc91f82869 (diff)
downloadcmd2-git-7294ebcfdc01364da0c7b259e3985dff36bb4864.tar.gz
Added read_input() example
Diffstat (limited to 'examples/read_input.py')
-rw-r--r--examples/read_input.py89
1 files changed, 89 insertions, 0 deletions
diff --git a/examples/read_input.py b/examples/read_input.py
new file mode 100644
index 00000000..36e92ec3
--- /dev/null
+++ b/examples/read_input.py
@@ -0,0 +1,89 @@
+#!/usr/bin/env python
+# coding=utf-8
+"""
+A simple example demonstrating the various ways to call cmd2.Cmd.read_input() for input history and tab completion
+"""
+from typing import List
+
+import cmd2
+
+EXAMPLE_COMMANDS = "Example Commands"
+
+
+class ReadInputApp(cmd2.Cmd):
+ def __init__(self, *args, **kwargs) -> None:
+ super().__init__(*args, **kwargs)
+ self.prompt = "\n" + self.prompt
+ self.custom_history = ['history 1', 'history 2']
+
+ @cmd2.with_category(EXAMPLE_COMMANDS)
+ def do_basic(self, _) -> None:
+ """Call read_input with no history or tab completion"""
+ print("Tab completion and up-arrow history is off")
+ try:
+ self.read_input("> ")
+ except EOFError:
+ pass
+
+ @cmd2.with_category(EXAMPLE_COMMANDS)
+ def do_basic_with_history(self, _) -> None:
+ """Call read_input with custom history and no tab completion"""
+ print("Tab completion is off but up-arrow history is populated")
+ try:
+ input_str = self.read_input("> ", history=self.custom_history)
+ self.custom_history.append(input_str)
+ except EOFError:
+ pass
+
+ @cmd2.with_category(EXAMPLE_COMMANDS)
+ def do_commands(self, _) -> None:
+ """Call read_input the same way cmd2 prompt does to read commands"""
+ print("Tab completing and up-arrow history configured for commands")
+ try:
+ self.read_input("> ", completion_mode=cmd2.CompletionMode.COMMANDS)
+ except EOFError:
+ pass
+
+ @cmd2.with_category(EXAMPLE_COMMANDS)
+ def do_custom_choices(self, _) -> None:
+ """Call read_input to use custom history and choices"""
+ print("Tab completing with static choices list and custom history")
+ try:
+ input_str = self.read_input("> ", history=self.custom_history, completion_mode=cmd2.CompletionMode.CUSTOM,
+ choices=['choice_1', 'choice_2', 'choice_3'])
+ self.custom_history.append(input_str)
+ except EOFError:
+ pass
+
+ # noinspection PyMethodMayBeStatic
+ def choices_provider(self) -> List[str]:
+ """Example choices provider function"""
+ return ["provider_1", "provider_2", "provider_3"]
+
+ @cmd2.with_category(EXAMPLE_COMMANDS)
+ def do_custom_choices_provider(self, _) -> None:
+ """Call read_input to use custom history and choices provider function"""
+ print("Tab completing with choices from provider function and custom history")
+ try:
+ input_str = self.read_input("> ", history=self.custom_history, completion_mode=cmd2.CompletionMode.CUSTOM,
+ choices_provider=ReadInputApp.choices_provider)
+ self.custom_history.append(input_str)
+ except EOFError:
+ pass
+
+ @cmd2.with_category(EXAMPLE_COMMANDS)
+ def do_custom_completer(self, _) -> None:
+ """all read_input to use custom history and completer function"""
+ print("Tab completing paths and using custom history")
+ try:
+ input_str = self.read_input("> ", history=self.custom_history, completion_mode=cmd2.CompletionMode.CUSTOM,
+ completer=cmd2.Cmd.path_complete)
+ self.custom_history.append(input_str)
+ except EOFError:
+ pass
+
+
+if __name__ == '__main__':
+ import sys
+ app = ReadInputApp()
+ sys.exit(app.cmdloop())