diff options
author | Jared Crapo <jared@kotfu.net> | 2017-08-21 12:27:09 -0600 |
---|---|---|
committer | Jared Crapo <jared@kotfu.net> | 2017-08-21 12:27:09 -0600 |
commit | 1f2fea6c481bdcba526bc10c29edd162cf5212c7 (patch) | |
tree | e2dbd1096e2e294ff4b0351ec88c7e2f6878a596 /examples/example.py | |
parent | 0fff2bec6462a778cb7852c57c7081b7e62629a4 (diff) | |
download | cmd2-git-1f2fea6c481bdcba526bc10c29edd162cf5212c7.tar.gz |
Write documentation for revised transcription feature
Diffstat (limited to 'examples/example.py')
-rwxr-xr-x | examples/example.py | 43 |
1 files changed, 33 insertions, 10 deletions
diff --git a/examples/example.py b/examples/example.py index 482788cc..03508024 100755 --- a/examples/example.py +++ b/examples/example.py @@ -1,14 +1,18 @@ #!/usr/bin/env python # coding=utf-8 -"""A sample application for cmd2. +""" +A sample application for cmd2. -Thanks to cmd2's built-in transcript testing capability, it also serves as a test suite for example.py when used with - the exampleSession.txt transcript. +Thanks to cmd2's built-in transcript testing capability, it also serves as a +test suite for example.py when used with the exampleSession.txt transcript. -Running `python example.py -t exampleSession.txt` will run all the commands in the transcript against example.py, -verifying that the output produced matches the transcript. +Running `python example.py -t exampleSession.txt` will run all the commands in +the transcript against example.py, verifying that the output produced matches +the transcript. """ +import random + from cmd2 import Cmd, make_option, options, set_use_arg_list @@ -17,13 +21,16 @@ class CmdLineApp(Cmd): # Setting this true makes it run a shell command if a cmd2/cmd command doesn't exist # default_to_shell = True + MUMBLES = ['like', '...', 'um', 'er', 'hmmm', 'ahh'] + MUMBLE_FIRST = ['so', 'like', 'well'] + MUMBLE_LAST = ['right?'] def __init__(self): self.abbrev = True self.multilineCommands = ['orate'] self.maxrepeats = 3 - # Add stuff to settable and shortcutgs before calling base class initializer + # Add stuff to settable and shortcuts before calling base class initializer self.settable['maxrepeats'] = 'max repetitions for speak command' self.shortcuts.update({'&': 'speak'}) @@ -46,14 +53,30 @@ class CmdLineApp(Cmd): arg = arg.upper() repetitions = opts.repeat or 1 for i in range(min(repetitions, self.maxrepeats)): - self.stdout.write(arg) - self.stdout.write('\n') - # self.stdout.write is better than "print", because Cmd can be - # initialized with a non-standard output destination + self.poutput(arg) + # recommend using the poutput function instead of + # self.stdout.write or "print", because Cmd allows the user + # to redirect output do_say = do_speak # now "say" is a synonym for "speak" do_orate = do_speak # another synonym, but this one takes multi-line input + @options([ make_option('-r', '--repeat', type="int", help="output [n] times") ]) + def do_mumble(self, arg, opts=None): + """Mumbles what you tell me to.""" + repetitions = opts.repeat or 1 + arg = arg.split() + for i in range(min(repetitions, self.maxrepeats)): + output = [] + if (random.random() < .33): + output.append(random.choice(self.MUMBLE_FIRST)) + for word in arg: + if (random.random() < .40): + output.append(random.choice(self.MUMBLES)) + output.append(word) + if (random.random() < .25): + output.append(random.choice(self.MUMBLE_LAST)) + self.poutput(' '.join(output)) if __name__ == '__main__': c = CmdLineApp() |