diff -r 917295aaad76 Lib/test/test_cmd.py --- a/Lib/test/test_cmd.py Mon Jul 30 00:01:44 2012 +0200 +++ b/Lib/test/test_cmd.py Sun Jul 29 23:15:12 2012 -0400 @@ -221,10 +221,76 @@ "(Cmd) *** Unknown syntax: EOF\n")) +class TestNestedCLI(unittest.TestCase): + + class InnerCLI(cmd.Cmd): + + def do_bar(self, s): + self.stdout.write('baz\n') + + def do_quit(self, s): + return True + + def do_EOF(self, args): + return True + + + class OuterCLI(cmd.Cmd): + + def __init__(self, *arg, **kwargs): + cmd.Cmd.__init__(self, *arg, **kwargs) + self.register_subCLI('inner', TestNestedCLI.InnerCLI) + + def do_foo(self, s): + self.stdout.write('bar\n') + + def do_quit(self, s): + return True + + def do_EOF(self, args): + return True + + def register_subCLI(self, command_name, subcli_class): + '''Allows an inner Cmd instance (``subcli_class``) + to be invoked from an outer Cmd instance + via the ``command_name`` command.''' + _parent = self + def call_subcli(self): + subcli = subcli_class(stdin=_parent.stdin, + stdout=_parent.stdout) + subcli.use_rawinput = _parent.use_rawinput + subcli.cmdloop() + setattr(self, 'do_%s' % (command_name, ), call_subcli) + + def test_nested_subcli_works(self): + input = io.StringIO("foo \n inner \n bar \n quit \n") + output = io.StringIO() + cmd = self.OuterCLI(stdin=input, stdout=output) + cmd.use_rawinput = False + cmd.cmdloop() + self.assertMultiLineEqual(output.getvalue(), + ("(Cmd) bar\n" + "(Cmd) " + "(Cmd) baz\n" + "(Cmd) " + "(Cmd) " + )) + + def test_nested_subcli_appears_in_help(self): + input = io.StringIO("help \n") + output = io.StringIO() + cmd = self.OuterCLI(stdin=input, stdout=output) + cmd.use_rawinput = False + cmd.cmdloop() + self.assertIn("foo", output.getvalue()) + self.assertIn("inner", output.getvalue()) + + def test_main(verbose=None): from test import test_cmd support.run_doctest(test_cmd, verbose) support.run_unittest(TestAlternateInput) + support.run_unittest(TestNestedCLI) def test_coverage(coverdir): trace = support.import_module('trace')