Commands Guide
Defining commands and subcommands with XaCLI derive macros
Commands Guide
Application Definition
use xacli::derive::{App, Command};
use xacli::Context;
#[derive(App)]
#[app(
name = "myapp",
version = "1.0.0",
title = "My App",
description = "A CLI application"
)]
struct MyApp {
#[command(subcommands)]
commands: Commands,
}
Subcommands (Enum)
#[derive(Command)]
enum Commands {
/// Add a new item
Add(AddCmd),
/// Remove an item
Remove(RemoveCmd),
}
Leaf Command (Struct)
#[derive(Command)]
#[command(description = "Add an item")]
struct AddCmd {
#[arg(short = 'n', long = "name")]
name: bool,
}
impl AddCmd {
fn run(&self, ctx: &mut dyn Context) -> xacli::Result<()> {
use std::io::Write;
writeln!(ctx.stdout(), "Adding item...")?;
Ok(())
}
}
Nested Command Groups
#[derive(Command)]
#[command(description = "Database commands")]
struct DbCmd {
#[command(subcommands)]
action: DbActions,
}
#[derive(Command)]
enum DbActions {
Migrate(MigrateCmd),
Seed(SeedCmd),
}
Attribute Reference
#[app(...)] - Application
| Attribute | Description |
|---|
name | CLI name |
version | Version string |
title | Display title |
description | Help text |
#[command(...)] - Commands
| Attribute | Description |
|---|
description | Command help |
name | Override name |
subcommands | Mark as container |
#[arg(...)] - Arguments
| Attribute | Description |
|---|
short | Short flag (-x) |
long | Long flag (--name) |
positional | Positional arg |
Execution
fn main() {
if let Err(e) = MyApp::execute() {
eprintln!("Error: {}", e);
std::process::exit(1);
}
}