#[command]Expand description
Add a command to a Clawless application
This macro attribute can be used to register a function as a (sub)command in a Clawless application. The name of the function will be used as the name of the command, and it will be automatically registered as a subcommand under its parent module.
Command functions must accept exactly two parameters:
- An
argsparameter: aclap::Argsstruct with the command’s arguments - A
contextparameter: theContextproviding access to the application environment and the cancellation token for cooperative shutdown
§Attributes
alias = "name"- Add a visible alias for the command. Can be repeated for multiple aliases.require_subcommand- Require a subcommand; show help if the command is invoked without one.
§Requiring Subcommands
Use require_subcommand to create a command that serves as a container for subcommands. When
this attribute is set, invoking the command without a subcommand will display help instead of
running the command body. This is useful for organizing related commands under a common prefix.
For example, a CLI might have db migrate, db seed, and db reset commands, where db
itself requires a subcommand and doesn’t perform any action on its own.
§Examples
Basic command:
use clawless::prelude::*;
#[derive(Debug, Args)]
pub struct GreetArgs {
#[arg(short, long)]
name: String,
}
#[command]
pub async fn greet(args: GreetArgs, context: Context) -> CommandResult {
println!("Hello, {}!", args.name);
Ok(())
}Command with alias:
use clawless::prelude::*;
#[derive(Debug, Args)]
pub struct GenerateArgs {}
// Users can run `mycli generate` or `mycli g`
#[command(alias = "g")]
pub async fn generate(args: GenerateArgs, context: Context) -> CommandResult {
Ok(())
}Command that requires a subcommand:
use clawless::prelude::*;
#[derive(Debug, Args)]
pub struct DbArgs {}
// Running `mycli db` shows help; users must specify a subcommand like `mycli db migrate`
#[command(require_subcommand, alias = "d")]
pub async fn db(args: DbArgs, context: Context) -> CommandResult {
Ok(())
}