From 7a1a75b8948d57610b5bd7af164286555bcb247d Mon Sep 17 00:00:00 2001 From: FredApps <153173854+FredApps@users.noreply.github.com> Date: Thu, 26 Nov 2026 23:25:02 +0100 Subject: [PATCH 1/4] refactor(cli): anicraft-cli follows RFC-0018 (clap, typed errors, run(args, out, err), exit codes 0/1/2) --- Cargo.lock | 155 ++ Cargo.toml | 3 + crates/cli/Cargo.toml | 5 + crates/cli/examples/man-and-completions.rs | 42 + crates/cli/src/args.rs | 544 +++++++ crates/cli/src/bench_stroke.rs | 26 +- crates/cli/src/error.rs | 98 ++ crates/cli/src/lib.rs | 1508 ++++++-------------- crates/cli/src/main.rs | 36 +- crates/cli/src/tests.rs | 203 +++ crates/cli/tests/it/cli.rs | 319 +++++ crates/cli/tests/it/commands.rs | 304 ++++ crates/cli/tests/it/main.rs | 36 + crates/cli/tests/it/man_and_completions.rs | 50 + crates/cli/tests/it/protocol.rs | 148 ++ 15 files changed, 2371 insertions(+), 1106 deletions(-) create mode 100644 crates/cli/examples/man-and-completions.rs create mode 100644 crates/cli/src/args.rs create mode 100644 crates/cli/src/error.rs create mode 100644 crates/cli/src/tests.rs create mode 100644 crates/cli/tests/it/cli.rs create mode 100644 crates/cli/tests/it/commands.rs create mode 100644 crates/cli/tests/it/main.rs create mode 100644 crates/cli/tests/it/man_and_completions.rs create mode 100644 crates/cli/tests/it/protocol.rs diff --git a/Cargo.lock b/Cargo.lock index 9f584843..5320faaf 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -284,8 +284,12 @@ dependencies = [ "anicraft-geom", "anicraft-guide", "anicraft-render", + "clap", + "clap_complete", + "clap_mangen", "getrandom 0.3.4", "serde_json", + "thiserror 2.0.21", "wat", ] @@ -467,6 +471,56 @@ dependencies = [ "thiserror 2.0.21", ] +[[package]] +name = "anstream" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "824a212faf96e9acacdbd09febd34438f8f711fb84e09a8916013cd7815ca28d" +dependencies = [ + "anstyle", + "anstyle-parse", + "anstyle-query", + "anstyle-wincon", + "colorchoice", + "is_terminal_polyfill", + "utf8parse", +] + +[[package]] +name = "anstyle" +version = "1.0.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "940b3a0ca603d1eade50a4846a2afffd5ef57a9feac2c0e2ec2e14f9ead76000" + +[[package]] +name = "anstyle-parse" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "52ce7f38b242319f7cabaa6813055467063ecdc9d355bbb4ce0c68908cd8130e" +dependencies = [ + "utf8parse", +] + +[[package]] +name = "anstyle-query" +version = "1.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "40c48f72fd53cd289104fc64099abca73db4166ad86ea0b4341abe65af83dadc" +dependencies = [ + "windows-sys 0.61.2", +] + +[[package]] +name = "anstyle-wincon" +version = "3.0.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "291e6a250ff86cd4a820112fb8898808a366d8f9f58ce16d1f538353ad55747d" +dependencies = [ + "anstyle", + "once_cell_polyfill", + "windows-sys 0.61.2", +] + [[package]] name = "arboard" version = "3.7.0" @@ -1029,6 +1083,65 @@ dependencies = [ "num-traits", ] +[[package]] +name = "clap" +version = "4.6.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "aa8876b300ab35ba921adea3dfd70157a46249b33f95c9084ae5709785478946" +dependencies = [ + "clap_builder", + "clap_derive", +] + +[[package]] +name = "clap_builder" +version = "4.6.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ec0797fb7aeb1406c84efac526901f7ec3ead2124f946b494e72879d4b54704d" +dependencies = [ + "anstream", + "anstyle", + "clap_lex", + "strsim", +] + +[[package]] +name = "clap_complete" +version = "4.6.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6a35200383f20018d41c14d1ab19c1727c697f544492143f7d93aa39f0187848" +dependencies = [ + "clap", +] + +[[package]] +name = "clap_derive" +version = "4.6.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f9c751b79415d4e559e3d1fcf128e09e720eb673a06d26cf6f392d37d75b66e0" +dependencies = [ + "heck", + "proc-macro2", + "quote", + "syn 3.0.9", +] + +[[package]] +name = "clap_lex" +version = "1.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e0921b3876b3a1df8a1c9f912e7f94342458022c2e84288015893d83c4d62a79" + +[[package]] +name = "clap_mangen" +version = "0.2.33" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7e30ffc187e2e3aeafcd1c6e2aa416e29739454c0ccaa419226d5ecd181f2d78" +dependencies = [ + "clap", + "roff", +] + [[package]] name = "clipboard-win" version = "5.4.3" @@ -1064,6 +1177,12 @@ version = "1.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3d7b894f5411737b7867f4827955924d7c254fc9f4d91a6aad6b097804b1018b" +[[package]] +name = "colorchoice" +version = "1.0.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1d07550c9036bf2ae0c684c4297d503f838287c83c53686d05370d0e139ae570" + [[package]] name = "combine" version = "4.6.9" @@ -2500,6 +2619,12 @@ dependencies = [ "fearless_simd", ] +[[package]] +name = "heck" +version = "0.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2304e00983f87ffb38b55b444b5e3b60a884b5d30c0fca7d82fe33449bbe55ea" + [[package]] name = "hermit-abi" version = "0.5.3" @@ -2728,6 +2853,12 @@ dependencies = [ "memoffset", ] +[[package]] +name = "is_terminal_polyfill" +version = "1.70.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a6cb138bb79a146c1bd460005623e142ef0181e3d0219cb493e02f7d08a35695" + [[package]] name = "itertools" version = "0.14.0" @@ -3621,6 +3752,12 @@ version = "1.21.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9f7c3e4beb33f85d45ae3e3a1792185706c8e16d043238c593331cc7cd313b50" +[[package]] +name = "once_cell_polyfill" +version = "1.70.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "384b8ab6d37215f3c5301a95a4accb5d64aa607f1fcb26a11b5303878451b4fe" + [[package]] name = "oorandom" version = "11.1.5" @@ -4282,6 +4419,12 @@ dependencies = [ "windows-sys 0.52.0", ] +[[package]] +name = "roff" +version = "1.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "323c417e1d9665a65b263ec744ba09030cfb277e9daa0b018a4ab62e57bc8189" + [[package]] name = "roxmltree" version = "0.21.2" @@ -4728,6 +4871,12 @@ dependencies = [ "serde", ] +[[package]] +name = "strsim" +version = "0.11.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7da8b5736845d9f2fcb837ea5d9e2628564b3b043a70948a3f0b778838c5fb4f" + [[package]] name = "subtle" version = "2.6.1" @@ -5408,6 +5557,12 @@ dependencies = [ "utf_types", ] +[[package]] +name = "utf8parse" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "06abde3611657adf66d383f00b093d7faecc7fa57071cce2578660c9f1010821" + [[package]] name = "utf_types" version = "1.0.0" diff --git a/Cargo.toml b/Cargo.toml index 4c3a7097..f0869945 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -64,6 +64,9 @@ anicraft-actions = { path = "crates/actions" } serde = { version = "1", features = ["derive"] } serde_json = "1" thiserror = "2" +clap = { version = "4", features = ["derive", "env"] } +clap_complete = "4" +clap_mangen = "0.2" rayon = "1" # Unions of flattened polygons on an integer grid, for a stroke's outline (ADR 0110): pure # Rust, no unsafe in our use, MIT OR Apache-2.0 (its i_float, i_shape, i_tree and i_key_sort: MIT). diff --git a/crates/cli/Cargo.toml b/crates/cli/Cargo.toml index 43cb8607..7911875e 100644 --- a/crates/cli/Cargo.toml +++ b/crates/cli/Cargo.toml @@ -23,6 +23,8 @@ anicraft-guide.workspace = true # The legacy Flash container, for `fla-dump` (ADR 0120). anicraft-fla.workspace = true serde_json.workspace = true +thiserror.workspace = true +clap.workspace = true # The live control port's per-run token (ADR 0104): the operating system's random numbers. # MIT OR Apache-2.0, already in the tree. Not for the web: a page has no sockets, so the control @@ -31,6 +33,9 @@ serde_json.workspace = true getrandom = "0.3" [dev-dependencies] +# Man pages and shell completions, written by `examples/man-and-completions.rs` (RFC-0018 rule 1.9). +clap_complete.workspace = true +clap_mangen.workspace = true # Exporter plug-in fixtures written in WebAssembly text (Apache-2.0 WITH LLVM-exception). wat = "1.228" diff --git a/crates/cli/examples/man-and-completions.rs b/crates/cli/examples/man-and-completions.rs new file mode 100644 index 00000000..29864c44 --- /dev/null +++ b/crates/cli/examples/man-and-completions.rs @@ -0,0 +1,42 @@ +//! Writes anicraft-cli's shell completions ([`clap_complete`]: bash, zsh, fish, elvish, +//! PowerShell) and man pages ([`clap_mangen`]: `anicraft-cli.1` and one per subcommand, such as +//! `anicraft-cli-render.1`) into a directory. The Linux packages ship them +//! (`packaging/linux/package.sh`): +//! +//! ```sh +//! cargo run -p anicraft-cli --example man-and-completions -- +//! ``` + +use std::path::Path; +use std::process::ExitCode; + +use clap::ValueEnum; +use clap_complete::Shell; + +fn main() -> ExitCode { + let mut args = std::env::args_os().skip(1); + let (Some(dir), None) = (args.next(), args.next()) else { + eprintln!("usage: man-and-completions "); + return ExitCode::from(2); + }; + let dir = Path::new(&dir); + match generate(dir) { + Ok(()) => ExitCode::SUCCESS, + Err(e) => { + eprintln!("error: {}: {e}", dir.display()); + ExitCode::FAILURE + } + } +} + +/// Also run by the tests. +pub(crate) fn generate(dir: &Path) -> std::io::Result<()> { + std::fs::create_dir_all(dir)?; + let mut cmd = anicraft_cli::command(); + for &shell in Shell::value_variants() { + clap_complete::generate_to(shell, &mut cmd, "anicraft-cli", dir)?; + } + // A fresh command: `clap_complete::generate_to` built `cmd`, adding the `help` subcommand, + // which `clap_mangen` would otherwise give pages of its own (`anicraft-cli-help-render.1`). + clap_mangen::generate_to(anicraft_cli::command(), dir) +} diff --git a/crates/cli/src/args.rs b/crates/cli/src/args.rs new file mode 100644 index 00000000..69f89190 --- /dev/null +++ b/crates/cli/src/args.rs @@ -0,0 +1,544 @@ +//! The command line as clap derive types. Values arrive typed, so the commands never validate a +//! string; what clap can't say alone is a usage error built here, printed and exited (2) like +//! clap's own. The help text is the doc comments (RFC-0018 rule 1.8). + +use std::ffi::OsString; +use std::path::{Path, PathBuf}; + +use clap::builder::BoolishValueParser; +use clap::error::ErrorKind; +use clap::{Arg, Args, CommandFactory, FromArgMatches, Parser, Subcommand}; +use serde_json::Value; + +/// The end of `--help` of every command. +const EXIT_STATUS: &str = "Exit status:\n 0 success\n 1 a command failed\n 2 usage error\n\nA process ended by a signal exits 128 + the signal number (130 for Ctrl-C), as in the shell."; + +/// What a document argument is. +const DOCUMENT_NOTE: &str = "The document is an .anicraft file, an Animate document (.fla, .xfl) or `--sample`, a built-in sample."; + +const FRAMES_NOTE: &str = "Frame flags on the command line (--frame, --from, --to) are 1-based, as in the timeline. Frame numbers inside JSON params, scripts and command results are 0-based, as everywhere in the engine."; + +const ROOTS_NOTE: &str = "Commands read files only inside the read roots and write only inside the write root: links and `..` are followed first, and a refusal names the root. --automation-read-root may be given several times (docs/mcp.md)."; + +/// AniCraft without a window: headless animation engine, MCP server and the app's control channel +#[derive(Parser, Debug)] +#[command(name = "anicraft-cli", version, propagate_version = true, arg_required_else_help = true, after_help = EXIT_STATUS, after_long_help = format!("{FRAMES_NOTE}\n\n{EXIT_STATUS}"))] +pub(crate) struct Cli { + #[command(subcommand)] + pub(crate) command: Command, +} + +#[derive(Subcommand, Debug)] +pub(crate) enum Command { + /// Print a document's state as JSON + #[command(after_help = DOCUMENT_NOTE)] + Info(InfoArgs), + /// List the streams and CPic* record tags of a legacy Flash 8-CS4 .fla file as JSON + FlaDump(FlaDumpArgs), + /// Render one frame to a PNG file + #[command(after_help = format!("{DOCUMENT_NOTE}\n{FRAMES_NOTE}"))] + Render(RenderArgs), + /// Render a frame range to numbered PNG files in a folder + #[command(after_help = format!("{DOCUMENT_NOTE}\n{FRAMES_NOTE}"))] + RenderSeq(RenderSeqArgs), + /// Write an animated GIF + #[command(after_help = format!("{DOCUMENT_NOTE}\n{FRAMES_NOTE}"))] + Gif(GifArgs), + /// Write an animated PNG, in full colour with real transparency + #[command(after_help = format!("{DOCUMENT_NOTE}\n{FRAMES_NOTE}"))] + Apng(MotionArgs), + /// Write an animated WebP (lossless), in full colour with real transparency + #[command(after_help = format!("{DOCUMENT_NOTE}\n{FRAMES_NOTE}"))] + Webp(MotionArgs), + /// Export in any format: a built-in one or an exporter plug-in + /// + /// The built-in formats are gif, apng, webp, lottie, html, video, svg, png, png-sequence, + /// svg-sequence and sprite-sheet; exporter plug-ins are named by id and need --plugins. + /// `formats` lists them all. --options are the plug-in's options, or a built-in's extra + /// params, as a JSON object. + #[command(after_help = format!("{DOCUMENT_NOTE}\n{FRAMES_NOTE}"))] + Export(ExportArgs), + /// List every export format, plug-ins included, as JSON + Formats(FormatsArgs), + /// List every command with its parameters, as JSON + Commands, + /// Run a script on a document: a JSON array of {cmd, params}, or a saved command + /// + /// The steps are one undo step, and nothing changes if one fails. With FILE, the document is + /// FILE; with --sample, the first argument is the script. + #[command(after_help = DOCUMENT_NOTE, override_usage = "anicraft-cli run [OPTIONS]