|
| 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. |
0 commit comments