Derive Macros Reference

Reference for xacli derive macros

Derive Macros Reference

use xacli::derive::{App, Command};

#[derive(App)]

Application entry point.

#[derive(App)]
#[app(
    name = "myapp",
    version = "1.0.0",
    title = "My App",
    description = "A CLI application"
)]
struct MyApp {
    #[command(subcommands)]
    commands: Commands,
}

#[app(...)] Attributes

AttributeRequiredDescription
nameYesCLI binary name
versionNoVersion string
titleNoDisplay title
descriptionNoHelp text

#[derive(Command)]

Commands and subcommands.

Enum (Subcommand Container)

#[derive(Command)]
enum Commands {
    /// Help text from doc comment
    Add(AddCmd),
    Remove(RemoveCmd),
}

Struct (Leaf Command)

#[derive(Command)]
#[command(description = "Add an item")]
struct AddCmd {
    #[arg(short = 'n', long = "name")]
    name: bool,
}

#[command(...)] Attributes

AttributeDescription
descriptionCommand help text
nameOverride command name
subcommandsMark field as subcommand container

#[arg(...)]

Argument definition on struct fields.

#[arg(short = 'v', long = "verbose")]
verbose: bool,

#[arg(positional)]
file: bool,

#[arg(...)] Attributes

AttributeDescription
shortShort flag (-x)
longLong flag (--name)
positionalPositional argument

Execution

fn main() {
    if let Err(e) = MyApp::execute() {
        eprintln!("Error: {}", e);
        std::process::exit(1);
    }
}