| 1 | use clap::{Args, Parser, Subcommand}; |
| 2 | |
| 3 | #[derive(Parser)] |
| 4 | #[command(author, version, about, long_about = None)] |
| 5 | #[command(propagate_version = true)] |
| 6 | struct Cli { |
| 7 | #[command(subcommand)] |
| 8 | command: Commands, |
| 9 | } |
| 10 | |
| 11 | #[derive(Subcommand)] |
| 12 | enum Commands { |
| 13 | /// Adds files to myapp |
| 14 | Add(AddArgs), |
| 15 | } |
| 16 | |
| 17 | #[derive(Args)] |
| 18 | struct AddArgs { |
| 19 | name: Option<String>, |
| 20 | } |
| 21 | |
| 22 | fn 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 | } |
| 33 | |