Debian Codegraph @

docs/features/plugins.md

raw symbols SCIP
1 # Plugins
2
3 `cmd2` has a built-in plugin framework which allows developers to create a a `cmd2` plugin which can
4 extend basic `cmd2` functionality and can be used by multiple applications.
5
6 There are many ways to add functionality to `cmd2` using a plugin. Most plugins will be implemented
7 as a mixin. A mixin is a class that encapsulates and injects code into another class. Developers who
8 use a plugin in their `cmd2` project will inject the plugin's code into their subclass of
9 [cmd2.Cmd][].
10
11 ## Mixin and Initialization
12
13 The following short example shows how to mix in a plugin and how the plugin gets initialized.
14
15 Here's the plugin:
16
17 ```py
18 class MyPlugin:
19 def __init__(self, *args, **kwargs):
20 # code placed here runs before cmd2.Cmd initializes
21 super().__init__(*args, **kwargs)
22 # code placed here runs after cmd2.Cmd initializes
23 ```
24
25 and an example app which uses the plugin:
26
27 ```py
28 import cmd2
29 import cmd2_myplugin
30
31 class Example(cmd2_myplugin.MyPlugin, cmd2.Cmd):
32 """An class to show how to use a plugin"""
33 def __init__(self, *args, **kwargs):
34 # code placed here runs before cmd2.Cmd or
35 # any plugins initialize
36 super().__init__(*args, **kwargs)
37 # code placed here runs after cmd2.Cmd and
38 # all plugins have initialized
39 ```
40
41 !!! warning
42
43 The plugin must be inherited (or mixed in) before `cmd2.Cmd`. This is required for two
44 reasons:
45
46 - The `cmd.Cmd.__init__` method in the Python standard library does not call `super().__init__()`.
47 Because of this oversight, if you don't inherit from `MyPlugin` first, the `MyPlugin.__init__()`
48 method will never be called.
49 - You may want your plugin to be able to override methods from `cmd2.Cmd`. If you mixin the plugin
50 after `cmd2.Cmd`, the Python method resolution order will call [cmd2.Cmd][] methods before it
51 calls those in your plugin.
52
53 ## Add commands
54
55 Your plugin can add user visible commands. You do it the same way in a plugin that you would in a
56 `cmd2.Cmd` app:
57
58 ```py
59 class MyPlugin:
60 def do_say(self, statement):
61 """Simple say command"""
62 self.poutput(statement)
63 ```
64
65 You have all the same capabilities within the plugin that you do inside a `cmd2.Cmd` app, including
66 argument parsing via decorators and custom help methods.
67
68 ## Add (or hide) settings
69
70 A plugin may add user controllable settings to the application. Here's an example:
71
72 ```py
73 class MyPlugin:
74 def __init__(self, *args, **kwargs):
75 # code placed here runs before cmd2.Cmd initializes
76 super().__init__(*args, **kwargs)
77 # code placed here runs after cmd2.Cmd initializes
78 self.mysetting = 'somevalue'
79 self.add_settable(cmd2.Settable('mysetting', str, 'short help message for mysetting', self))
80 ```
81
82 You can hide settings from the user by calling [cmd2.Cmd.remove_settable][]. See
83 [Settings](./settings.md) for more information.
84
85 ## Decorators
86
87 Your plugin can provide a decorator which users of your plugin can use to wrap functionality around
88 their own commands.
89
90 ## Override methods
91
92 Your plugin can override core `cmd2.Cmd` methods, changing their behavior. This approach should be
93 used sparingly, because it is very brittle. If a developer chooses to use multiple plugins in their
94 application, and several of the plugins override the same method, only the first plugin to be mixed
95 in will have the overridden method called.
96
97 Hooks are a much better approach.
98
99 ## Hooks
100
101 Plugins can register hook methods, which are called by [cmd2.Cmd][] during various points in the
102 application and command processing lifecycle. Plugins should not override any of the `cmd` base
103 class hook methods, instead they should register their hooks as described in the [Hooks](./hooks.md)
104 section.
105
106 You should name your hooks so that they begin with the name of your plugin. Hook methods get mixed
107 into the `cmd2` application and this naming convention helps avoid unintentional method overriding.
108
109 Here's a simple example:
110
111 ```py
112 class MyPlugin:
113 def __init__(self, *args, **kwargs):
114 # code placed here runs before cmd2 initializes
115 super().__init__(*args, **kwargs)
116 # code placed here runs after cmd2 initializes
117 # this is where you register any hook functions
118 self.register_postparsing_hook(self.cmd2_myplugin_postparsing_hook)
119
120 def cmd2_myplugin_postparsing_hook(self, data: cmd2.plugin.PostparsingData) -> cmd2.plugin.PostparsingData:
121 """Method to be called after parsing user input, but before running the command"""
122 self.poutput('in postparsing_hook')
123 return data
124 ```
125
126 Registration allows multiple plugins (or even the application itself) to each inject code to be
127 called during the application or command processing lifecycle.
128
129 See the [Hooks](./hooks.md) documentation for full details of the application and command lifecycle,
130 including all available hooks and the ways hooks can influence the lifecycle.
131
132 ## Classes and Functions
133
134 Your plugin can also provide classes and functions which can be used by developers of `cmd2` based
135 applications. Describe these classes and functions in your documentation so users of your plugin
136 will know what's available.
137
138 ## Examples
139
140 See [cmd2 Plugin Template](https://github.com/python-cmd2/cmd2-plugin-template) for more info.