# HG changeset patch # User cat@eee # Date 1266035359 18000 # Node ID 99dd71cb477a4b06967fac8fc4399dd73bea471c # Parent 45e70737791f8becbc0c0e5cc831692736dcdea3 run() folded into app.cmdloop() diff -r 45e70737791f -r 99dd71cb477a cmd2.py --- a/cmd2.py Fri Feb 12 22:57:41 2010 -0500 +++ b/cmd2.py Fri Feb 12 23:29:19 2010 -0500 @@ -883,7 +883,7 @@ line = line[:-1] return line - def cmdloop(self, intro=None): + def _cmdloop(self, intro=None): """Repeatedly issue a prompt, accept input, parse an initial prefix off the received input, and dispatch to action methods, passing them the remainder of the line as argument. @@ -1212,7 +1212,7 @@ self.use_rawinput = False self.prompt = self.continuation_prompt = '' self.current_script_dir = os.path.split(targetname)[0] - stop = self.cmdloop() + stop = self._cmdloop() self.stdin.close() keepstate.restore() self.lastcmd = '' @@ -1245,6 +1245,32 @@ data = f.read() f.close() return data + + def runTranscriptTests(self, callargs): + class TestMyAppCase(Cmd2TestCase): + CmdApp = self.__class__ + self.__class__.testfiles = callargs + sys.argv = [sys.argv[0]] # the --test argument upsets unittest.main() + testcase = TestMyAppCase() + runner = unittest.TextTestRunner() + result = runner.run(testcase) + result.printErrors() + + def cmdloop(self): + parser = optparse.OptionParser() + parser.add_option('-t', '--test', dest='test', + action="store_true", + help='Test against transcript(s) in FILE (wildcards OK)') + (callopts, callargs) = parser.parse_args() + if callopts.test: + self.runTranscriptTests(callargs) + else: + # hold onto the args and run .onecmd with them + # in sqlpython, first arg has implied \connect + for initial_command in callargs: + if self.onecmd(initial_command + '\n') == app._STOP_AND_EXIT: + return + self._cmdloop() class HistoryItem(str): listformat = '-------------------------[%d]\n%s\n' @@ -1326,7 +1352,6 @@ def extend(self, new): for n in new: self.append(n) - def get(self, getme=None, fromEnd=False): if not getme: @@ -1492,28 +1517,6 @@ def tearDown(self): if self.CmdApp: self.outputTrap.tearDown() - -def run(app): - parser = optparse.OptionParser() - parser.add_option('-t', '--test', dest='test', action="store_true", - help='Test against transcript(s) in FILE (wildcards OK)') - (callopts, callargs) = parser.parse_args() - if callopts.test: - class TestMyAppCase(Cmd2TestCase): - CmdApp = app.__class__ - app.__class__.testfiles = callargs - sys.argv = [sys.argv[0]] # the --test argument upsets unittest.main() - testcase = TestMyAppCase() - runner = unittest.TextTestRunner() - result = runner.run(testcase) - result.printErrors() - else: - # hold onto the args and run .onecmd with them - # in sqlpython, first arg has implied \connect - for initial_command in callargs: - if app.onecmd(initial_command + '\n') == app._STOP_AND_EXIT: - return - app.cmdloop() if __name__ == '__main__': doctest.testmod(optionflags = doctest.NORMALIZE_WHITESPACE) diff -r 45e70737791f -r 99dd71cb477a example/example.py --- a/example/example.py Fri Feb 12 22:57:41 2010 -0500 +++ b/example/example.py Fri Feb 12 23:29:19 2010 -0500 @@ -1,6 +1,6 @@ '''A sample application for cmd2.''' -from cmd2 import Cmd, make_option, options, run +from cmd2 import Cmd, make_option, options import unittest, optparse, sys class CmdLineApp(Cmd): @@ -31,4 +31,4 @@ do_orate = do_speak # another synonym, but this one takes multi-line input c = CmdLineApp() -run(c) +c.cmdloop() diff -r 45e70737791f -r 99dd71cb477a example/exampleSession.txt --- a/example/exampleSession.txt Fri Feb 12 22:57:41 2010 -0500 +++ b/example/exampleSession.txt Fri Feb 12 23:29:19 2010 -0500 @@ -88,4 +88,4 @@ prompt - was: (Cmd) now: ---> ---> say goodbye -goodbye \ No newline at end of file +goodbye