From 9fd5f6274da443db4f37273d1284e6d09f94aab5 Mon Sep 17 00:00:00 2001 From: kotfu Date: Fri, 23 Aug 2019 19:09:02 -0600 Subject: Add documentation and example for removing built-in commands For #765 --- examples/remove_builtin_commands.py | 31 +++++++++++++++++++++++++++++++ examples/remove_unused.py | 31 ------------------------------- 2 files changed, 31 insertions(+), 31 deletions(-) create mode 100755 examples/remove_builtin_commands.py delete mode 100755 examples/remove_unused.py (limited to 'examples') diff --git a/examples/remove_builtin_commands.py b/examples/remove_builtin_commands.py new file mode 100755 index 00000000..4c1794d6 --- /dev/null +++ b/examples/remove_builtin_commands.py @@ -0,0 +1,31 @@ +#!/usr/bin/env python +# coding=utf-8 +"""A simple example demonstrating how to remove unused commands. + +Commands can be removed from help menu and tab completion by appending their command name to the hidden_commands list. +These commands will still exist and can be executed and help can be retrieved for them by +name, they just won't clutter the help menu. + +Commands can also be removed entirely by using Python's "del". +""" + +import cmd2 + + +class RemoveBuiltinCommands(cmd2.Cmd): + """ Example cmd2 application where we remove some unused built-in commands.""" + + def __init__(self): + super().__init__() + + # To hide commands from displaying in the help menu, add them to the hidden_commands list + self.hidden_commands.append('py') + + # To remove built-in commands entirely, delete their "do_*" function from the cmd2.Cmd class + del cmd2.Cmd.do_edit + + +if __name__ == '__main__': + import sys + app = RemoveBuiltinCommands() + sys.exit(app.cmdloop()) diff --git a/examples/remove_unused.py b/examples/remove_unused.py deleted file mode 100755 index 62103022..00000000 --- a/examples/remove_unused.py +++ /dev/null @@ -1,31 +0,0 @@ -#!/usr/bin/env python -# coding=utf-8 -"""A simple example demonstrating how to remove unused commands. - -Commands can be removed from help menu and tab completion by appending their command name to the hidden_commands list. -These commands will still exist and can be executed and help can be retrieved for them by -name, they just won't clutter the help menu. - -Commands can also be removed entirely by using Python's "del". -""" - -import cmd2 - - -class RemoveUnusedBuiltinCommands(cmd2.Cmd): - """ Example cmd2 application where we remove some unused built-in commands.""" - - def __init__(self): - super().__init__() - - # To hide commands from displaying in the help menu, add them to the hidden_commands list - self.hidden_commands.append('py') - - # To remove built-in commands entirely, delete their "do_*" function from the cmd2.Cmd class - del cmd2.Cmd.do_edit - - -if __name__ == '__main__': - import sys - app = RemoveUnusedBuiltinCommands() - sys.exit(app.cmdloop()) -- cgit v1.2.1