summaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
authorKevin Van Brunt <kmvanbrunt@gmail.com>2020-09-01 18:25:36 -0400
committerKevin Van Brunt <kmvanbrunt@gmail.com>2020-09-01 18:25:36 -0400
commite4e05127e144ea45ff63a266025f2135d1d9cd72 (patch)
tree05d9dab04510611ecec767c87208f1aad8c4ac39 /examples
parentbf3d1e4132a04dd0983dd0bddc2889454b6a3048 (diff)
downloadcmd2-git-e4e05127e144ea45ff63a266025f2135d1d9cd72.tar.gz
Updated read_input example
Diffstat (limited to 'examples')
-rw-r--r--examples/read_input.py43
1 files changed, 33 insertions, 10 deletions
diff --git a/examples/read_input.py b/examples/read_input.py
index 36e92ec3..e772a106 100644
--- a/examples/read_input.py
+++ b/examples/read_input.py
@@ -19,7 +19,7 @@ class ReadInputApp(cmd2.Cmd):
@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")
+ self.poutput("Tab completion and up-arrow history is off")
try:
self.read_input("> ")
except EOFError:
@@ -28,17 +28,18 @@ class ReadInputApp(cmd2.Cmd):
@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")
+ self.poutput("Tab completion is off but using custom history")
try:
input_str = self.read_input("> ", history=self.custom_history)
- self.custom_history.append(input_str)
except EOFError:
pass
+ else:
+ self.custom_history.append(input_str)
@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")
+ self.poutput("Tab completing and up-arrow history configured for commands")
try:
self.read_input("> ", completion_mode=cmd2.CompletionMode.COMMANDS)
except EOFError:
@@ -47,34 +48,36 @@ class ReadInputApp(cmd2.Cmd):
@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")
+ self.poutput("Tab completing with static choices list and using 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
+ else:
+ self.custom_history.append(input_str)
# noinspection PyMethodMayBeStatic
def choices_provider(self) -> List[str]:
"""Example choices provider function"""
- return ["provider_1", "provider_2", "provider_3"]
+ return ["from_provider_1", "from_provider_2", "from_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")
+ self.poutput("Tab completing with choices from provider function and using 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
+ else:
+ self.custom_history.append(input_str)
@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")
+ self.poutput("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)
@@ -82,6 +85,26 @@ class ReadInputApp(cmd2.Cmd):
except EOFError:
pass
+ @cmd2.with_category(EXAMPLE_COMMANDS)
+ def do_custom_parser(self, _) -> None:
+ """Call read_input to use a custom history and an argument parser"""
+ parser = cmd2.Cmd2ArgumentParser(prog='', description="An example parser")
+ parser.add_argument('-o', '--option', help="an optional arg")
+ parser.add_argument('arg_1', help="a choice for this arg", metavar='arg_1',
+ choices=['my_choice', 'your_choice'])
+ parser.add_argument('arg_2', help="path of something", completer=cmd2.Cmd.path_complete)
+
+ self.poutput("Tab completing with argument parser and using custom history")
+ self.poutput(parser.format_usage())
+
+ try:
+ input_str = self.read_input("> ", history=self.custom_history, completion_mode=cmd2.CompletionMode.CUSTOM,
+ parser=parser)
+ except EOFError:
+ pass
+ else:
+ self.custom_history.append(input_str)
+
if __name__ == '__main__':
import sys