| 1 |
|
| 2 |
"""A sample cmd application that shows how to trivially migrate a cmd application to use cmd2.""" |
| 3 |
|
| 4 |
|
| 5 |
import cmd |
| 6 |
import secrets |
| 7 |
|
| 8 |
|
| 9 |
class CmdLineApp(cmd.Cmd): |
| 10 |
"""Example cmd application.""" |
| 11 |
|
| 12 |
MUMBLES = ("like", "...", "um", "er", "hmmm", "ahh") |
| 13 |
MUMBLE_FIRST = ("so", "like", "well") |
| 14 |
MUMBLE_LAST = ("right?",) |
| 15 |
|
| 16 |
def __init__(self) -> None: |
| 17 |
super().__init__() |
| 18 |
self._secure_generator = secrets.SystemRandom() |
| 19 |
|
| 20 |
def do_exit(self, _line) -> bool: |
| 21 |
"""Exit the application.""" |
| 22 |
return True |
| 23 |
|
| 24 |
do_EOF = do_exit |
| 25 |
do_quit = do_exit |
| 26 |
|
| 27 |
def do_speak(self, line) -> None: |
| 28 |
"""Repeats what you tell me to.""" |
| 29 |
print(line, file=self.stdout) |
| 30 |
|
| 31 |
do_say = do_speak |
| 32 |
|
| 33 |
def do_mumble(self, line) -> None: |
| 34 |
"""Mumbles what you tell me to.""" |
| 35 |
words = line.split(" ") |
| 36 |
output = [] |
| 37 |
if self._secure_generator.random() < 0.33: |
| 38 |
output.append(secrets.choice(self.MUMBLE_FIRST)) |
| 39 |
for word in words: |
| 40 |
if self._secure_generator.random() < 0.40: |
| 41 |
output.append(secrets.choice(self.MUMBLES)) |
| 42 |
output.append(word) |
| 43 |
if self._secure_generator.random() < 0.25: |
| 44 |
output.append(secrets.choice(self.MUMBLE_LAST)) |
| 45 |
print(" ".join(output), file=self.stdout) |
| 46 |
|
| 47 |
|
| 48 |
if __name__ == "__main__": |
| 49 |
import sys |
| 50 |
|
| 51 |
c = CmdLineApp() |
| 52 |
sys.exit(c.cmdloop()) |