My personal project and infrastructure archive
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 
nomicon/development/tools/cargo-workspace2/src/cli.rs

91 lines
2.7 KiB

//! Helpers and utilities to parse the CLI input
use std::env;
pub struct CmdSet {
pub debug: bool,
pub line: Vec<String>,
}
fn get_nth(idx: usize) -> Option<String> {
env::args().nth(idx).as_ref().map(|s| s.to_owned()).clone()
}
/// Call this instead of env::args() - it handles !commands too
pub fn parse_env_args() -> CmdSet {
let mut line: Vec<_> = env::args().collect();
let mut debug = false;
if line.len() == 1 {
render_help(2);
}
find_bang_commands().into_iter().for_each(|(idx, bang)| {
let maybe_next = line.iter().nth(idx + 1).as_ref().map(|s| s.to_owned());
match bang.trim() {
"!help" => match maybe_next {
None => render_help(0),
Some(cmd) => crate::ops::render_help(cmd.to_string()),
},
"!version" => render_version(),
"!debug" => {
debug = true;
line.remove(idx);
}
bang => {
if debug {
eprintln!("Unrecognised bang command: {}", bang);
}
line.remove(idx);
}
}
});
CmdSet { line, debug }
}
/// Get env::args() and look for any string with a `!` in front of it.
/// If it's not in the set of known bang commands, ignore it.
fn find_bang_commands() -> Vec<(usize, String)> {
env::args()
.enumerate()
.filter_map(|(idx, s)| {
if s.starts_with("!") {
Some((idx, s))
} else {
None
}
})
.collect()
}
pub(crate) fn render_help(code: i32) -> ! {
eprintln!("cargo-ws v{}", env!("CARGO_PKG_VERSION"));
eprintln!("An expression language and command executor for cargo workspaces.");
eprintln!("Usage: cargo ws2 <QUERY LANG> <COMMAND> [COMMAND ARGS] [!debug]");
eprintln!(" cargo ws2 [!version | !debug]");
eprintln!(" cargo ws2 !help [COMMAND]");
eprintln!("");
crate::ops::list_commands();
eprintln!("");
eprintln!("Query language examples:\n");
eprintln!(" - [ foo bar ]: select crates foo and bar");
eprintln!(" - {{ foo < }}: select crates that depend on foo");
eprintln!(" - {{ foo < bar &< }}: select crates that depend on foo AND bar");
eprintln!(" - {{ foo < bar |< }}: select crates that depend on foo OR bar");
eprintln!("\nIf you have any questions, or find bugs, please e-mail me: kookie@spacekookie.de");
std::process::exit(code)
}
fn render_version() -> ! {
eprintln!("cargo-ws v{}", env!("CARGO_PKG_VERSION"));
eprintln!(
"Build with: {}",
include_str!(concat!(env!("OUT_DIR"), "/rustc.version"))
);
std::process::exit(0)
}