diff options
| author | Catherine Devlin <catherine.devlin@gmail.com> | 2010-09-17 11:32:36 -0400 |
|---|---|---|
| committer | Catherine Devlin <catherine.devlin@gmail.com> | 2010-09-17 11:32:36 -0400 |
| commit | b06daf114fa27402bd2ddcc9a5c3493c35ee8219 (patch) | |
| tree | f0e1ac43eac3e22831bc91525ef280b7f2b53786 /example | |
| download | cmd2-git-b06daf114fa27402bd2ddcc9a5c3493c35ee8219.tar.gz | |
no xclip >>> output got lost
Diffstat (limited to 'example')
| -rwxr-xr-x | example/example.py | 34 | ||||
| -rw-r--r-- | example/exampleSession.txt | 91 |
2 files changed, 125 insertions, 0 deletions
diff --git a/example/example.py b/example/example.py new file mode 100755 index 00000000..662b6ec6 --- /dev/null +++ b/example/example.py @@ -0,0 +1,34 @@ +'''A sample application for cmd2.''' + +from cmd2 import Cmd, make_option, options +import unittest, optparse, sys + +class CmdLineApp(Cmd): + multilineCommands = ['orate'] + Cmd.shortcuts.update({'&': 'speak'}) + maxrepeats = 3 + Cmd.settable.append('maxrepeats Max number of `--repeat`s allowed') + + @options([make_option('-p', '--piglatin', action="store_true", help="atinLay"), + make_option('-s', '--shout', action="store_true", help="N00B EMULATION MODE"), + make_option('-r', '--repeat', type="int", help="output [n] times") + ]) + def do_speak(self, arg, opts=None): + """Repeats what you tell me to.""" + arg = ''.join(arg) + if opts.piglatin: + arg = '%s%say' % (arg[1:].rstrip(), arg[0]) + if opts.shout: + 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 + + do_say = do_speak # now "say" is a synonym for "speak" + do_orate = do_speak # another synonym, but this one takes multi-line input + +c = CmdLineApp() +c.cmdloop() diff --git a/example/exampleSession.txt b/example/exampleSession.txt new file mode 100644 index 00000000..c751fd33 --- /dev/null +++ b/example/exampleSession.txt @@ -0,0 +1,91 @@ +(Cmd) help + +Documented commands (type help <topic>): +======================================== +_load ed history list pause run set show +_relative_load edit l load py save shell speak +cmdenvironment hi li orate r say shortcuts + +Undocumented commands: +====================== +EOF eof exit help q quit + +(Cmd) help say +Repeats what you tell me to. +Usage: speak [options] arg + +Options: + -h, --help show this help message and exit + -p, --piglatin atinLay + -s, --shout N00B EMULATION MODE + -r REPEAT, --repeat=REPEAT + output [n] times + +(Cmd) say goodnight, Gracie +goodnight, Gracie +(Cmd) say -ps --repeat=5 goodnight, Gracie +OODNIGHT, GRACIEGAY +OODNIGHT, GRACIEGAY +OODNIGHT, GRACIEGAY +(Cmd) set +abbrev: True +case_insensitive: True +colors: True +continuation_prompt: > +debug: False +default_file_name: command.txt +echo: False +editor: gedit +feedback_to_output: False +maxrepeats: 3 +prompt: (Cmd) +quiet: False +timing: False +(Cmd) set maxrepeats 5 +maxrepeats - was: 3 +now: 5 +(Cmd) say -ps --repeat=5 goodnight, Gracie +OODNIGHT, GRACIEGAY +OODNIGHT, GRACIEGAY +OODNIGHT, GRACIEGAY +OODNIGHT, GRACIEGAY +OODNIGHT, GRACIEGAY +(Cmd) hi +-------------------------[1] +help +-------------------------[2] +help say +-------------------------[3] +say goodnight, Gracie +-------------------------[4] +say -ps --repeat=5 goodnight, Gracie +-------------------------[5] +set +-------------------------[6] +set maxrepeats 5 +-------------------------[7] +say -ps --repeat=5 goodnight, Gracie +(Cmd) run 4 +say -ps --repeat=5 goodnight, Gracie +OODNIGHT, GRACIEGAY +OODNIGHT, GRACIEGAY +OODNIGHT, GRACIEGAY +OODNIGHT, GRACIEGAY +OODNIGHT, GRACIEGAY +(Cmd) orate Four score and +> seven releases ago +> our BDFL +> +Four score and +seven releases ago +our BDFL +(Cmd) & look, a shortcut! +look, a shortcut! +(Cmd) say put this in a file > myfile.txt +(Cmd) say < myfile.txt +put this in a file +(Cmd) set prompt "---> " +prompt - was: (Cmd) +now: ---> +---> say goodbye +goodbye |
