Repository: clap-rs/clap (rust)

Issue:
--version output should go to the clap::Error instead of directly to stdout
### Affected Version of clap

2.32.0

### Bug or Feature Request Summary

I'm using `get_matches_from_safe` to avoid exits and writing to stdout and stderr, because I'm using Clap not to parse arguments to the program itself, but somewhere inside the program (say, for commands submitted by connecting clients, or an internal command line interface in a gui):

```rust
let foo = app.get_matches_from_safe(client_args).map_err(|e| writeln!(client_output, "{}", e).unwrap())?;
```

This works fine, except that the output of `--version` ends up in stdout, instead of in `client_output`. It works fine for `--help`.

### Expected Behaviour Summary

I would expect the output from `--version` to not be written directly to stdout, but to end up in a `clap::Error`. This looks like the originally intended behaviour, seeing that `clap::Error::use_stderr` returns true for `ErrorKind::VersionDisplayed`.

### Actual Behaviour Summary

The version is not printed to the stream that I print the `clap::Error` to, but is always sent to stdout.

### Sample Code

```rust
extern crate clap;

use std::fmt::Write;

fn main() {
	let mut buf = String::new();

	let _ = clap::App::new("test").version("1")
		.get_matches_from_safe(&["test", "--version"])
		.map_err(|e| writeln!(&mut buf, "{}", e).unwrap());

	assert_eq!(buf, "test 1\n");
}
```

### Debug output

<details>
<summary> Debug Output </summary>

```
DEBUG:clap:Parser::propagate_settings: self=test, g_settings=AppFlags(
    (empty)
)
DEBUG:clap:Parser::get_matches_with;
DEBUG:clap:Parser::create_help_and_version;
DEBUG:clap:Parser::create_help_and_version: Building --help
DEBUG:clap:Parser::create_help_and_version: Building --version
DEBUG:clap:Parser::get_matches_with: Begin parsing '"--version"' ([45, 45, 118, 101, 114, 115, 105, 111, 110])
DEBUG:clap:Parser::is_new_arg:"--version":NotFound
DEBUG:clap:Parser::is_new_arg: arg_allows_tac=false
DEBUG:clap:Parser::is_new_arg: -- found
DEBUG:clap:Parser::is_new_arg: starts_new_arg=true
DEBUG:clap:Parser::possible_subcommand: arg="--version"
DEBUG:clap:Parser::get_matches_with: possible_sc=false, sc=None
DEBUG:clap:ArgMatcher::process_arg_overrides:None;
DEBUG:clap:Parser::parse_long_arg;
DEBUG:clap:Parser::parse_long_arg: Does it contain '='...No
DEBUG:clap:Parser::parse_long_arg: Found valid flag '--version'
DEBUG:clap:Parser::check_for_help_and_version_str;
DEBUG:clap:Parser::check_for_help_and_version_str: Checking if --version is help or version...Version
DEBUG:clap:Parser::_version: 
test 1thread 'main' panicked at 'assertion failed: `(left == right)`
  left: `"\n\n"`,
 right: `"test 1\n"`', src/main.rs:12:2
```
</details>


Pull request description:
Don't write --version output directly to stdout.
Fixes #1390.

Patch:
diff --git a/src/parse/parser.rs b/src/parse/parser.rs
index 316163da..1a1f1aa7 100644
--- a/src/parse/parser.rs
+++ b/src/parse/parser.rs
@@ -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,
             },


Test patch:
diff --git a/tests/version.rs b/tests/version.rs
index 750d608b..6a934777 100644
--- a/tests/version.rs
+++ b/tests/version.rs
@@ -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]