03_04_subcommands_alt_derive/
03_04_subcommands_alt.rs

1use clap::{Args, Parser, Subcommand};
2
3#[derive(Parser)]
4#[command(version, about, long_about = None)]
5#[command(propagate_version = true)]
6struct Cli {
7    #[command(subcommand)]
8    command: Commands,
9}
10
11#[derive(Subcommand)]
12enum Commands {
13    /// Adds files to myapp
14    Add(AddArgs),
15}
16
17#[derive(Args)]
18struct AddArgs {
19    name: Option<String>,
20}
21
22fn main() {
23    let cli = Cli::parse();
24
25    // You can check for the existence of subcommands, and if found use their
26    // matches just as you would the top level cmd
27    match &cli.command {
28        Commands::Add(name) => {
29            println!("'myapp add' was used, name is: {:?}", name.name);
30        }
31    }
32}