Files
photocraft/apps/photocraft-cli/src/lib.rs
T
IvanandClaude Opus 5.5 69d277440d feat(cli): generate man pages and shell completions (#7071)
Add the man-and-completions example, which writes photocraft-cli's
completions for bash, zsh, fish, elvish and PowerShell (clap_complete)
and its man pages (clap_mangen: photocraft-cli.1 and one per
subcommand, such as photocraft-cli-convert.1) into a directory:

  cargo run -p photocraft-cli --example man-and-completions -- <out-dir>

clap_complete and clap_mangen are dev-dependencies, so the binary is
unchanged. clap_mangen gets its env feature, so the pages name each
flag's environment variable as --help does; clap already has env, so
Cargo.lock gains no package for it. The library exposes only
`command()`, the clap definition; the args module stays private.

A test runs Command::debug_assert and checks that every shell's
completions and every page generate. Another repeats the example's two
generate_to calls and pins the exact file list: the five completion
files under the names the packages install, photocraft-cli.1, one page
per subcommand, and no pages for the help subcommand.

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-24 19:45:27 +03:00

86 lines
2.9 KiB
Rust

//! Headless PhotoCraft command line. `run` is the whole program, so it can be
//! tested in-process as well as through the binary.
#![deny(clippy::unwrap_used, clippy::expect_used, clippy::panic, clippy::unimplemented, clippy::todo, clippy::unreachable)]
use std::ffi::OsString;
use std::io::Write;
use clap::CommandFactory;
mod actions;
mod args;
mod automation;
mod batch;
mod document;
mod error;
mod output;
pub use actions::{ActionsError, Steps, load_actions, load_actions_file, parse_actions};
use args::{Command, parse};
use automation::{commands, mcp, serve};
use batch::{batch, droplet};
use document::{convert, info, run_cmds};
pub(crate) use error::{CliError, Result};
/// The command line's clap definition, for generating shell completions and man pages
/// (`examples/man-and-completions.rs`).
pub fn command() -> clap::Command {
args::Cli::command()
}
/// Run the CLI on its arguments (without the program name). Returns the process exit code (0 ok,
/// 1 failure, 2 usage). clap reads the arguments as they are: a path that is not valid Unicode (a
/// Latin-1 file name on Linux) is taken, and one where text is expected is a usage error (exit 2).
/// Never a panic, as `std::env::args()` aborts on one (issue #1108).
pub fn run<I, T>(args: I, out: &mut dyn Write, err: &mut dyn Write) -> i32
where
I: IntoIterator<Item = T>,
T: Into<OsString>,
{
let args: Vec<OsString> = args.into_iter().map(Into::into).collect();
// Help and `--version` (`help`, `-h`, `<subcommand> --help`, `-V`) come back as errors too,
// for `out`, exit 0: the subcommand never runs.
let cli = match parse(&args) {
Ok(cli) => cli,
Err(e) => {
let to: &mut dyn Write = if e.use_stderr() { err } else { out };
let _ = write!(to, "{}", e.render());
return e.exit_code();
}
};
let result = match &cli.command {
Command::Convert(a) => convert(a, out, err),
Command::Info(a) => info(a, out),
Command::Run(a) => run_cmds(a, out, err),
Command::Batch(a) => batch(a, out, err),
Command::Droplet(a) => droplet(a, out, err),
Command::Commands(a) => commands(a, out),
Command::Mcp(a) => mcp(a, err),
Command::Serve(a) => serve(a, err),
};
match result {
Ok(()) => 0,
Err(e) => {
let _ = writeln!(err, "error: {e}");
1
}
}
}
#[cfg(test)]
pub(crate) mod test_support {
/// What [`crate::run`] returned and wrote.
pub(crate) struct Invoke {
pub(crate) code: i32,
pub(crate) stdout: String,
pub(crate) stderr: String,
}
pub(crate) fn invoke(args: &[&str]) -> Invoke {
let (mut stdout, mut stderr) = (Vec::new(), Vec::new());
let code = crate::run(args, &mut stdout, &mut stderr);
Invoke { code, stdout: String::from_utf8(stdout).unwrap(), stderr: String::from_utf8(stderr).unwrap() }
}
}