Skip to content

Commit 749311e

Browse files
committed
docs: add command parametes and renamed provisioning
1 parent 557b6cb commit 749311e

4 files changed

Lines changed: 138 additions & 9 deletions

File tree

Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
Command Parameters
2+
==================
3+
4+
This page explains how to handle command parameters in Errbot plugins, including default values, argument parsing, and best practices.
5+
6+
Basic Command Parameters
7+
------------------------
8+
9+
The most basic form of a bot command takes a message object and arguments:
10+
11+
.. code-block:: python
12+
13+
@botcmd
14+
def hello(self, msg, args):
15+
return f"Hello! You said: {args}"
16+
17+
In this case, ``msg`` is the message object containing information about who sent the command and where, and ``args`` is a string containing everything after the command.
18+
19+
Default Values
20+
--------------
21+
22+
You can provide default values for command parameters. This is useful when you want to make certain arguments optional:
23+
24+
.. code-block:: python
25+
26+
@botcmd
27+
def echo(self, msg, args="default message"):
28+
return f"You said: {args}"
29+
30+
In this example, if someone calls the command without arguments, ``args`` will be set to "default message".
31+
32+
.. note::
33+
Default values work for both the ``msg`` and ``args`` parameters. However, it's recommended to only use default values for ``args`` as the ``msg`` parameter is typically required for proper command handling.
34+
35+
Argument Splitting
36+
------------------
37+
38+
You can automatically split arguments into a list using the ``split_args_with`` parameter:
39+
40+
.. code-block:: python
41+
42+
@botcmd(split_args_with=None) # Split on any whitespace
43+
def count(self, msg, args):
44+
# If user types: !count one two three
45+
# args will be ['one', 'two', 'three']
46+
return f"You provided {len(args)} arguments"
47+
48+
The ``split_args_with`` parameter works exactly like Python's ``str.split()``. Common values are:
49+
50+
- ``None``: Split on any whitespace (recommended for most cases)
51+
- ``' '``: Split on single spaces only
52+
- ``','``: Split on commas
53+
- ``'|'``: Split on pipe characters
54+
55+
Advanced Argument Parsing
56+
-------------------------
57+
58+
For more complex argument parsing, you can use the ``arg_botcmd`` decorator which provides argparse-style argument handling:
59+
60+
.. code-block:: python
61+
62+
@arg_botcmd('name', type=str)
63+
@arg_botcmd('--count', dest='repeat', type=int, default=1)
64+
def repeat(self, msg, name=None, repeat=None):
65+
return name * repeat
66+
67+
This allows for:
68+
- Type checking and conversion
69+
- Optional arguments with defaults
70+
- Named arguments
71+
- Help text generation
72+
73+
Best Practices
74+
--------------
75+
76+
1. **Parameter Order**: Always keep parameters in the order ``(self, msg, args)`` for consistency.
77+
78+
2. **Default Values**: Use default values for optional parameters, but be careful with the ``msg`` parameter as it's usually required.
79+
80+
3. **Argument Splitting**: Use ``split_args_with=None`` when you need to handle multiple space-separated arguments.
81+
82+
4. **Type Safety**: Use ``arg_botcmd`` when you need type checking or complex argument parsing.
83+
84+
5. **Documentation**: Always document your command's parameters and expected usage in the function's docstring.
85+
86+
Example with All Features
87+
-------------------------
88+
89+
Here's a complete example showing various parameter handling techniques:
90+
91+
.. code-block:: python
92+
93+
@arg_botcmd('name', type=str, help='The name to greet')
94+
@arg_botcmd('--count', dest='repeat', type=int, default=1, help='Number of times to repeat')
95+
@arg_botcmd('--shout', dest='shout', action='store_true', help='Convert to uppercase')
96+
def greet(self, msg, name=None, repeat=None, shout=False):
97+
"""Greet someone with a customizable message.
98+
99+
Example:
100+
!greet Alice --count 3 --shout
101+
"""
102+
if not name:
103+
return "Please provide a name to greet"
104+
105+
message = f"Hello, {name}!"
106+
if shout:
107+
message = message.upper()
108+
109+
return message * repeat
110+
111+
This command demonstrates:
112+
- Required and optional arguments
113+
- Type conversion
114+
- Default values
115+
- Boolean flags
116+
- Help text
117+
- Proper documentation
118+
119+
Common Pitfalls
120+
---------------
121+
122+
1. **Default Values for msg**: While possible, it's generally not recommended to provide default values for the ``msg`` parameter as it's essential for command context.
123+
124+
2. **Argument Splitting**: Remember that ``split_args_with=None`` splits on any whitespace, which might not be what you want if you need to preserve spaces in arguments.
125+
126+
3. **Type Conversion**: When using ``arg_botcmd``, always specify the correct type for arguments to ensure proper conversion and validation.
127+
128+
4. **Parameter Names**: Keep parameter names consistent with the decorator's expectations (``msg`` and ``args`` for basic commands, or the names specified in ``arg_botcmd``).
129+
130+
5. **Documentation**: Always include examples in your docstrings to help users understand how to use your commands correctly.

‎docs/user_guide/plugin_development/index.rst‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,14 @@ with sets of recipes on a range of topics describing how to handle more advanced
1414
development_environment
1515
basics
1616
botcommands
17+
command_parameters
1718
messaging
1819
threaded_replies
1920
presence
2021
mentions
2122
persistence
2223
configuration
24+
provisioning
2325
streams
2426
dependencies
2527
dynaplugs

docs/user_guide/provisioning.rst renamed to docs/user_guide/plugin_development/provisioning.rst

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,13 @@ It will give you on stdout a python dictionary of the core namespace like::
3333

3434
{'configs': {'Webserver': {'PORT': 8888}}}
3535

36-
To read the values from a plugin storage, for example here from alimac/err-factoid you can do::
36+
To read the values from a plugin storage, for example here from the ChatRoom plugin you can do::
3737

38-
errbot --storage-get Factoid
38+
errbot --storage-get ChatRoom
3939

4040
It will give you on stdout a similar output::
4141

42-
{'FACTOID': {'fire': 'burns', 'water': 'wet'}}
42+
{'rooms': ['#general', '#support']}
4343

4444

4545
Writing values
@@ -54,11 +54,8 @@ Checking back::
5454
errbot --storage-get core
5555
{'configs': {'Webserver': {'PORT': 9999}}}
5656

57-
Changing facts in Factoid (note the merge is only on the first level so we change all FACTOID here)::
57+
Changing plugin storage values (note the merge is only on the first level)::
5858

59-
echo "{'FACTOID': {'errbot': 'awesome'}}" | errbot --storage-merge Factoid
60-
61-
>>> !errbot?
62-
errbot is awesome
59+
echo "{'rooms': ['#general', '#support', '#dev']}" | errbot --storage-merge ChatRoom
6360

6461
You can use --storage-set in the same fashion but it will erase first the namespace before writing your values.

‎docs/user_guide/setup.rst‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ on `PyPI <https://pypi.org/project/errbot/>`_.
197197
Provisioning (advanced)
198198
-----------------------
199199

200-
See the :doc:`provisioning documentation </user_guide/provisioning>`
200+
See the :doc:`provisioning documentation </user_guide/plugin_development/provisioning>`
201201

202202
.. _virtualenv: https://virtualenv.pypa.io/en/latest/
203203
.. _pip: https://pip.pypa.io/en/stable/

0 commit comments

Comments
 (0)