Rust CLI Tool with Clap

Build a command-line tool in Rust using Clap with derive macros for subcommands, typed arguments, environment variable fallback, colored output, and shell.

Rust Cli Tooling

Code

snippet
use clap::{Parser, Subcommand};

#[derive(Parser)]
#[command(name = "mycli", version = "1.0")]
struct Cli {
    #[command(subcommand)]
    command: Commands,
}

#[derive(Subcommand)]
enum Commands {
    Build { input: String, #[arg(short, long, default_value = "dist")] output: String },
    Deploy { #[arg(long)] verbose: bool },
}

fn main() {
    let cli = Cli::parse();
    match cli.command {
        Commands::Build { input, output } => println!("Building {input} → {output}"),
        Commands::Deploy { verbose } => println!("Deploying (verbose: {verbose})"),
    }
}