diff example/example.py @ 113:7d215852f9a6

still unpackaging
author catherine@Elli.myhome.westell.com
date Sat, 25 Oct 2008 19:34:16 -0400
parents cmd2/example/example.py@c1e5df33721f
children 07dff0ba981e
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/example/example.py	Sat Oct 25 19:34:16 2008 -0400
@@ -0,0 +1,45 @@
+'''A sample application for cmd2.'''
+
+from cmd2 import Cmd, make_option, options, Cmd2TestCase
+import unittest, optparse, sys
+
+class CmdLineApp(Cmd):
+    multilineCommands = ['orate']
+    Cmd.shortcuts.update({'&': 'speak'})
+    maxrepeats = 3
+    Cmd.settable.append('maxrepeats')
+
+    @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:], 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
+
+class TestMyAppCase(Cmd2TestCase):
+    CmdApp = CmdLineApp
+    transcriptFileName = 'exampleSession.txt'
+
+parser = optparse.OptionParser()
+parser.add_option('-t', '--test', dest='unittests', action='store_true', default=False, help='Run unit test suite')
+(callopts, callargs) = parser.parse_args()
+if callopts.unittests:
+    sys.argv = [sys.argv[0]]  # the --test argument upsets unittest.main()
+    unittest.main()
+else:
+    app = CmdLineApp()
+    app.cmdloop()