Overview
Bench allows you to extend its functionality by creating custom commands within your Frappe applications. These commands become available through the bench CLI, making it easy to add application-specific functionality to your development workflow.How Custom Commands Work
Bench utilizesfrappe.utils.bench_manager to discover and load commands from:
- The Frappe framework itself
- Any custom applications installed in your bench environment
Creating Custom Commands
Directory Structure
To make custom commands available to bench, create acommands module under your application’s parent module:
commands module can be:
- A single file:
commands.py - A directory with an
__init__.pyfile
Command Structure
Custom commands use the Click library for command-line interface creation. Here’s the basic structure:Key Components
Example: Flag Management Command
Here’s a complete example for an application named ‘flags’:Usage
Once the command is defined, you can execute it from within your bench directory:Advanced Command Features
Multiple Arguments
Options with Values
Multiple Commands in One Module
Best Practices
Use Descriptive Command Names
Use Descriptive Command Names
Choose clear, descriptive names that follow bench’s naming convention (lowercase with hyphens).
Provide Help Text
Provide Help Text
Always include docstrings and help text for commands and options:
Handle Errors Gracefully
Handle Errors Gracefully
Implement proper error handling and provide meaningful error messages:
Use Click's Built-in Features
Use Click's Built-in Features
Leverage Click’s features for better user experience:
- Type validation:
type=int,type=float,type=click.Path() - Choices:
type=click.Choice(['opt1', 'opt2']) - Prompts:
prompt=True - Confirmation:
click.confirm('Continue?', abort=True)
Listing Available Commands
To see all available commands, including custom ones:Related Resources
Click Documentation
Learn more about the Click library
All Commands
See all available bench commands