// Created on savesnippets.com ยท https://savesnippets.com/AFVHI7wNjgL3b2 // Cargo.toml: clap = { version = "4", features = ["derive"] } use clap::{Parser, Subcommand}; #[derive(Parser, Debug)] #[command(name = "myapp", version, about = "Does a useful thing", long_about = None)] struct Cli { /// Verbose output (-v, -vv, -vvv) #[arg(short, long, action = clap::ArgAction::Count)] verbose: u8, /// Configuration file #[arg(short, long, default_value = "config.toml")] config: String, #[command(subcommand)] command: Command, } #[derive(Subcommand, Debug)] enum Command { /// Initialize a new project Init { path: String }, /// Run the server Run { #[arg(short, long, default_value_t = 8080)] port: u16, #[arg(long)] debug: bool, }, } fn main() { let cli = Cli::parse(); println!("config: {} verbose level: {}", cli.config, cli.verbose); match cli.command { Command::Init { path } => println!("initializing in {path}"), Command::Run { port, debug } => println!("server on :{port} (debug={debug})"), } } // Run: cargo run -- run --port 3000 --debug -vv