SolidCraft: an open-source parametric 3D CAD application built in pure Rust. Desktop, web, and automation through MCP.
CLI: --help on each subcommand prints its usage and exits 0 (#4538) (#4545)
Homero Thompson5e0e7a4c6acb
diff --git a/apps/solidcraft-cli/src/crashes.rs b/apps/solidcraft-cli/src/crashes.rsindex 01288fbfdc..f628cf48f1 100644--- a/apps/solidcraft-cli/src/crashes.rs+++ b/apps/solidcraft-cli/src/crashes.rs@@ -6,11 +6,14 @@ use std::sync::Mutex;use serde_json::{Value, json};use solidcraft_engine::crash;-const USAGE: &str = "usage: solidcraft-cli crashes [list [--json] | show <id> | replay <id> | dir | clear [<id>]]";+/// The command's usage, from the same lines the top-level usage lists.+fn usage() -> String {+ crate::command_usage("crashes")+}pub fn run(args: &[String]) -> Result<(), String> {let dir = crash::default_dir().ok_or("no crash folder on this system")?;- let id = || args.get(1).map(String::as_str).ok_or_else(|| USAGE.to_string());+ let id = || args.get(1).map(String::as_str).ok_or_else(usage);match args.first().map(String::as_str) {None | Some("list") => {let reports = crash::list(&dir);@@ -43,7 +46,7 @@ pub fn run(args: &[String]) -> Result<(), String> {println!("deleted {} report(s)", crash::clear(&dir, args.get(1).map(String::as_str)));Ok(())}- _ => Err(USAGE.to_string()),+ _ => Err(usage()),}}diff --git a/apps/solidcraft-cli/src/fuzz.rs b/apps/solidcraft-cli/src/fuzz.rsindex fcd5756e1e..5a25b9d0fd 100644--- a/apps/solidcraft-cli/src/fuzz.rs+++ b/apps/solidcraft-cli/src/fuzz.rs@@ -16,9 +16,16 @@ fn flag<T: std::str::FromStr>(args: &[String], name: &str, default: T) -> T {args.iter().position(|a| a == name).and_then(|i| args.get(i + 1)).and_then(|v| v.parse().ok()).unwrap_or(default)}+/// The options `fuzz` takes, and the ones that stand alone.+const OPTIONS: &[&str] = &["--from", "--count", "--jobs", "--timeout", "--steps", "--out", "--report"];+const SWITCHES: &[&str] = &["--minimise"];+/// One seed: a design and a boolean case, as JSON on stdout (progress on stderr).pub fn one(args: &[String]) -> Result<(), String> {let seed: u64 = args.first().and_then(|s| s.parse().ok()).ok_or("fuzz-one <seed> [--steps N] [--minimise]")?;+ // The seed is the first argument; the rest are options, and an option `fuzz-one` doesn't+ // take is refused rather than ignored.+ crate::known_args("fuzz-one", &args[1..], &["--steps"], SWITCHES, false)?;let steps = flag(args, "--steps", 8usize);let minimise = args.iter().any(|a| a == "--minimise");// Panics caught inside the kernel: harmless natively, fatal in the web build.@@ -64,6 +71,9 @@ struct Job {/// Run seeds `--from`…`--from + --count` in `--jobs` processes, `--timeout` seconds each;/// write failing scripts to `--out` and a summary to `--report`.pub fn run(args: &[String]) -> Result<(), String> {+ // An option `fuzz` doesn't take is refused before a seed runs: it would otherwise be+ // ignored, and the run would go on for minutes with the wrong settings.+ crate::known_args("fuzz", args, OPTIONS, SWITCHES, false)?;let from = flag(args, "--from", 0u64);let count = flag(args, "--count", 100u64);let last = from.checked_add(count.saturating_sub(1)).ok_or("seed range exceeds u64::MAX: reduce --from or --count")?;diff --git a/apps/solidcraft-cli/src/main.rs b/apps/solidcraft-cli/src/main.rsindex f724d2901c..f342fefc3b 100644--- a/apps/solidcraft-cli/src/main.rs+++ b/apps/solidcraft-cli/src/main.rs@@ -46,58 +46,98 @@ mod app_icon;use solidcraft_engine::Session;use solidcraft_engine::view::Guides;-const USAGE: &str = "usage:- solidcraft-cli run <script.json|design.solidcraft|part.step> [--in design.solidcraft|part.step] [--out FILE]... [--save FILE] [--quiet] [--results] [--timings] [--profile] [--json-out FILE] [--allow-errors]- solidcraft-cli run --in <design.solidcraft|part.step> [--out FILE]...- (a timeline feature that fails fails the run and nothing is written; --allow-errors writes anyway)- (--timings prints {timings: [{index, command, ms}], total_ms} for a script, with --results its results too;- --profile adds each command's profile: {boolean_ms, tessellate_ms, other_ms}, the kernel's share)- solidcraft-cli eval <script.json|design.solidcraft|part.step> [--json-out FILE] (alias: inspect; or --in FILE)- (--json-out FILE writes the JSON that run (with --results, else the measurement) and eval print to FILE instead of stdout,- as plain UTF-8: a shell's > can re-encode it, e.g. Windows PowerShell adds a byte-order mark)- solidcraft-cli script <design.solidcraft|script.json> [--out script.json]- (the design's history as a command script that eval and run replay)- solidcraft-cli bench-open <design.solidcraft | part.step> [--parse-only] [--save OUT.solidcraft] [--display LEVEL [--edit NAME EXPR]] [--ops script.json]- (--ops: after opening, run each command of the script and undo it, timing the command, the- evaluations it caused and the view it leaves: placing and meshing what changed)- solidcraft-cli bench-agent [--scale F] [--save design.solidcraft] [--script-out script.json]- solidcraft-cli bench-assistant --connect 127.0.0.1:PORT [--token T] --base-url URL --model NAME [--tasks 1,2,5] [--repeat N] [--timeout-s 300] [--json out.json] [--markdown out.md]- solidcraft-cli snapshot <script|design> [--in design.solidcraft|part.step] --out shot.png [--width W] [--height H] [--view NAME | --camera JSON] [--no-grid] [--no-axes]- [--body NAME:opacity=0.3,color=#rrggbb]... [--tool NAME]... [--section z=27]- solidcraft-cli snapshot --in <design.solidcraft|part.step> --out shot.png ...- (as with run, --in starts from a design and the script runs on top of it; an option snapshot doesn't take is an error;- --view: iso, front, back, top, bottom, left, right, or the name of a view saved in the design with view.save;- --camera: {target: [x,y,z], yaw, pitch, distance, fov?} in mm and radians, as the MCP screenshot takes it (docs/mcp.md);- --no-grid and --no-axes leave the ground grid (on Z 0) and the origin's axes out of the picture;- --body: how one body is drawn, as appearance.assign takes it: opacity 0-1 (below 1 is see-through, so what is inside- it shows) and color #rrggbb, either or both, once per body;- --tool: draw this body solid in the tool colour and every other body see-through, so a cut's or a join's tool body- is visible inside the material it meets; a later --body overrides it;- --section: cut the model as inspect.section does, so the inside shows: z=27 (the plane through that world- coordinate, keeping the smaller side), add ,flip to keep the other side, or XY, XZ, YZ, or its own JSON)- solidcraft-cli render <script|design> --out image.png [--setup NAME] [--width W] [--height H] [--transparent] [--overwrite] [--timeout SECONDS]- solidcraft-cli exec <command> [json-params]- solidcraft-cli commands- solidcraft-cli recipe <recipe.json>- solidcraft-cli oracle <case-dir>... [--json]- solidcraft-cli fuzz [--from N] [--count N] [--jobs J] [--timeout S] [--steps N] [--minimise] [--out DIR] [--report FILE]- solidcraft-cli mcp [--headless] [--in design.solidcraft|script.json] [--connect 127.0.0.1:PORT [--token T]] [--allow-path DIR]... [--allow-any-path]- [--param-check off|warn|strict]- solidcraft-cli crashes [list [--json] | show <id> | replay <id> | dir | clear [<id>]]- solidcraft-cli licences- solidcraft-cli gpu [--policy auto|hardware|software|cpu]- (the GPU adapters a headless render can open, best first, the one the policy picks and the tier it- lands on: gpu, gpu-software, or the CPU rasterizer (auto with no adapter, or cpu); the policy defaults- to SOLIDCRAFT_GPU, else auto; exits with an error when hardware or software finds no such adapter)- solidcraft-cli icon --out icon.png [--size 256] | --out icon.ico- --debug, with any command: what SolidCraft logs (why a body has no display plan, why a save left its- model cache out), on stderr; RUST_LOG=error|warn|info|debug|trace sets one level for every crate-";+/// One command the CLI takes: the name to type, the usage it prints for `--help`,+/// and whether the top-level usage lists it (an alias answers to another command's name).+struct Command {+ name: &'static str,+ usage: &'static str,+ listed: bool,+}++/// `run`'s lines in the usage.+const USAGE_RUN: &str = " solidcraft-cli run <script.json|design.solidcraft|part.step> [--in design.solidcraft|part.step] [--out FILE]... [--save FILE] [--quiet] [--results] [--timings] [--profile] [--json-out FILE] [--allow-errors]\n solidcraft-cli run --in <design.solidcraft|part.step> [--out FILE]...\n (a timeline feature that fails fails the run and nothing is written; --allow-errors writes anyway)\n (--timings prints {timings: [{index, command, ms}], total_ms} for a script, with --results its results too;\n --profile adds each command's profile: {boolean_ms, tessellate_ms, other_ms}, the kernel's share)\n";+/// `eval`'s lines in the usage.+const USAGE_EVAL: &str = " solidcraft-cli eval <script.json|design.solidcraft|part.step> [--json-out FILE] (alias: inspect; or --in FILE)\n (--json-out FILE writes the JSON that run (with --results, else the measurement) and eval print to FILE instead of stdout,\n as plain UTF-8: a shell's > can re-encode it, e.g. Windows PowerShell adds a byte-order mark)\n";+/// `script`'s lines in the usage.+const USAGE_SCRIPT: &str = " solidcraft-cli script <design.solidcraft|script.json> [--out script.json]\n (the design's history as a command script that eval and run replay)\n";+/// `bench-open`'s lines in the usage.+const USAGE_BENCH_OPEN: &str = " solidcraft-cli bench-open <design.solidcraft | part.step> [--parse-only] [--save OUT.solidcraft] [--display LEVEL [--edit NAME EXPR]] [--ops script.json]\n (--ops: after opening, run each command of the script and undo it, timing the command, the\n evaluations it caused and the view it leaves: placing and meshing what changed)\n";+/// `bench-agent`'s lines in the usage.+const USAGE_BENCH_AGENT: &str = " solidcraft-cli bench-agent [--scale F] [--save design.solidcraft] [--script-out script.json]\n";+/// `bench-assistant`'s lines in the usage.+const USAGE_BENCH_ASSISTANT: &str = " solidcraft-cli bench-assistant --connect 127.0.0.1:PORT [--token T] --base-url URL --model NAME [--tasks 1,2,5] [--repeat N] [--timeout-s 300] [--json out.json] [--markdown out.md]\n";+/// `snapshot`'s lines in the usage.+const USAGE_SNAPSHOT: &str = " solidcraft-cli snapshot <script|design> [--in design.solidcraft|part.step] --out shot.png [--width W] [--height H] [--view NAME | --camera JSON] [--no-grid] [--no-axes]\n [--body NAME:opacity=0.3,color=#rrggbb]... [--tool NAME]... [--section z=27]\n solidcraft-cli snapshot --in <design.solidcraft|part.step> --out shot.png ...\n (as with run, --in starts from a design and the script runs on top of it; an option snapshot doesn't take is an error;\n --view: iso, front, back, top, bottom, left, right, or the name of a view saved in the design with view.save;\n --camera: {target: [x,y,z], yaw, pitch, distance, fov?} in mm and radians, as the MCP screenshot takes it (docs/mcp.md);\n --no-grid and --no-axes leave the ground grid (on Z 0) and the origin's axes out of the picture;\n --body: how one body is drawn, as appearance.assign takes it: opacity 0-1 (below 1 is see-through, so what is inside\n it shows) and color #rrggbb, either or both, once per body;\n --tool: draw this body solid in the tool colour and every other body see-through, so a cut's or a join's tool body\n is visible inside the material it meets; a later --body overrides it;\n --section: cut the model as inspect.section does, so the inside shows: z=27 (the plane through that world\n coordinate, keeping the smaller side), add ,flip to keep the other side, or XY, XZ, YZ, or its own JSON)\n";+/// `render`'s lines in the usage.+const USAGE_RENDER: &str = " solidcraft-cli render <script|design> --out image.png [--setup NAME] [--width W] [--height H] [--transparent] [--overwrite] [--timeout SECONDS]\n";+/// `exec`'s lines in the usage.+const USAGE_EXEC: &str = " solidcraft-cli exec <command> [json-params]\n";+/// `commands`'s lines in the usage.+const USAGE_COMMANDS: &str = " solidcraft-cli commands\n";+/// `recipe`'s lines in the usage.+const USAGE_RECIPE: &str = " solidcraft-cli recipe <recipe.json>\n";+/// `oracle`'s lines in the usage.+const USAGE_ORACLE: &str = " solidcraft-cli oracle <case-dir>... [--json]\n";+/// `fuzz`'s lines in the usage.+const USAGE_FUZZ: &str =+ " solidcraft-cli fuzz [--from N] [--count N] [--jobs J] [--timeout S] [--steps N] [--minimise] [--out DIR] [--report FILE]\n";+/// `mcp`'s lines in the usage.+const USAGE_MCP: &str = " solidcraft-cli mcp [--headless] [--in design.solidcraft|script.json] [--connect 127.0.0.1:PORT [--token T]] [--allow-path DIR]... [--allow-any-path]\n [--param-check off|warn|strict]\n";+/// `crashes`'s lines in the usage.+const USAGE_CRASHES: &str = " solidcraft-cli crashes [list [--json] | show <id> | replay <id> | dir | clear [<id>]]\n";+/// `licences`'s lines in the usage.+const USAGE_LICENCES: &str = " solidcraft-cli licences\n";+/// `gpu`'s lines in the usage.+const USAGE_GPU: &str = " solidcraft-cli gpu [--policy auto|hardware|software|cpu]\n (the GPU adapters a headless render can open, best first, the one the policy picks and the tier it\n lands on: gpu, gpu-software, or the CPU rasterizer (auto with no adapter, or cpu); the policy defaults\n to SOLIDCRAFT_GPU, else auto; exits with an error when hardware or software finds no such adapter)\n";+/// `icon`'s lines in the usage.+const USAGE_ICON: &str = " solidcraft-cli icon --out icon.png [--size 256] | --out icon.ico\n";+/// `fuzz-one`'s lines in the usage: a seed and the flags of one fuzz run.+const USAGE_FUZZ_ONE: &str = " solidcraft-cli fuzz-one <seed> [--steps N] [--minimise]\n";+/// `bench-edit`'s lines in the usage: the parameter a script changes.+const USAGE_BENCH_EDIT: &str = " solidcraft-cli bench-edit <script.json|recipe.json> <param> <expr>\n";+/// `save-stress`'s lines in the usage: the file saved over and over.+const USAGE_SAVE_STRESS: &str = " solidcraft-cli save-stress <path> <rounds>\n";+/// The flags every command takes, at the end of the top-level usage.+const USAGE_DEBUG: &str = " --debug, with any command: what SolidCraft logs (why a body has no display plan, why a save left its\n model cache out), on stderr; RUST_LOG=error|warn|info|debug|trace sets one level for every crate\n";++/// The commands the CLI takes, in the order the top-level usage lists them.+const COMMANDS: &[Command] = &[+ Command { name: "run", usage: USAGE_RUN, listed: true },+ Command { name: "eval", usage: USAGE_EVAL, listed: true },+ Command { name: "inspect", usage: USAGE_EVAL, listed: false },+ Command { name: "script", usage: USAGE_SCRIPT, listed: true },+ Command { name: "bench-open", usage: USAGE_BENCH_OPEN, listed: true },+ Command { name: "bench-agent", usage: USAGE_BENCH_AGENT, listed: true },+ Command { name: "bench-assistant", usage: USAGE_BENCH_ASSISTANT, listed: true },+ Command { name: "snapshot", usage: USAGE_SNAPSHOT, listed: true },+ Command { name: "render", usage: USAGE_RENDER, listed: true },+ Command { name: "exec", usage: USAGE_EXEC, listed: true },+ Command { name: "commands", usage: USAGE_COMMANDS, listed: true },+ Command { name: "recipe", usage: USAGE_RECIPE, listed: true },+ Command { name: "oracle", usage: USAGE_ORACLE, listed: true },+ Command { name: "fuzz", usage: USAGE_FUZZ, listed: true },+ Command { name: "fuzz-one", usage: USAGE_FUZZ_ONE, listed: false },+ Command { name: "mcp", usage: USAGE_MCP, listed: true },+ Command { name: "crashes", usage: USAGE_CRASHES, listed: true },+ Command { name: "licences", usage: USAGE_LICENCES, listed: true },+ Command { name: "licenses", usage: USAGE_LICENCES, listed: false },+ Command { name: "gpu", usage: USAGE_GPU, listed: true },+ Command { name: "icon", usage: USAGE_ICON, listed: true },+ Command { name: "bench-edit", usage: USAGE_BENCH_EDIT, listed: false },+ Command { name: "save-stress", usage: USAGE_SAVE_STRESS, listed: false },+];fn main() -> ExitCode {let mut args: Vec<String> = std::env::args().skip(1).collect();logger::init(&mut args);let r = match args.first().map(String::as_str) {+ // Asked-for help is not an error: the command's usage on stdout, exit 0. It is read+ // before the command runs, so `--help` never starts a run (a fuzz run, a server).+ Some(cmd) if wants_help(&args[1..]) && is_command(cmd) => {+ use std::io::Write as _;+ let _ = std::io::stdout().lock().write_all(command_usage(cmd).as_bytes());+ Ok(())+ }Some("run") => cmd_run(&args[1..]),Some("eval" | "inspect") => cmd_eval(&args[1..]),Some("script") => cmd_script(&args[1..]),@@ -118,7 +158,7 @@ fn main() -> ExitCode {}})}- Some("recipe") => args.get(1).ok_or_else(|| USAGE.to_string()).and_then(|p| {+ Some("recipe") => args.get(1).ok_or_else(usage).and_then(|p| {let v = read_json(p)?;recipe::to_script(&v).map(|s| println!("{}", pretty(&s)))}),@@ -150,10 +190,10 @@ fn main() -> ExitCode {// Asked-for help is not an error: usage on stdout, exit 0.Some("--help" | "-h" | "help") => {use std::io::Write as _;- let _ = std::io::stdout().lock().write_all(USAGE.as_bytes());+ let _ = std::io::stdout().lock().write_all(usage().as_bytes());Ok(())}- _ => Err(USAGE.to_string()),+ _ => Err(usage()),};match r {Ok(()) => ExitCode::SUCCESS,@@ -168,6 +208,35 @@ fn pretty(v: &Value) -> String {serde_json::to_string_pretty(v).unwrap_or_default()}+/// The whole usage: every command's lines under one `usage:` line. The lines come from the+/// same place a command's own `--help` prints, so the two cannot drift apart.+fn usage() -> String {+ let mut s = String::from("usage:\n");+ for c in COMMANDS.iter().filter(|c| c.listed) {+ s.push_str(c.usage);+ }+ s.push_str(USAGE_DEBUG);+ s+}++/// A command's own usage: its lines under a `usage:` line.+fn command_usage(cmd: &str) -> String {+ match COMMANDS.iter().find(|c| c.name == cmd) {+ Some(c) => format!("usage:\n{}", c.usage),+ None => usage(),+ }+}++/// Whether the name is a command the CLI takes.+fn is_command(cmd: &str) -> bool {+ COMMANDS.iter().any(|c| c.name == cmd)+}++/// Whether the arguments ask for help.+fn wants_help(args: &[String]) -> bool {+ args.iter().any(|a| a == "--help" || a == "-h")+}+pub fn read_json(path: &str) -> Result<Value, String> {// Through the engine's file access, which refuses anything that is not a regular file and// reads no more than the cap: a character device or a FIFO would otherwise never end.@@ -273,7 +342,7 @@ fn cmd_run(args: &[String]) -> Result<(), String> {let base = flag(args, "--in");let path = positional(args);if path.is_none() && base.is_none() {- return Err(USAGE.to_string());+ return Err(usage());}let want_results = args.iter().any(|a| a == "--results");// `--profile` times each command and splits out its kernel time; it implies `--timings`.@@ -333,13 +402,14 @@ fn open(base: Option<&str>, path: Option<&String>) -> Result<Session, String> {}(Some(b), None) => load(b),(None, Some(p)) => load(p),- (None, None) => Err(USAGE.to_string()),+ (None, None) => Err(usage()),}}/// Refuses an option `cmd` doesn't take, and one missing its value: a typo, or another-/// command's option, would otherwise be ignored without a word.-fn known_flags(cmd: &str, args: &[String], valued: &[&str], switches: &[&str]) -> Result<(), String> {+/// command's option, would otherwise be ignored without a word. `positionals` says whether the+/// command takes a word of its own (a file, a seed): `fuzz` takes none, and would start a run.+fn known_args(cmd: &str, args: &[String], valued: &[&str], switches: &[&str], positionals: bool) -> Result<(), String> {let mut i = 0;while let Some(a) = args.get(i) {if valued.contains(&a.as_str()) {@@ -347,7 +417,9 @@ fn known_flags(cmd: &str, args: &[String], valued: &[&str], switches: &[&str]) -Some(v) if !v.starts_with("--") => i += 2,_ => return Err(format!("{cmd}: {a} needs a value")),}- } else if a.starts_with("--") && !switches.contains(&a.as_str()) {+ } else if switches.contains(&a.as_str()) {+ i += 1;+ } else if a.starts_with("--") || !positionals {return Err(format!("{cmd} doesn't take {a} (it takes {})", [valued, switches].concat().join(", ")));} else {i += 1;@@ -371,7 +443,7 @@ fn timeline_ok(s: &Session) -> Result<(), String> {fn cmd_eval(args: &[String]) -> Result<(), String> {let json_out = json_out(args)?;- let path = flag(args, "--in").or_else(|| positional(args).map(String::as_str)).ok_or_else(|| USAGE.to_string())?;+ let path = flag(args, "--in").or_else(|| positional(args).map(String::as_str)).ok_or_else(usage)?;let mut s = load(path)?;let m = s.execute("inspect.measure", &json!({})).map_err(|e| e.to_string())?;let d = s.execute("document.inspect", &json!({})).map_err(|e| e.to_string())?;@@ -389,7 +461,7 @@ fn cmd_eval(args: &[String]) -> Result<(), String> {/// A design's history as a command script (`timeline.export_script`): printed, or written to/// `--out`; what the script leaves out goes to stderr.fn cmd_script(args: &[String]) -> Result<(), String> {- let path = positional(args).ok_or_else(|| USAGE.to_string())?;+ let path = positional(args).ok_or_else(usage)?;let s = &mut load(path)?;let out = flag(args, "--out");let r = s.execute("timeline.export_script", &json!({"path": out})).map_err(|e| e.to_string())?;@@ -450,14 +522,15 @@ fn install_renderer() {fn cmd_snapshot(args: &[String]) -> Result<(), String> {install_renderer();- known_flags(+ known_args("snapshot",args,&["--in", "--out", "--width", "--height", "--view", "--camera", "--body", "--tool", "--section"],&["--no-grid", "--no-axes"],+ true,)?;if positional(args).is_none() && flag(args, "--in").is_none() {- return Err(USAGE.to_string());+ return Err(usage());}let out = flag(args, "--out").ok_or("snapshot needs --out FILE.png")?;let size = |name: &str, default: u32| match flag(args, name) {@@ -541,7 +614,7 @@ fn ico(images: &[(usize, Vec<u8>)]) -> Result<Vec<u8>, String> {fn cmd_exec(args: &[String]) -> Result<(), String> {install_renderer();- let id = args.first().ok_or_else(|| USAGE.to_string())?;+ let id = args.first().ok_or_else(usage)?;let p: Value = match args.get(1) {Some(t) => serde_json::from_str(t).map_err(|e| format!("params: {e}"))?,None => json!({}),diff --git a/apps/solidcraft-cli/tests/help_output.rs b/apps/solidcraft-cli/tests/help_output.rsindex 2fda831e58..1762f363bc 100644--- a/apps/solidcraft-cli/tests/help_output.rs+++ b/apps/solidcraft-cli/tests/help_output.rs@@ -1,6 +1,84 @@//! An explicit help request prints usage to stdout and succeeds; an unknown command still fails.-use std::process::Command;+use std::process::{Command, Stdio};+use std::time::{Duration, Instant};++/// Every command the CLI takes, with the name its own usage names: `inspect` is `eval`, and+/// `licenses` is `licences`.+const COMMANDS: &[(&str, &str)] = &[+ ("run", "run"),+ ("eval", "eval"),+ ("inspect", "eval"),+ ("script", "script"),+ ("bench-open", "bench-open"),+ ("bench-agent", "bench-agent"),+ ("snapshot", "snapshot"),+ ("render", "render"),+ ("exec", "exec"),+ ("commands", "commands"),+ ("recipe", "recipe"),+ ("oracle", "oracle"),+ ("fuzz", "fuzz"),+ ("fuzz-one", "fuzz-one"),+ ("mcp", "mcp"),+ ("crashes", "crashes"),+ ("licences", "licences"),+ ("licenses", "licences"),+ ("gpu", "gpu"),+ ("icon", "icon"),+ ("bench-edit", "bench-edit"),+ ("save-stress", "save-stress"),+];++/// The commands the usage lists: the aliases answer to another command's name.+const LISTED: &[&str] = &[+ "run",+ "eval",+ "script",+ "bench-open",+ "bench-agent",+ "snapshot",+ "render",+ "exec",+ "commands",+ "recipe",+ "oracle",+ "fuzz",+ "mcp",+ "crashes",+ "licences",+ "gpu",+ "icon",+];++/// The command names a text mentions after `solidcraft-cli `, each one only once.+fn mentioned(text: &str) -> Vec<&str> {+ let mut names: Vec<&str> = text.split("solidcraft-cli ").skip(1).filter_map(|rest| rest.split_whitespace().next()).collect();+ names.sort_unstable();+ names.dedup();+ names+}++/// Runs the binary and waits at most `limit` for it to finish. `None` means it was still+/// running and was killed: the fuzz run `fuzz --help` used to start goes on for minutes.+fn run_within(limit: Duration, args: &[&str]) -> Option<(Option<i32>, String, String)> {+ let mut child = Command::new(env!("CARGO_BIN_EXE_solidcraft-cli")).args(args).stdout(Stdio::piped()).stderr(Stdio::piped()).spawn().unwrap();+ let started = Instant::now();+ loop {+ match child.try_wait().unwrap() {+ Some(status) => {+ let out = child.wait_with_output().unwrap();+ return Some((status.code(), String::from_utf8_lossy(&out.stdout).into_owned(), String::from_utf8_lossy(&out.stderr).into_owned()));+ }+ None if started.elapsed() >= limit => {+ let _ = child.kill();+ let _ = child.wait();+ return None;+ }+ None => std::thread::sleep(Duration::from_millis(25)),+ }+ }+}#[test]fn help_prints_usage_and_succeeds() {@@ -21,3 +99,48 @@ fn unknown_command_still_fails_with_usage() {assert!(output.stdout.is_empty());assert!(String::from_utf8_lossy(&output.stderr).starts_with("error: usage:"));}++#[test]+fn every_command_prints_its_own_usage() {+ for (cmd, named) in COMMANDS {+ for flag in ["--help", "-h"] {+ let output = Command::new(env!("CARGO_BIN_EXE_solidcraft-cli")).args([*cmd, flag]).output().unwrap();+ assert!(output.status.success(), "{cmd} {flag}: {}", String::from_utf8_lossy(&output.stderr));+ let stdout = String::from_utf8_lossy(&output.stdout);+ assert!(stdout.starts_with("usage:"), "{cmd} {flag}: {stdout}");+ assert_eq!(mentioned(&stdout), vec![*named], "{cmd} {flag} printed another command's usage: {stdout}");+ assert!(output.stderr.is_empty(), "{cmd} {flag}: {}", String::from_utf8_lossy(&output.stderr));+ }+ }+}++#[test]+fn the_top_level_usage_still_lists_every_command() {+ let output = Command::new(env!("CARGO_BIN_EXE_solidcraft-cli")).arg("--help").output().unwrap();+ let stdout = String::from_utf8_lossy(&output.stdout);+ for cmd in LISTED {+ assert!(stdout.contains(&format!("solidcraft-cli {cmd}")), "{cmd}: {stdout}");+ }+}++#[test]+fn fuzz_help_does_not_start_a_fuzz_run() {+ let Some((code, stdout, stderr)) = run_within(Duration::from_secs(20), &["fuzz", "--help"]) else {+ panic!("`fuzz --help` kept running: it started a fuzz run instead of printing the usage");+ };+ assert_eq!(code, Some(0), "{stderr}");+ assert!(stdout.starts_with("usage:"), "{stdout}");+ assert_eq!(mentioned(&stdout), vec!["fuzz"], "{stdout}");+ assert!(stderr.is_empty(), "{stderr}");+}++#[test]+fn fuzz_refuses_an_argument_it_does_not_take() {+ for arg in ["--not-a-flag", "stray"] {+ let Some((code, _stdout, stderr)) = run_within(Duration::from_secs(20), &["fuzz", arg]) else {+ panic!("`fuzz {arg}` kept running: it started a fuzz run instead of refusing the argument");+ };+ assert_ne!(code, Some(0), "`fuzz {arg}` succeeded");+ assert!(stderr.contains(arg), "`fuzz {arg}`: {stderr}");+ }+}