2022-05-23 15:50:11 +00:00
|
|
|
use std::ffi::OsString;
|
2021-11-30 18:30:19 +00:00
|
|
|
use std::path::PathBuf;
|
|
|
|
|
2022-02-12 03:48:29 +00:00
|
|
|
use clap::{arg, Command};
|
2021-11-30 18:30:19 +00:00
|
|
|
|
2022-08-15 19:29:46 +00:00
|
|
|
fn cli() -> Command {
|
2022-03-23 16:08:07 +00:00
|
|
|
Command::new("git")
|
2021-11-30 18:30:19 +00:00
|
|
|
.about("A fictional versioning CLI")
|
2022-02-11 20:40:04 +00:00
|
|
|
.subcommand_required(true)
|
|
|
|
.arg_required_else_help(true)
|
2022-02-10 17:51:40 +00:00
|
|
|
.allow_external_subcommands(true)
|
2021-11-30 18:30:19 +00:00
|
|
|
.subcommand(
|
2022-02-12 03:48:29 +00:00
|
|
|
Command::new("clone")
|
2021-11-30 18:30:19 +00:00
|
|
|
.about("Clones repos")
|
|
|
|
.arg(arg!(<REMOTE> "The remote to clone"))
|
2022-02-10 17:51:40 +00:00
|
|
|
.arg_required_else_help(true),
|
2021-11-30 18:30:19 +00:00
|
|
|
)
|
|
|
|
.subcommand(
|
2022-02-12 03:48:29 +00:00
|
|
|
Command::new("push")
|
2021-11-30 18:30:19 +00:00
|
|
|
.about("pushes things")
|
|
|
|
.arg(arg!(<REMOTE> "The remote to target"))
|
2022-02-10 17:51:40 +00:00
|
|
|
.arg_required_else_help(true),
|
2021-11-30 18:30:19 +00:00
|
|
|
)
|
|
|
|
.subcommand(
|
2022-02-12 03:48:29 +00:00
|
|
|
Command::new("add")
|
2021-11-30 18:30:19 +00:00
|
|
|
.about("adds things")
|
2022-02-10 17:51:40 +00:00
|
|
|
.arg_required_else_help(true)
|
2022-05-23 15:50:11 +00:00
|
|
|
.arg(arg!(<PATH> ... "Stuff to add").value_parser(clap::value_parser!(PathBuf))),
|
2021-11-30 18:30:19 +00:00
|
|
|
)
|
2022-03-23 16:08:07 +00:00
|
|
|
.subcommand(
|
|
|
|
Command::new("stash")
|
|
|
|
.args_conflicts_with_subcommands(true)
|
|
|
|
.args(push_args())
|
|
|
|
.subcommand(Command::new("push").args(push_args()))
|
|
|
|
.subcommand(Command::new("pop").arg(arg!([STASH])))
|
|
|
|
.subcommand(Command::new("apply").arg(arg!([STASH]))),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2022-08-15 19:29:46 +00:00
|
|
|
fn push_args() -> Vec<clap::Arg> {
|
2022-03-23 16:08:07 +00:00
|
|
|
vec![arg!(-m --message <MESSAGE>).required(false)]
|
|
|
|
}
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
let matches = cli().get_matches();
|
2021-11-30 18:30:19 +00:00
|
|
|
|
|
|
|
match matches.subcommand() {
|
|
|
|
Some(("clone", sub_matches)) => {
|
|
|
|
println!(
|
|
|
|
"Cloning {}",
|
2022-05-25 15:46:42 +00:00
|
|
|
sub_matches.get_one::<String>("REMOTE").expect("required")
|
2021-11-30 18:30:19 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
Some(("push", sub_matches)) => {
|
|
|
|
println!(
|
|
|
|
"Pushing to {}",
|
2022-05-25 15:46:42 +00:00
|
|
|
sub_matches.get_one::<String>("REMOTE").expect("required")
|
2021-11-30 18:30:19 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
Some(("add", sub_matches)) => {
|
|
|
|
let paths = sub_matches
|
2022-05-23 15:50:11 +00:00
|
|
|
.get_many::<PathBuf>("PATH")
|
|
|
|
.into_iter()
|
|
|
|
.flatten()
|
2021-11-30 18:30:19 +00:00
|
|
|
.collect::<Vec<_>>();
|
|
|
|
println!("Adding {:?}", paths);
|
|
|
|
}
|
2022-03-23 16:08:07 +00:00
|
|
|
Some(("stash", sub_matches)) => {
|
|
|
|
let stash_command = sub_matches.subcommand().unwrap_or(("push", sub_matches));
|
|
|
|
match stash_command {
|
|
|
|
("apply", sub_matches) => {
|
2022-05-25 15:46:42 +00:00
|
|
|
let stash = sub_matches.get_one::<String>("STASH");
|
2022-03-23 16:08:07 +00:00
|
|
|
println!("Applying {:?}", stash);
|
|
|
|
}
|
|
|
|
("pop", sub_matches) => {
|
2022-05-25 15:46:42 +00:00
|
|
|
let stash = sub_matches.get_one::<String>("STASH");
|
2022-03-23 16:08:07 +00:00
|
|
|
println!("Popping {:?}", stash);
|
|
|
|
}
|
|
|
|
("push", sub_matches) => {
|
2022-05-25 15:46:42 +00:00
|
|
|
let message = sub_matches.get_one::<String>("message");
|
2022-03-23 16:08:07 +00:00
|
|
|
println!("Pushing {:?}", message);
|
|
|
|
}
|
|
|
|
(name, _) => {
|
|
|
|
unreachable!("Unsupported subcommand `{}`", name)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-11-30 18:30:19 +00:00
|
|
|
Some((ext, sub_matches)) => {
|
|
|
|
let args = sub_matches
|
2022-05-23 15:50:11 +00:00
|
|
|
.get_many::<OsString>("")
|
|
|
|
.into_iter()
|
|
|
|
.flatten()
|
2021-11-30 18:30:19 +00:00
|
|
|
.collect::<Vec<_>>();
|
|
|
|
println!("Calling out to {:?} with {:?}", ext, args);
|
|
|
|
}
|
|
|
|
_ => unreachable!(), // If all subcommands are defined above, anything else is unreachabe!()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Continued program logic goes here...
|
|
|
|
}
|