Getting Started with XaCLI

Quick start guide to build your first CLI application with XaCLI

⏱️ 5 minutes

Getting Started with XaCLI

Installation

[dependencies]
xacli = { version = "0.1", features = ["derive"] }

Your First CLI App

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

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

#[derive(Command)]
enum Commands {
    /// Say hello
    Hello(HelloCmd),
}

#[derive(Command)]
struct HelloCmd {
    #[arg(short = 'n', long = "name")]
    name: bool,
}

impl HelloCmd {
    fn run(&self, ctx: &mut dyn Context) -> xacli::Result<()> {
        use std::io::Write;
        writeln!(ctx.stdout(), "Hello, World!")?;
        Ok(())
    }
}

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

Run

cargo run -- --help
cargo run -- hello

Next Steps

  • See the derive example: cargo run -p xacli --example derive --features derive,testing
  • Add interactive components with features = ["components"]
  • Add testing with features = ["testing"]
Hello, Alice!

With --help:

myapp - My first CLI app

USAGE:
    myapp [OPTIONS]

OPTIONS:
    -h, --help           Show help information
    -n, --name <NAME>    The name to greet [default: World]
    -v, --verbose        Enable verbose output
    -V, --version        Show version information

Next Steps