From ea442a15e512acc275cde4a442cbcaf2b0185a06 Mon Sep 17 00:00:00 2001 From: Marcelo Salloum Date: Mon, 3 Nov 2025 14:42:17 -0800 Subject: [PATCH 1/2] feat: Add version information to generated Markdown --- src/lib.rs | 8 ++++++++ tests/test_markdown.rs | 4 ++++ 2 files changed, 12 insertions(+) diff --git a/src/lib.rs b/src/lib.rs index 207cd95..c73f4fd 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -158,6 +158,14 @@ fn write_help_markdown( title_name ).unwrap(); + //---------------------------------- + // Write the version if available + //---------------------------------- + + if let Some(version) = command.get_version() { + writeln!(buffer, "**Version:** `{}`\n", version).unwrap(); + } + //---------------------------------- // Write the table of contents //---------------------------------- diff --git a/tests/test_markdown.rs b/tests/test_markdown.rs index d44aa5a..1951bd2 100644 --- a/tests/test_markdown.rs +++ b/tests/test_markdown.rs @@ -58,6 +58,8 @@ Options: This document contains the help content for the `my-program-display-name` command-line program. +**Version:** `1.2.3` + **Command Overview:** * [`my-program-display-name`↴](#my-program-display-name) @@ -133,6 +135,8 @@ Options: This document contains the help content for the `my-program-display-name` command-line program. +**Version:** `1.2.3` + **Command Overview:** * [`my-program-display-name`↴](#my-program-display-name) From 48a77e2b265eda3294155a24735a9453b80c1cf0 Mon Sep 17 00:00:00 2001 From: Marcelo Salloum Date: Mon, 3 Nov 2025 15:24:16 -0800 Subject: [PATCH 2/2] Deal with the scenario where versions are printed in multiple lines. --- src/lib.rs | 11 ++++++++- tests/test_markdown.rs | 51 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 61 insertions(+), 1 deletion(-) diff --git a/src/lib.rs b/src/lib.rs index c73f4fd..9d84360 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -163,7 +163,16 @@ fn write_help_markdown( //---------------------------------- if let Some(version) = command.get_version() { - writeln!(buffer, "**Version:** `{}`\n", version).unwrap(); + let version_str = version.to_string(); + + // Check if version contains multiple lines + if version_str.contains('\n') { + // Multi-line version: use a code block + writeln!(buffer, "**Version:**\n\n```\n{}\n```\n", version_str.trim()).unwrap(); + } else { + // Single-line version: use inline code + writeln!(buffer, "**Version:** `{}`\n", version_str).unwrap(); + } } //---------------------------------- diff --git a/tests/test_markdown.rs b/tests/test_markdown.rs index 1951bd2..0129291 100644 --- a/tests/test_markdown.rs +++ b/tests/test_markdown.rs @@ -155,6 +155,57 @@ This program does things. +" + ); +} + +/// Test behavior for an app with a multi-line version string +#[test] +fn test_version_behavior_for_multi_line_version_string() { + let multi_line_version = "my-cli 1.2.3 (abc123def)\nmy-lib 2.0.0 (789xyz456)\ndependency 3.1.0 (fedcba987)"; + + let mut app = Command::new("my-cli") + .version(multi_line_version) + .about("A CLI tool with multiple component versions") + .arg(Arg::new("input").short('i')); + let () = app.build(); + + assert_eq!( + help_markdown_command_custom( + &app, + &MarkdownOptions::new().show_footer(false) + ), + "\ +# Command-Line Help for `my-cli` + +This document contains the help content for the `my-cli` command-line program. + +**Version:** + +``` +my-cli 1.2.3 (abc123def) +my-lib 2.0.0 (789xyz456) +dependency 3.1.0 (fedcba987) +``` + +**Command Overview:** + +* [`my-cli`↴](#my-cli) + +## `my-cli` + +A CLI tool with multiple component versions + +**Usage:** `my-cli [OPTIONS]` + +###### **Options:** + +* `-i ` +* `-h`, `--help` — Print help +* `-V`, `--version` — Print version + + + " ); }