| 1 |
use std::collections::HashMap; |
| 2 |
use std::ffi::OsString; |
| 3 |
use std::path::PathBuf; |
| 4 |
|
| 5 |
use clap::{ArgMatches, Command, arg}; |
| 6 |
|
| 7 |
fn cli() -> Command { |
| 8 |
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 |
.subcommand( |
| 14 |
Command::new("clone") |
| 15 |
.about("Clones repos") |
| 16 |
.arg(arg!(<REMOTE> "The remote to clone")) |
| 17 |
.arg_required_else_help(true), |
| 18 |
) |
| 19 |
.subcommand( |
| 20 |
Command::new("diff") |
| 21 |
.about("Compare two commits") |
| 22 |
.arg(arg!(base: [COMMIT])) |
| 23 |
.arg(arg!(head: [COMMIT])) |
| 24 |
.arg(arg!(path: [PATH]).last(true)) |
| 25 |
.arg( |
| 26 |
arg!(--color <WHEN>) |
| 27 |
.value_parser(["always", "auto", "never"]) |
| 28 |
.num_args(0..=1) |
| 29 |
.require_equals(true) |
| 30 |
.default_value("auto") |
| 31 |
.default_missing_value("always"), |
| 32 |
), |
| 33 |
) |
| 34 |
.subcommand( |
| 35 |
Command::new("push") |
| 36 |
.about("pushes things") |
| 37 |
.arg(arg!(<REMOTE> "The remote to target")) |
| 38 |
.arg_required_else_help(true), |
| 39 |
) |
| 40 |
.subcommand( |
| 41 |
Command::new("add") |
| 42 |
.about("adds things") |
| 43 |
.arg_required_else_help(true) |
| 44 |
.arg(arg!(<PATH> ... "Stuff to add").value_parser(clap::value_parser!(PathBuf))), |
| 45 |
) |
| 46 |
.subcommand( |
| 47 |
Command::new("stash") |
| 48 |
.args_conflicts_with_subcommands(true) |
| 49 |
.flatten_help(true) |
| 50 |
.args(push_args()) |
| 51 |
.subcommand(Command::new("push").args(push_args())) |
| 52 |
.subcommand(Command::new("pop").arg(arg!([STASH]))) |
| 53 |
.subcommand(Command::new("apply").arg(arg!([STASH]))), |
| 54 |
) |
| 55 |
} |
| 56 |
|
| 57 |
fn push_args() -> Vec<clap::Arg> { |
| 58 |
vec![arg!(-m --message <MESSAGE>)] |
| 59 |
} |
| 60 |
|
| 61 |
fn aliases() -> HashMap<&'static str, Vec<&'static str>> { |
| 62 |
HashMap::from([ |
| 63 |
("last", vec!["diff", "HEAD~", "HEAD", "--"]), |
| 64 |
("stage", vec!["add"]), |
| 65 |
]) |
| 66 |
} |
| 67 |
|
| 68 |
fn parse_aliases() -> Result<ArgMatches, clap::Error> { |
| 69 |
let matches = cli().try_get_matches()?; |
| 70 |
expand_aliases(matches, Vec::new()) |
| 71 |
} |
| 72 |
|
| 73 |
fn expand_aliases( |
| 74 |
matches: ArgMatches, |
| 75 |
mut expanded: Vec<String>, |
| 76 |
) -> Result<ArgMatches, clap::Error> { |
| 77 |
let Some((name, sub_matches)) = matches.subcommand() else { |
| 78 |
return Ok(matches); |
| 79 |
}; |
| 80 |
if cli().find_subcommand(name).is_some() { |
| 81 |
return Ok(matches); |
| 82 |
} |
| 83 |
|
| 84 |
let aliases = aliases(); |
| 85 |
let Some(alias) = aliases.get(name) else { |
| 86 |
return Ok(matches); |
| 87 |
}; |
| 88 |
let mut args = alias.iter().map(OsString::from).collect::<Vec<_>>(); |
| 89 |
args.extend( |
| 90 |
sub_matches |
| 91 |
.get_many::<OsString>("") |
| 92 |
.into_iter() |
| 93 |
.flatten() |
| 94 |
.cloned(), |
| 95 |
); |
| 96 |
let matches = cli().no_binary_name(true).try_get_matches_from(args)?; |
| 97 |
let new_name = matches.subcommand_name().expect("subcommand required"); |
| 98 |
expanded.push(name.to_owned()); |
| 99 |
if expanded.iter().any(|name| name == new_name) { |
| 100 |
return Err(clap::Error::raw( |
| 101 |
clap::error::ErrorKind::InvalidSubcommand, |
| 102 |
format!("recursive alias `{}`", expanded[0]), |
| 103 |
)); |
| 104 |
} |
| 105 |
|
| 106 |
expand_aliases(matches, expanded) |
| 107 |
} |
| 108 |
|
| 109 |
fn main() { |
| 110 |
let matches = parse_aliases().unwrap_or_else(|error| error.exit()); |
| 111 |
|
| 112 |
match matches.subcommand() { |
| 113 |
Some(("clone", sub_matches)) => { |
| 114 |
println!( |
| 115 |
"Cloning {}", |
| 116 |
sub_matches.get_one::<String>("REMOTE").expect("required") |
| 117 |
); |
| 118 |
} |
| 119 |
Some(("diff", sub_matches)) => { |
| 120 |
let color = sub_matches |
| 121 |
.get_one::<String>("color") |
| 122 |
.map(|s| s.as_str()) |
| 123 |
.expect("defaulted in clap"); |
| 124 |
|
| 125 |
let mut base = sub_matches.get_one::<String>("base").map(|s| s.as_str()); |
| 126 |
let mut head = sub_matches.get_one::<String>("head").map(|s| s.as_str()); |
| 127 |
let mut path = sub_matches.get_one::<String>("path").map(|s| s.as_str()); |
| 128 |
if path.is_none() { |
| 129 |
path = head; |
| 130 |
head = None; |
| 131 |
if path.is_none() { |
| 132 |
path = base; |
| 133 |
base = None; |
| 134 |
} |
| 135 |
} |
| 136 |
let base = base.unwrap_or("stage"); |
| 137 |
let head = head.unwrap_or("worktree"); |
| 138 |
let path = path.unwrap_or(""); |
| 139 |
println!("Diffing {base}..{head} {path} (color={color})"); |
| 140 |
} |
| 141 |
Some(("push", sub_matches)) => { |
| 142 |
println!( |
| 143 |
"Pushing to {}", |
| 144 |
sub_matches.get_one::<String>("REMOTE").expect("required") |
| 145 |
); |
| 146 |
} |
| 147 |
Some(("add", sub_matches)) => { |
| 148 |
let paths = sub_matches |
| 149 |
.get_many::<PathBuf>("PATH") |
| 150 |
.into_iter() |
| 151 |
.flatten() |
| 152 |
.collect::<Vec<_>>(); |
| 153 |
println!("Adding {paths:?}"); |
| 154 |
} |
| 155 |
Some(("stash", sub_matches)) => { |
| 156 |
let stash_command = sub_matches.subcommand().unwrap_or(("push", sub_matches)); |
| 157 |
match stash_command { |
| 158 |
("apply", sub_matches) => { |
| 159 |
let stash = sub_matches.get_one::<String>("STASH"); |
| 160 |
println!("Applying {stash:?}"); |
| 161 |
} |
| 162 |
("pop", sub_matches) => { |
| 163 |
let stash = sub_matches.get_one::<String>("STASH"); |
| 164 |
println!("Popping {stash:?}"); |
| 165 |
} |
| 166 |
("push", sub_matches) => { |
| 167 |
let message = sub_matches.get_one::<String>("message"); |
| 168 |
println!("Pushing {message:?}"); |
| 169 |
} |
| 170 |
(name, _) => { |
| 171 |
unreachable!("Unsupported subcommand `{name}`") |
| 172 |
} |
| 173 |
} |
| 174 |
} |
| 175 |
Some((ext, sub_matches)) => { |
| 176 |
let args = sub_matches |
| 177 |
.get_many::<OsString>("") |
| 178 |
.into_iter() |
| 179 |
.flatten() |
| 180 |
.collect::<Vec<_>>(); |
| 181 |
println!("Calling out to {ext:?} with {args:?}"); |
| 182 |
} |
| 183 |
_ => unreachable!(), // If all subcommands are defined above, anything else is unreachable!() |
| 184 |
} |
| 185 |
|
| 186 |
// Continued program logic goes here... |
| 187 |
} |