]>
git.proxmox.com Git - rustc.git/blob - vendor/clap-3.2.20/examples/git.rs
1 use std
::ffi
::OsString
;
2 use std
::path
::PathBuf
;
4 use clap
::{arg, Command}
;
6 fn cli() -> Command
<'
static> {
8 .about("A fictional versioning CLI")
9 .subcommand_required(true)
10 .arg_required_else_help(true)
11 .allow_external_subcommands(true)
12 .allow_invalid_utf8_for_external_subcommands(true)
15 .about("Clones repos")
16 .arg(arg
!(<REMOTE
> "The remote to clone"))
17 .arg_required_else_help(true),
21 .about("pushes things")
22 .arg(arg
!(<REMOTE
> "The remote to target"))
23 .arg_required_else_help(true),
28 .arg_required_else_help(true)
29 .arg(arg
!(<PATH
> ... "Stuff to add").value_parser(clap
::value_parser
!(PathBuf
))),
33 .args_conflicts_with_subcommands(true)
35 .subcommand(Command
::new("push").args(push_args()))
36 .subcommand(Command
::new("pop").arg(arg
!([STASH
])))
37 .subcommand(Command
::new("apply").arg(arg
!([STASH
]))),
41 fn push_args() -> Vec
<clap
::Arg
<'
static>> {
42 vec
![arg
!(-m
--message
<MESSAGE
>).required(false)]
46 let matches
= cli().get_matches();
48 match matches
.subcommand() {
49 Some(("clone", sub_matches
)) => {
52 sub_matches
.get_one
::<String
>("REMOTE").expect("required")
55 Some(("push", sub_matches
)) => {
58 sub_matches
.get_one
::<String
>("REMOTE").expect("required")
61 Some(("add", sub_matches
)) => {
62 let paths
= sub_matches
63 .get_many
::<PathBuf
>("PATH")
67 println
!("Adding {:?}", paths
);
69 Some(("stash", sub_matches
)) => {
70 let stash_command
= sub_matches
.subcommand().unwrap_or(("push", sub_matches
));
72 ("apply", sub_matches
) => {
73 let stash
= sub_matches
.get_one
::<String
>("STASH");
74 println
!("Applying {:?}", stash
);
76 ("pop", sub_matches
) => {
77 let stash
= sub_matches
.get_one
::<String
>("STASH");
78 println
!("Popping {:?}", stash
);
80 ("push", sub_matches
) => {
81 let message
= sub_matches
.get_one
::<String
>("message");
82 println
!("Pushing {:?}", message
);
85 unreachable
!("Unsupported subcommand `{}`", name
)
89 Some((ext
, sub_matches
)) => {
90 let args
= sub_matches
91 .get_many
::<OsString
>("")
95 println
!("Calling out to {:?} with {:?}", ext
, args
);
97 _
=> unreachable
!(), // If all subcommands are defined above, anything else is unreachabe!()
100 // Continued program logic goes here...