From 1a42fa1b7995b0396983942e3ec0409cdcdd635b Mon Sep 17 00:00:00 2001 From: Calvin Lobo Date: Thu, 18 Jan 2018 12:01:33 -0500 Subject: added a decorator function that allows nesting of Cmd instances to create submenus --- examples/submenus.py | 90 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 90 insertions(+) create mode 100644 examples/submenus.py (limited to 'examples') diff --git a/examples/submenus.py b/examples/submenus.py new file mode 100644 index 00000000..39512ba7 --- /dev/null +++ b/examples/submenus.py @@ -0,0 +1,90 @@ +""" +Create a CLI with a nested command structure as follows. The commands 'second' and 'third' navigate the CLI to the scope +of the submenu. Nesting of the submenus is done with the cmd2.AddSubmenu() decorator. + + (Top Level)----second----->(2nd Level)----third----->(3rd Level) + | | | + ---> say ---> say ---> say + + + +""" + +from __future__ import print_function +import cmd2 + + +class ThirdLevel(cmd2.Cmd): + """To be used as a third level command class. """ + + def __init__(self, *args, **kwargs): + cmd2.Cmd.__init__(self, *args, **kwargs) + self.prompt = '3rdLevel ' + self.top_level_attr = None + self.second_level_attr = None + + def do_say(self, line): + print("You called a command in ThirdLevel with '%s'. " + "It has access to second_level_attr: %s " + "and second_level_attr: %s" % (line, self.top_level_attr, self.second_level_attr)) + + def help_say(self): + print("This is a third level submenu (submenu_ab). Options are qwe, asd, zxc") + + def complete_say(self, text, line, begidx, endidx): + return [s for s in ['qwe', 'asd', 'zxc'] if s.startswith(text)] + + +@cmd2.AddSubmenu(ThirdLevel(), + command='third', + aliases=('third_alias',), + shared_attributes=dict(second_level_attr='second_level_attr', top_level_attr='top_level_attr')) +class SecondLevel(cmd2.Cmd): + """To be used as a second level command class. """ + def __init__(self, *args, **kwargs): + cmd2.Cmd.__init__(self, *args, **kwargs) + self.prompt = '2ndLevel ' + self.top_level_attr = None + self.second_level_attr = 987654321 + + def do_say(self, line): + print("You called a command in SecondLevel with '%s'. " + "It has access to top_level_attr: %s" % (line, self.top_level_attr)) + + def help_say(self): + print("This is a SecondLevel menu. Options are qwe, asd, zxc") + + def complete_say(self, text, line, begidx, endidx): + return [s for s in ['qwe', 'asd', 'zxc'] if s.startswith(text)] + + + +@cmd2.AddSubmenu(SecondLevel(), + command='second', + aliases=('second_alias',), + shared_attributes=dict(top_level_attr='top_level_attr')) +class TopLevel(cmd2.Cmd): + """To be used as the main / top level command class that will contain other submenus.""" + + def __init__(self, *args, **kwargs): + cmd2.Cmd.__init__(self, *args, **kwargs) + self.prompt = 'TopLevel ' + self.top_level_attr = 123456789 + + def do_say(self, line): + print("You called a command in TopLevel with '%s'. " + "TopLevel has attribute top_level_attr=%s" % (line, self.top_level_attr)) + + def help_say(self): + print("This is a top level submenu. Options are qwe, asd, zxc") + + def complete_say(self, text, line, begidx, endidx): + return [s for s in ['qwe', 'asd', 'zxc'] if s.startswith(text)] + + + +if __name__ == '__main__': + + root = TopLevel() + root.cmdloop() + -- cgit v1.2.1 From bb53287029df5ce870e15b46a1b9109090620f05 Mon Sep 17 00:00:00 2001 From: Calvin Lobo Date: Sun, 4 Feb 2018 15:33:00 -0500 Subject: Fixed typo in ThirdLevel.do_say() print Added shebang line --- examples/submenus.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'examples') diff --git a/examples/submenus.py b/examples/submenus.py index 39512ba7..6fcbb307 100644 --- a/examples/submenus.py +++ b/examples/submenus.py @@ -1,3 +1,4 @@ +#!/usr/bin/env python """ Create a CLI with a nested command structure as follows. The commands 'second' and 'third' navigate the CLI to the scope of the submenu. Nesting of the submenus is done with the cmd2.AddSubmenu() decorator. @@ -25,7 +26,7 @@ class ThirdLevel(cmd2.Cmd): def do_say(self, line): print("You called a command in ThirdLevel with '%s'. " - "It has access to second_level_attr: %s " + "It has access to top_level_attr: %s " "and second_level_attr: %s" % (line, self.top_level_attr, self.second_level_attr)) def help_say(self): -- cgit v1.2.1 From 68326c0a5c242c22be81428c0bcd50b0cc32bf72 Mon Sep 17 00:00:00 2001 From: Todd Leonhardt Date: Wed, 7 Feb 2018 22:24:30 -0500 Subject: Added ability to use ipy for embedded IPython to submenus.py example This is simply to allow introspection on self for debug and development --- examples/submenus.py | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) (limited to 'examples') diff --git a/examples/submenus.py b/examples/submenus.py index 6fcbb307..52f26e08 100644 --- a/examples/submenus.py +++ b/examples/submenus.py @@ -10,9 +10,11 @@ of the submenu. Nesting of the submenus is done with the cmd2.AddSubmenu() decor """ - from __future__ import print_function +import sys + import cmd2 +from IPython import embed class ThirdLevel(cmd2.Cmd): @@ -48,6 +50,16 @@ class SecondLevel(cmd2.Cmd): self.top_level_attr = None self.second_level_attr = 987654321 + def do_ipy(self, arg): + """Enters an interactive IPython shell. + + Run python code from external files with ``run filename.py`` + End with ``Ctrl-D`` (Unix) / ``Ctrl-Z`` (Windows), ``quit()``, '`exit()``. + """ + banner = 'Entering an embedded IPython shell type quit() or -d to exit ...' + exit_msg = 'Leaving IPython, back to {}'.format(sys.argv[0]) + embed(banner1=banner, exit_msg=exit_msg) + def do_say(self, line): print("You called a command in SecondLevel with '%s'. " "It has access to top_level_attr: %s" % (line, self.top_level_attr)) @@ -72,6 +84,16 @@ class TopLevel(cmd2.Cmd): self.prompt = 'TopLevel ' self.top_level_attr = 123456789 + def do_ipy(self, arg): + """Enters an interactive IPython shell. + + Run python code from external files with ``run filename.py`` + End with ``Ctrl-D`` (Unix) / ``Ctrl-Z`` (Windows), ``quit()``, '`exit()``. + """ + banner = 'Entering an embedded IPython shell type quit() or -d to exit ...' + exit_msg = 'Leaving IPython, back to {}'.format(sys.argv[0]) + embed(banner1=banner, exit_msg=exit_msg) + def do_say(self, line): print("You called a command in TopLevel with '%s'. " "TopLevel has attribute top_level_attr=%s" % (line, self.top_level_attr)) -- cgit v1.2.1