Don't write --version output directly to stdout. (#1602)

Fixes #1390.
This commit is contained in:
Mara Bos 2019-11-26 18:39:12 +01:00 committed by Dylan DPC
parent 335f34bee2
commit 2a2969aee4
2 changed files with 10 additions and 7 deletions

View file

@ -1,7 +1,7 @@
// Std
use std::cell::Cell;
use std::ffi::{OsStr, OsString};
use std::io::{self, BufWriter, Write};
use std::io::Write;
use std::iter::Peekable;
use std::mem;
#[cfg(all(
@ -1545,13 +1545,12 @@ where
fn version_err(&self, use_long: bool) -> ClapError {
debugln!("Parser::version_err: ");
let out = io::stdout();
let mut buf_w = BufWriter::new(out.lock());
match self.print_version(&mut buf_w, use_long) {
let mut buf = vec![];
match self.print_version(&mut buf, use_long) {
Err(e) => e,
_ => ClapError {
cause: String::new(),
message: String::new(),
message: String::from_utf8(buf).unwrap_or_default(),
kind: ErrorKind::VersionDisplayed,
info: None,
},

View file

@ -18,7 +18,9 @@ fn version_short() {
.try_get_matches_from(vec!["myprog", "-V"]);
assert!(m.is_err());
assert_eq!(m.unwrap_err().kind, ErrorKind::VersionDisplayed);
let err = m.unwrap_err();
assert_eq!(err.kind, ErrorKind::VersionDisplayed);
assert_eq!(err.message, "test 1.3");
}
#[test]
@ -30,7 +32,9 @@ fn version_long() {
.try_get_matches_from(vec!["myprog", "--version"]);
assert!(m.is_err());
assert_eq!(m.unwrap_err().kind, ErrorKind::VersionDisplayed);
let err = m.unwrap_err();
assert_eq!(err.kind, ErrorKind::VersionDisplayed);
assert_eq!(err.message, "test 1.3");
}
#[test]