diff --git a/CHANGELOG.md b/CHANGELOG.md index 7bdf6e8..89fa337 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,13 @@ listed under a **Changed** or **Removed** heading. ## [Unreleased] +### Fixed + +- **`termlens --version` is no longer an unknown option.** + The flag was handled only in the top-level dispatch, so `termlens inspect + --version` exited 2 while `-h`/`--help` worked in that position. All three + subcommands now print the same version string as the top-level command. (#310) + ## [0.10.1] - 2026-09-08 ### Changed diff --git a/crates/termlens-cli/src/main.rs b/crates/termlens-cli/src/main.rs index ae0ce4e..a7c8612 100644 --- a/crates/termlens-cli/src/main.rs +++ b/crates/termlens-cli/src/main.rs @@ -92,6 +92,11 @@ fn fail(message: &str) -> ExitCode { ExitCode::from(2) } +/// The one-line string every `--version` flag prints. +fn version() -> String { + format!("termlens {}\n", env!("CARGO_PKG_VERSION")) +} + fn main() -> ExitCode { let mut args = std::env::args().skip(1); let Some(command) = args.next() else { @@ -101,7 +106,7 @@ fn main() -> ExitCode { let rest: Vec = args.collect(); match command.as_str() { "-h" | "--help" => print(&format!("{USAGE}\n")), - "--version" => print(&format!("termlens {}\n", env!("CARGO_PKG_VERSION"))), + "--version" => print(&version()), "inspect" => inspect(rest), "diff" => diff(&rest), "render" => render(&rest), @@ -164,6 +169,7 @@ fn diff(args: &[String]) -> ExitCode { while let Some(arg) = args.next() { match arg.as_str() { "-h" | "--help" => return print(&format!("{DIFF_USAGE}\n")), + "--version" => return print(&version()), "--color" => { color = match args.next().map(String::as_str) { Some("auto") => ColorWhen::Auto, @@ -281,6 +287,7 @@ fn render(args: &[String]) -> ExitCode { for arg in args { match arg.as_str() { "-h" | "--help" => return print(&format!("{RENDER_USAGE}\n")), + "--version" => return print(&version()), "--svg" | "--html" | "--ansi" | "--text" => format = Some(arg.as_str()), other if other.starts_with('-') && other != "-" => { return fail(&format!( @@ -349,6 +356,7 @@ fn inspect(args: Vec) -> ExitCode { let flag = args.next().unwrap_or_default(); let parsed = match flag.as_str() { "-h" | "--help" => return print(&format!("{INSPECT_USAGE}\n")), + "--version" => return print(&version()), "--" => break, "--size" => take(&mut args, "--size", "COLSxROWS", "120x40", |spec| { let (c, r) = spec.split_once('x')?; diff --git a/crates/termlens-cli/tests/cli.rs b/crates/termlens-cli/tests/cli.rs index c3e8a09..838e3c6 100644 --- a/crates/termlens-cli/tests/cli.rs +++ b/crates/termlens-cli/tests/cli.rs @@ -213,3 +213,26 @@ fn help_and_version() -> termlens::Result<()> { let _: Screen = Screen::parse("size: 1x1 cursor: 0,0\n")?; Ok(()) } + +#[test] +fn subcommand_version_prints_same_string_as_top_level() -> termlens::Result<()> { + // The top-level version string is the reference. + let mut top = termlens::bin!("termlens", args(["--version"]))?; + assert_eq!(top.wait_exit()?.code(), Some(0)); + let expected = concat!("termlens ", env!("CARGO_PKG_VERSION")); + + for subcommand in ["diff", "render", "inspect"] { + let mut t = termlens::bin!("termlens", args([subcommand, "--version"]))?; + assert_eq!( + t.wait_exit()?.code(), + Some(0), + "`termlens {subcommand} --version` exited non-zero" + ); + assert!( + t.screen().contains(expected), + "`termlens {subcommand} --version` output:\n{}", + t.screen() + ); + } + Ok(()) +}