|
| 1 | +# Custom Commands |
| 2 | + |
| 3 | +Cecli allows you to create and use custom commands to extend its functionality. Custom commands are Python classes that extend the `BaseCommand` class and can be loaded from specified directories or files. |
| 4 | + |
| 5 | +## How Custom Commands Work |
| 6 | + |
| 7 | +### Command Registry System |
| 8 | + |
| 9 | +Cecli uses a centralized command registry that manages all available commands: |
| 10 | + |
| 11 | +- **Built-in Commands**: Standard commands like `/add`, `/model`, `/help`, etc. |
| 12 | +- **Custom Commands**: User-defined commands loaded from specified paths |
| 13 | +- **Command Discovery**: Automatic loading of commands from configured directories |
| 14 | + |
| 15 | +### Configuration |
| 16 | + |
| 17 | +Custom commands can be configured using the `command-paths` configuration option in your YAML configuration file: |
| 18 | + |
| 19 | +```yaml |
| 20 | +command-paths: [".cecli/commands/", "~/my-commands/", "./special_command.py"] |
| 21 | +``` |
| 22 | +
|
| 23 | +The `command-paths` configuration option allows you to specify directories or files containing custom commands to load. |
| 24 | + |
| 25 | +The `command-paths` can include: |
| 26 | +- **Directories**: All `.py` files in the directory will be scanned for `CustomCommand` classes |
| 27 | +- **Individual Python files**: Specific command files can be loaded directly |
| 28 | + |
| 29 | +When cecli starts, it: |
| 30 | +1. **Parses configuration**: Reads `command-paths` from config files |
| 31 | +2. **Scans directories**: Looks for Python files in specified directories |
| 32 | +3. **Loads modules**: Imports each Python file as a module |
| 33 | +4. **Registers commands**: Finds classes named `CustomCommand` and registers them |
| 34 | +5. **Makes available**: Registered commands appear in `/help` and can be executed |
| 35 | + |
| 36 | +### Creating Custom Commands |
| 37 | + |
| 38 | +Custom commands are created by writing Python files that follow this structure: |
| 39 | + |
| 40 | +```python |
| 41 | +from typing import List |
| 42 | +from cecli.commands.utils.base_command import BaseCommand |
| 43 | +from cecli.commands.utils.helpers import format_command_result |
| 44 | +
|
| 45 | +class CustomCommand(BaseCommand): |
| 46 | + NORM_NAME = "custom-command" |
| 47 | + DESCRIPTION = "Description of what the command does" |
| 48 | +
|
| 49 | + @classmethod |
| 50 | + async def execute(cls, io, coder, args, **kwargs): |
| 51 | + """ |
| 52 | + Execute the custom command. |
| 53 | +
|
| 54 | + Args: |
| 55 | + io: InputOutput instance |
| 56 | + coder: Coder instance (may be None for some commands) |
| 57 | + args: Command arguments as string |
| 58 | + **kwargs: Additional context |
| 59 | +
|
| 60 | + Returns: |
| 61 | + Optional result (most commands return None) |
| 62 | + """ |
| 63 | + # Command implementation here |
| 64 | + result = f"Command executed with arguments: {args}" |
| 65 | + return format_command_result(io, cls.NORM_NAME, result) |
| 66 | +
|
| 67 | + @classmethod |
| 68 | + def get_completions(cls, io, coder, args) -> List[str]: |
| 69 | + """ |
| 70 | + Get completion options for this command. |
| 71 | +
|
| 72 | + Args: |
| 73 | + io: InputOutput instance |
| 74 | + coder: Coder instance |
| 75 | + args: Partial arguments for completion |
| 76 | +
|
| 77 | + Returns: |
| 78 | + List of completion strings |
| 79 | + """ |
| 80 | + # Return completion options or raise CommandCompletionException |
| 81 | + # for dynamic completions |
| 82 | + return [] |
| 83 | +
|
| 84 | + @classmethod |
| 85 | + def get_help(cls) -> str: |
| 86 | + """ |
| 87 | + Get help text for this command. |
| 88 | +
|
| 89 | + Returns: |
| 90 | + String containing help text for the command |
| 91 | + """ |
| 92 | + help_text = super().get_help() |
| 93 | + help_text += "\nAdditional information about this custom command." |
| 94 | + return help_text |
| 95 | +``` |
| 96 | + |
| 97 | +### Important Requirements |
| 98 | + |
| 99 | +1. **Class Name**: The command class **must** be named exactly `CustomCommand` |
| 100 | +2. **Inheritance**: Must inherit from `BaseCommand` (from `cecli.commands.utils.base_command`) |
| 101 | +3. **Class Properties**: Must define `NORM_NAME` and `DESCRIPTION` class attributes |
| 102 | +4. **Execute Method**: Must implement the `execute` class method |
| 103 | + |
| 104 | +### Example: Add List Command |
| 105 | + |
| 106 | +Here's a complete example of a custom command that adds a list of numbers: |
| 107 | + |
| 108 | +```python |
| 109 | +from typing import List |
| 110 | +from cecli.commands.utils.base_command import BaseCommand |
| 111 | +from cecli.commands.utils.helpers import format_command_result |
| 112 | +
|
| 113 | +class CustomCommand(BaseCommand): |
| 114 | + NORM_NAME = "add-list" |
| 115 | + DESCRIPTION = "Add a list of numbers." |
| 116 | +
|
| 117 | + @classmethod |
| 118 | + async def execute(cls, io, coder, args, **kwargs): |
| 119 | + """Execute the context command with given parameters.""" |
| 120 | + num_list = map(int, filter(None, args.split(" "))) |
| 121 | + return format_command_result(io, cls.NORM_NAME, sum(num_list)) |
| 122 | +
|
| 123 | + @classmethod |
| 124 | + def get_completions(cls, io, coder, args) -> List[str]: |
| 125 | + """Get completion options for context command.""" |
| 126 | + # The original completions_context raises CommandCompletionException |
| 127 | + # This is handled by the completion system |
| 128 | + from cecli.io import CommandCompletionException |
| 129 | + raise CommandCompletionException() |
| 130 | +
|
| 131 | + @classmethod |
| 132 | + def get_help(cls) -> str: |
| 133 | + """Get help text for the context command.""" |
| 134 | + help_text = super().get_help() |
| 135 | + help_text += "Add list of integers" |
| 136 | + return help_text |
| 137 | +``` |
| 138 | + |
| 139 | +#### Complete Configuration Example |
| 140 | + |
| 141 | +Complete configuration example in YAML configuration file (`.cecli.conf.yml` or `~/.cecli.conf.yml`): |
| 142 | + |
| 143 | +```yaml |
| 144 | +# Model configuration |
| 145 | +model: gemini/gemini-3-pro-preview |
| 146 | +weak-model: gemini/gemini-3-flash-preview |
| 147 | +
|
| 148 | +# Custom commands configuration |
| 149 | +command-paths: [".cecli/commands/"] |
| 150 | +
|
| 151 | +# Other cecli options |
| 152 | +... |
| 153 | +``` |
| 154 | + |
| 155 | +### Error Handling |
| 156 | + |
| 157 | +If there are errors loading custom commands: |
| 158 | + |
| 159 | +- **Invalid paths**: Warnings are logged but cecli continues to run |
| 160 | +- **Syntax errors**: The specific file fails to load but other commands still work |
| 161 | +- **Missing requirements**: Commands that can't be imported are skipped |
| 162 | + |
| 163 | +### Best Practices |
| 164 | + |
| 165 | +1. **Organize commands**: Group related commands in the same directory |
| 166 | +2. **Use descriptive names**: Make `NORM_NAME` clear, memorable, and unique |
| 167 | +3. **Provide good help**: Implement `get_help()` with clear usage instructions |
| 168 | +4. **Handle errors gracefully**: Use `format_command_result()` for consistent output |
| 169 | +5. **Test commands**: Verify commands work before adding to production config |
| 170 | + |
| 171 | +### Integration with Other Features |
| 172 | + |
| 173 | +Custom commands work seamlessly with other cecli features: |
| 174 | + |
| 175 | +- **Command completion**: Custom commands appear in tab completion |
| 176 | +- **Help system**: Included in `/help` output |
| 177 | +- **TUI interface**: Available in the graphical interface |
| 178 | +- **Agent Mode**: Can be used alongside Agent Mode tools |
| 179 | + |
| 180 | +### Benefits |
| 181 | + |
| 182 | +- **Extensibility**: Add project-specific functionality |
| 183 | +- **Automation**: Create commands for repetitive tasks |
| 184 | +- **Integration**: Connect cecli with other tools and systems |
| 185 | +- **Customization**: Tailor cecli to your specific workflow |
| 186 | + |
| 187 | +Custom commands provide a powerful way to extend cecli's capabilities, allowing you to create specialized functionality for your specific needs while maintaining the familiar command interface. |
0 commit comments