Repository: pacak/bpaf
Pull request #291 (2023-09-04 20:57:55)

Make sure --version contains a newline at the end
Fixes https://github.com/pacak/bpaf/issues/289


Patch:
diff --git a/src/buffer/html.rs b/src/buffer/html.rs
index dd03e88..69eea25 100644
--- a/src/buffer/html.rs
+++ b/src/buffer/html.rs
@@ -389,7 +389,7 @@ impl Doc {
                                 res.push_str("## ");
                             } else {
                                 res.push_str("# ");
-                                app_name_seen = true
+                                app_name_seen = true;
                             }
                         }
                         Block::Section2 => {
diff --git a/src/info.rs b/src/info.rs
index 2e8dfa0..af98a15 100644
--- a/src/info.rs
+++ b/src/info.rs
@@ -273,9 +273,12 @@ impl<T> OptionParser<T> {
                     )
                 }
                 ExtraParams::Version(v) => {
+                    use crate::buffer::{Block, Token};
                     let mut buffer = Doc::default();
+                    buffer.token(Token::BlockStart(Block::Block));
                     buffer.text("Version: ");
                     buffer.doc(&v);
+                    buffer.token(Token::BlockEnd(Block::Block));
                     buffer
                 }
             };


Test patch:
diff --git a/tests/help_format.rs b/tests/help_format.rs
index 2c502c7..2be8be9 100644
--- a/tests/help_format.rs
+++ b/tests/help_format.rs
@@ -592,3 +592,17 @@ Available options:
 ";
     assert_eq!(r, expected);
 }
+
+#[test]
+fn help_and_version_newline() {
+    let parser = short('a').switch().to_options().version("1");
+
+    let r = parser.run_inner(&["--help"]).unwrap_err().unwrap_stdout();
+    assert_eq!(r, "Usage: [-a]\n\nAvailable options:\n    -a\n    -h, --help     Prints help information\n    -V, --version  Prints version information\n");
+
+    let r = parser
+        .run_inner(&["--version"])
+        .unwrap_err()
+        .unwrap_stdout();
+    assert_eq!(r, "Version: 1\n");
+}
diff --git a/tests/tests.rs b/tests/tests.rs
index b97f2fe..8226fde 100644
--- a/tests/tests.rs
+++ b/tests/tests.rs
@@ -132,7 +132,7 @@ fn either_of_three_required_flags() {
 
     // version help requires version meta
     let ver = decorated.run_inner(&["-V"]).unwrap_err().unwrap_stdout();
-    assert_eq!("Version: 1.0", ver);
+    assert_eq!("Version: 1.0\n", ver);
 
     // help is always generated
     let help = decorated.run_inner(&["-h"]).unwrap_err().unwrap_stdout();
@@ -165,7 +165,7 @@ fn either_of_three_required_flags2() {
     let decorated = p.to_options().version("1.0");
 
     let ver = decorated.run_inner(&["-V"]).unwrap_err().unwrap_stdout();
-    assert_eq!("Version: 1.0", ver);
+    assert_eq!("Version: 1.0\n", ver);
 
     // help is always generated
     let help = decorated.run_inner(&["-h"]).unwrap_err().unwrap_stdout();
@@ -198,7 +198,7 @@ fn either_of_two_required_flags_and_one_optional() {
     let decorated = p.to_options().version("1.0");
 
     let ver = decorated.run_inner(&["-V"]).unwrap_err().unwrap_stdout();
-    assert_eq!("Version: 1.0", ver);
+    assert_eq!("Version: 1.0\n", ver);
 
     // help is always generated
     let help = decorated.run_inner(&["-h"]).unwrap_err().unwrap_stdout();