]> git.proxmox.com Git - rustc.git/blob - vendor/clap/examples/git.rs
New upstream version 1.62.1+dfsg1
[rustc.git] / vendor / clap / examples / git.rs
1 // Note: this requires the `cargo` feature
2
3 use std::path::PathBuf;
4
5 use clap::{arg, Command};
6
7 fn main() {
8 let matches = Command::new("git")
9 .about("A fictional versioning CLI")
10 .subcommand_required(true)
11 .arg_required_else_help(true)
12 .allow_external_subcommands(true)
13 .allow_invalid_utf8_for_external_subcommands(true)
14 .subcommand(
15 Command::new("clone")
16 .about("Clones repos")
17 .arg(arg!(<REMOTE> "The remote to clone"))
18 .arg_required_else_help(true),
19 )
20 .subcommand(
21 Command::new("push")
22 .about("pushes things")
23 .arg(arg!(<REMOTE> "The remote to target"))
24 .arg_required_else_help(true),
25 )
26 .subcommand(
27 Command::new("add")
28 .about("adds things")
29 .arg_required_else_help(true)
30 .arg(arg!(<PATH> ... "Stuff to add").allow_invalid_utf8(true)),
31 )
32 .get_matches();
33
34 match matches.subcommand() {
35 Some(("clone", sub_matches)) => {
36 println!(
37 "Cloning {}",
38 sub_matches.value_of("REMOTE").expect("required")
39 );
40 }
41 Some(("push", sub_matches)) => {
42 println!(
43 "Pushing to {}",
44 sub_matches.value_of("REMOTE").expect("required")
45 );
46 }
47 Some(("add", sub_matches)) => {
48 let paths = sub_matches
49 .values_of_os("PATH")
50 .unwrap_or_default()
51 .map(PathBuf::from)
52 .collect::<Vec<_>>();
53 println!("Adding {:?}", paths);
54 }
55 Some((ext, sub_matches)) => {
56 let args = sub_matches
57 .values_of_os("")
58 .unwrap_or_default()
59 .collect::<Vec<_>>();
60 println!("Calling out to {:?} with {:?}", ext, args);
61 }
62 _ => unreachable!(), // If all subcommands are defined above, anything else is unreachabe!()
63 }
64
65 // Continued program logic goes here...
66 }