Handle world Cargo help locally before the host request

This commit is contained in:
2026-11-07 18:23:01 +00:00
parent ad31954890
commit 2d5449b3dc
+20
View File
@@ -23,8 +23,17 @@ struct Executable {
fn error(value: impl std::fmt::Debug) -> io::Error {
io::Error::other(format!("{value:?}"))
}
fn local_help(argv: &[String]) -> Option<&'static str> {
matches!(argv.first().map(String::as_str), Some("--help" | "-h" | "help")).then_some(
"Usage: cargo <COMMAND> [OPTIONS]\n\nWorld commands: build, check, clippy, test, run, fmt, fetch, add, remove\n",
)
}
pub fn run() -> io::Result<i32> {
let argv: Vec<String> = std::env::args().skip(1).collect();
if let Some(help) = local_help(&argv) {
io::stdout().write_all(help.as_bytes())?;
return Ok(0);
}
let data = serde_json::to_vec(
&serde_json::json!({"argv": argv, "cwd": std::env::current_dir()?.to_string_lossy()}),
)?;
@@ -108,6 +117,17 @@ pub fn run() -> io::Result<i32> {
}
Ok(0)
}
#[cfg(test)]
#[test]
fn help_is_local_and_lists_world_commands() {
for flag in ["--help", "-h", "help"] {
let help = local_help(&[flag.into()]).expect("local help");
assert!(help.starts_with("Usage: cargo <COMMAND> [OPTIONS]\n"));
assert!(help.contains("build, check, clippy, test, run, fmt, fetch, add, remove"));
}
assert!(local_help(&["build".into()]).is_none());
assert!(local_help(&[]).is_none());
}
fn execution_args(argv: &[String]) -> Vec<String> {
let split = argv.iter().position(|a| a == "--").unwrap_or(argv.len());
let mut args = argv.get(split + 1..).unwrap_or_default().to_vec();