clap/examples/git-derive.rs

108 lines
2.5 KiB
Rust
Raw Normal View History

2021-12-09 01:25:48 +00:00
// Note: this requires the `derive` feature
use std::ffi::OsString;
use std::path::PathBuf;
use clap::{Args, Parser, Subcommand};
/// A fictional versioning CLI
#[derive(Debug, Parser)]
#[clap(name = "git")]
#[clap(about = "A fictional versioning CLI", long_about = None)]
struct Cli {
#[clap(subcommand)]
command: Commands,
}
#[derive(Debug, Subcommand)]
enum Commands {
/// Clones repos
#[clap(arg_required_else_help = true)]
Clone {
/// The remote to clone
2022-05-21 00:52:04 +00:00
#[clap(value_parser)]
remote: String,
},
/// pushes things
#[clap(arg_required_else_help = true)]
Push {
/// The remote to target
2022-05-21 00:52:04 +00:00
#[clap(value_parser)]
remote: String,
},
/// adds things
#[clap(arg_required_else_help = true)]
Add {
/// Stuff to add
2022-05-21 00:52:04 +00:00
#[clap(required = true, value_parser)]
path: Vec<PathBuf>,
},
Stash(Stash),
#[clap(external_subcommand)]
External(Vec<OsString>),
}
#[derive(Debug, Args)]
#[clap(args_conflicts_with_subcommands = true)]
struct Stash {
#[clap(subcommand)]
command: Option<StashCommands>,
#[clap(flatten)]
push: StashPush,
}
#[derive(Debug, Subcommand)]
enum StashCommands {
Push(StashPush),
2022-05-21 00:52:04 +00:00
Pop {
#[clap(value_parser)]
stash: Option<String>,
},
Apply {
#[clap(value_parser)]
stash: Option<String>,
},
}
#[derive(Debug, Args)]
struct StashPush {
2022-05-21 00:52:04 +00:00
#[clap(short, long, value_parser)]
message: Option<String>,
}
fn main() {
let args = Cli::parse();
match args.command {
Commands::Clone { remote } => {
println!("Cloning {}", remote);
}
Commands::Push { remote } => {
println!("Pushing to {}", remote);
}
Commands::Add { path } => {
println!("Adding {:?}", path);
}
Commands::Stash(stash) => {
let stash_cmd = stash.command.unwrap_or(StashCommands::Push(stash.push));
match stash_cmd {
StashCommands::Push(push) => {
println!("Pushing {:?}", push);
}
StashCommands::Pop { stash } => {
println!("Popping {:?}", stash);
}
StashCommands::Apply { stash } => {
println!("Applying {:?}", stash);
}
}
}
Commands::External(args) => {
println!("Calling out to {:?} with {:?}", &args[0], &args[1..]);
}
}
// Continued program logic goes here...
}