| 1 |
|
| 2 |
"""A simple example demonstrating the various ways to call cmd2.Cmd.read_input() and cmd2.Cmd.read_secret(). |
| 3 |
|
| 4 |
These methods can be used to read input from stdin with optional history, tab completion, or password masking. |
| 5 |
It also demonstrates how to use the cmd2.Cmd.select method. |
| 6 |
""" |
| 7 |
|
| 8 |
import contextlib |
| 9 |
|
| 10 |
import cmd2 |
| 11 |
|
| 12 |
EXAMPLE_COMMANDS = "Example Commands" |
| 13 |
|
| 14 |
|
| 15 |
class ReadInputApp(cmd2.Cmd): |
| 16 |
def __init__(self, *args, **kwargs) -> None: |
| 17 |
super().__init__(*args, **kwargs) |
| 18 |
self.prompt = "\n" + self.prompt |
| 19 |
self.custom_history = ["history 1", "history 2"] |
| 20 |
|
| 21 |
@cmd2.with_category(EXAMPLE_COMMANDS) |
| 22 |
def do_basic(self, _) -> None: |
| 23 |
"""Call read_input with no history or tab completion.""" |
| 24 |
self.poutput("Tab completion and up-arrow history is off") |
| 25 |
with contextlib.suppress(EOFError): |
| 26 |
self.read_input("> ") |
| 27 |
|
| 28 |
@cmd2.with_category(EXAMPLE_COMMANDS) |
| 29 |
def do_basic_with_history(self, _) -> None: |
| 30 |
"""Call read_input with custom history and no tab completion.""" |
| 31 |
self.poutput("Tab completion is off but using custom history") |
| 32 |
try: |
| 33 |
input_str = self.read_input("> ", history=self.custom_history) |
| 34 |
except EOFError: |
| 35 |
pass |
| 36 |
else: |
| 37 |
self.custom_history.append(input_str) |
| 38 |
|
| 39 |
@cmd2.with_category(EXAMPLE_COMMANDS) |
| 40 |
def do_custom_choices(self, _) -> None: |
| 41 |
"""Call read_input to use custom history and choices.""" |
| 42 |
self.poutput("Tab completing with static choices list and using custom history") |
| 43 |
try: |
| 44 |
input_str = self.read_input( |
| 45 |
"> ", |
| 46 |
history=self.custom_history, |
| 47 |
choices=["choice_1", "choice_2", "choice_3"], |
| 48 |
) |
| 49 |
except EOFError: |
| 50 |
pass |
| 51 |
else: |
| 52 |
self.custom_history.append(input_str) |
| 53 |
|
| 54 |
def choices_provider(self) -> cmd2.Choices: |
| 55 |
"""Example choices provider function.""" |
| 56 |
return cmd2.Choices.from_values(["from_provider_1", "from_provider_2", "from_provider_3"]) |
| 57 |
|
| 58 |
@cmd2.with_category(EXAMPLE_COMMANDS) |
| 59 |
def do_custom_choices_provider(self, _) -> None: |
| 60 |
"""Call read_input to use custom history and choices provider function.""" |
| 61 |
self.poutput("Tab completing with choices from provider function and using custom history") |
| 62 |
try: |
| 63 |
input_str = self.read_input( |
| 64 |
"> ", |
| 65 |
history=self.custom_history, |
| 66 |
choices_provider=ReadInputApp.choices_provider, |
| 67 |
) |
| 68 |
except EOFError: |
| 69 |
pass |
| 70 |
else: |
| 71 |
self.custom_history.append(input_str) |
| 72 |
|
| 73 |
@cmd2.with_category(EXAMPLE_COMMANDS) |
| 74 |
def do_custom_completer(self, _) -> None: |
| 75 |
"""Call read_input to use custom history and completer function.""" |
| 76 |
self.poutput("Tab completing paths and using custom history") |
| 77 |
try: |
| 78 |
input_str = self.read_input("> ", history=self.custom_history, completer=cmd2.Cmd.path_complete) |
| 79 |
self.custom_history.append(input_str) |
| 80 |
except EOFError: |
| 81 |
pass |
| 82 |
|
| 83 |
@cmd2.with_category(EXAMPLE_COMMANDS) |
| 84 |
def do_custom_parser(self, _) -> None: |
| 85 |
"""Call read_input to use a custom history and an argument parser.""" |
| 86 |
parser = cmd2.Cmd2ArgumentParser(prog="", description="An example parser") |
| 87 |
parser.add_argument("-o", "--option", help="an optional arg") |
| 88 |
parser.add_argument("arg_1", help="a choice for this arg", metavar="arg_1", choices=["my_choice", "your_choice"]) |
| 89 |
parser.add_argument("arg_2", help="path of something", completer=cmd2.Cmd.path_complete) |
| 90 |
|
| 91 |
self.poutput("Tab completing with argument parser and using custom history") |
| 92 |
self.poutput(parser.format_usage()) |
| 93 |
|
| 94 |
try: |
| 95 |
input_str = self.read_input("> ", history=self.custom_history, parser=parser) |
| 96 |
except EOFError: |
| 97 |
pass |
| 98 |
else: |
| 99 |
self.custom_history.append(input_str) |
| 100 |
|
| 101 |
@cmd2.with_category(EXAMPLE_COMMANDS) |
| 102 |
def do_read_password(self, _) -> None: |
| 103 |
"""Call read_secret to read a password without displaying it while being typed. |
| 104 |
|
| 105 |
WARNING: Password will be displayed for verification after it is typed. |
| 106 |
""" |
| 107 |
self.poutput("The input will not be displayed on the screen") |
| 108 |
try: |
| 109 |
password = self.read_secret("Password: ") |
| 110 |
self.poutput(f"You entered: {password}") |
| 111 |
except EOFError: |
| 112 |
pass |
| 113 |
|
| 114 |
def do_eat(self, arg) -> None: |
| 115 |
"""Example of using the select method for reading multiple choice input. |
| 116 |
|
| 117 |
Usage: eat wheatties |
| 118 |
""" |
| 119 |
sauce = self.select("sweet salty", "Sauce? ") |
| 120 |
result = "{food} with {sauce} sauce, yum!" |
| 121 |
result = result.format(food=arg, sauce=sauce) |
| 122 |
self.stdout.write(result + "\n") |
| 123 |
|
| 124 |
|
| 125 |
if __name__ == "__main__": |
| 126 |
import sys |
| 127 |
|
| 128 |
app = ReadInputApp() |
| 129 |
sys.exit(app.cmdloop()) |