-
Notifications
You must be signed in to change notification settings - Fork 130
Doc additions #780
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Doc additions #780
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
65cf54a
command documentation for #765
kotfu 9fd5f62
Add documentation and example for removing built-in commands
kotfu b9d21a2
Document hiding and disabling commands
kotfu 74857c3
Merge branch 'master' into doc_additions
kotfu efadff3
Merge branch 'master' into doc_additions
tleonhardt b45fec9
Minor spelling and grammar fixes
tleonhardt File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,5 +5,4 @@ Examples | |
| :maxdepth: 1 | ||
|
|
||
| first_app | ||
| removing_builtin_commands | ||
| alternate_event_loops | ||
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,103 @@ | ||
| Disabling Commands | ||
| ================== | ||
|
|
||
| How to disable and re-enable commands, by individual command and by category | ||
| ``cmd2`` allows a developer to: | ||
|
|
||
| - remove commands included in ``cmd2`` | ||
| - prevent commands from appearing in the help menu (hide commands) | ||
| - disable and re-enable commands at runtime | ||
|
|
||
|
|
||
| Remove A Command | ||
| ---------------- | ||
|
|
||
| When a command has been removed, the command method has been deleted from the | ||
| object. The command doesn't show up in help, and it can't be executed. This | ||
| approach is appropriate if you never want a built-in command to be part of your | ||
| application. Delete the command method in your initialization code:: | ||
|
|
||
| class RemoveBuiltinCommand(cmd2.Cmd): | ||
| """An app which removes a built-in command from cmd2""" | ||
|
|
||
| def __init__(self): | ||
| super().__init__() | ||
| # To remove built-in commands entirely, delete | ||
| # the "do_*" function from the cmd2.Cmd class | ||
| del cmd2.Cmd.do_edit | ||
|
|
||
|
|
||
| Hide A Command | ||
| -------------- | ||
|
|
||
| When a command is hidden, it won't show up in the help menu, but if | ||
| the user knows it's there and types the command, it will be executed. | ||
| You hide a command by adding it to the ``hidden_commands`` list:: | ||
|
|
||
| class HiddenCommands(cmd2.Cmd): | ||
| ""An app which demonstrates how to hide a command""" | ||
| def __init__(self): | ||
| super().__init__() | ||
| self.hidden_commands.append('py') | ||
|
|
||
| As shown above, you would typically do this as part of initializing your | ||
| application. If you decide you want to unhide a command later in the execution | ||
| of your application, you can by doing:: | ||
|
|
||
| self.hidden_commands = [cmd for cmd in self.hidden_commands if cmd != 'py'] | ||
|
|
||
| You might be thinking that the list comprehension is overkill and you'd rather | ||
| do something like:: | ||
|
|
||
| self.hidden_commands.remove('py') | ||
|
|
||
| You may be right, but ``remove()`` will raise a ``ValueError`` if ``py`` | ||
| isn't in the list, and it will only remove the first one if it's in the list | ||
| multiple times. | ||
|
|
||
|
|
||
| Disable A Command | ||
| ----------------- | ||
|
|
||
| One way to disable a command is to add code to the command method which | ||
| determines whether the command should be executed or not. If the command should | ||
| not be executed, your code can print an appropriate error message and return. | ||
|
|
||
| ``cmd2`` also provides another way to accomplish the same thing. Here's a | ||
| simple app which disables the ``open`` command if the door is locked:: | ||
|
|
||
| class DisabledCommands(cmd2.Cmd): | ||
| """An application which disables and enables commands""" | ||
|
|
||
| def do_lock(self, line): | ||
| self.disable_command('open', "you can't open the door because it is locked") | ||
| self.poutput('the door is locked') | ||
|
|
||
| def do_unlock(self, line): | ||
| self.enable_command('open') | ||
| self.poutput('the door is unlocked') | ||
|
|
||
| def do_open(self, line): | ||
| """open the door""" | ||
| self.poutput('opening the door') | ||
|
|
||
| This method has the added benefit of removing disabled commands from the help | ||
| menu. But, this method only works if you know in advance that the command | ||
| should be disabled, and if the conditions for re-enabling it are likewise known | ||
| in advance. | ||
|
|
||
|
|
||
| Disable A Category of Commands | ||
| ------------------------------ | ||
|
|
||
| You can group or categorize commands as shown in | ||
| :ref:`features/help:Categorizing Commands`. If you do so, you can disable and | ||
| enable all the commands in a category with a single method call. Say you have | ||
| created a category of commands called "Server Information". You can disable | ||
| all commands in that category:: | ||
|
|
||
| not_connected_msg = 'You must be connected to use this command' | ||
| self.disable_category('Server Information', not_connected_msg) | ||
|
|
||
| Similarly, you can re-enable all the commands in a category:: | ||
|
|
||
| self.enable_category('Server Information') |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@kotfu You did a great job improving the documentation in this file. I really like how it flows.