//! 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]