Arguments Guide

Defining arguments with XaCLI derive macros

Arguments Guide

#[arg(...)] Attributes

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

#[derive(Command)]
struct MyCmd {
    /// Short flag: -v
    #[arg(short = 'v')]
    verbose: bool,

    /// Long flag: --output
    #[arg(long = "output")]
    output: bool,

    /// Both: -p, --port
    #[arg(short = 'p', long = "port")]
    port: bool,

    /// Positional argument
    #[arg(positional)]
    file: bool,
}

Attribute Reference

AttributeDescriptionExample
shortShort flag#[arg(short = 'v')]-v
longLong flag#[arg(long = "verbose")]--verbose
positionalPositional arg#[arg(positional)]

Usage Examples

myapp -v                    # short flag
myapp --verbose             # long flag
myapp -p 8080               # short with value
myapp --port 8080           # long with value
myapp input.txt             # positional
myapp input.txt -v --port 3000  # combined

Accessing Arguments

Arguments are accessed via the Context in command handlers:

impl MyCmd {
    fn run(&self, ctx: &mut dyn Context) -> xacli::Result<()> {
        // Access parsed arguments through ctx
        use std::io::Write;
        writeln!(ctx.stdout(), "Running command...")?;
        Ok(())
    }
}