From d3fea96b4467cc6fa22c9e7dfe6b6ce6375848b0 Mon Sep 17 00:00:00 2001
From: Robin Krahl <robin.krahl@ireas.org>
Date: Sun, 17 Feb 2019 13:34:44 +0000
Subject: Respect ctx.std{out,err} for version output

Due to a bug in argparse [0], custom stdout and stderr settings are
ignored when using argparse::Print, as we currently do for the --version
option. This patch adds a workaround for this problem: Instead of using
argparse::Print, we use argparse::StoreTrue for the --version option.
The argument parsing will fail as the command is missing, but the
version variable will still be set to true if the version option was
set. So we ignore the parsing result and discard the argparse output if
the version variable is set.

[0] https://github.com/tailhook/rust-argparse/pull/50
---
 nitrocli/src/args.rs | 46 ++++++++++++++++++++++++++--------------------
 1 file changed, 26 insertions(+), 20 deletions(-)

diff --git a/nitrocli/src/args.rs b/nitrocli/src/args.rs
index 8bf0013..7f0bfca 100644
--- a/nitrocli/src/args.rs
+++ b/nitrocli/src/args.rs
@@ -866,6 +866,7 @@ fn pws_status(ctx: &mut ExecCtx<'_>, args: Vec<String>) -> Result<()> {
 pub(crate) fn handle_arguments(ctx: &mut RunCtx<'_>, args: Vec<String>) -> Result<()> {
   use std::io::Write;
 
+  let mut version = false;
   let mut model: Option<DeviceModel> = None;
   let model_help = format!(
     "Select the device model to connect to ({})",
@@ -876,9 +877,9 @@ pub(crate) fn handle_arguments(ctx: &mut RunCtx<'_>, args: Vec<String>) -> Resul
   let cmd_help = cmd_help!(command);
   let mut subargs = vec![];
   let mut parser = argparse::ArgumentParser::new();
-  parser.add_option(
+  let _ = parser.refer(&mut version).add_option(
     &["-V", "--version"],
-    argparse::Print(format!("nitrocli {}", env!("CARGO_PKG_VERSION"))),
+    argparse::StoreTrue,
     "Print version information and exit",
   );
   let _ = parser.refer(&mut verbosity).add_option(
@@ -907,22 +908,27 @@ pub(crate) fn handle_arguments(ctx: &mut RunCtx<'_>, args: Vec<String>) -> Resul
   let mut stdio_buf = (&mut stdout_buf, &mut stderr_buf);
   let result = parse(&mut stdio_buf, parser, args);
 
-  stdout_buf.flush()?;
-  stderr_buf.flush()?;
-
-  result?;
-  subargs.insert(0, format!("nitrocli {}", command));
-
-  let mut ctx = ExecCtx {
-    model,
-    stdout: ctx.stdout,
-    stderr: ctx.stderr,
-    admin_pin: ctx.admin_pin.take(),
-    user_pin: ctx.user_pin.take(),
-    new_admin_pin: ctx.new_admin_pin.take(),
-    new_user_pin: ctx.new_user_pin.take(),
-    password: ctx.password.take(),
-    verbosity,
-  };
-  command.execute(&mut ctx, subargs)
+  if version {
+    println!(ctx, "nitrocli {}", env!("CARGO_PKG_VERSION"))?;
+    Ok(())
+  } else {
+    stdout_buf.flush()?;
+    stderr_buf.flush()?;
+
+    result?;
+    subargs.insert(0, format!("nitrocli {}", command));
+
+    let mut ctx = ExecCtx {
+      model,
+      stdout: ctx.stdout,
+      stderr: ctx.stderr,
+      admin_pin: ctx.admin_pin.take(),
+      user_pin: ctx.user_pin.take(),
+      new_admin_pin: ctx.new_admin_pin.take(),
+      new_user_pin: ctx.new_user_pin.take(),
+      password: ctx.password.take(),
+      verbosity,
+    };
+    command.execute(&mut ctx, subargs)
+  }
 }
-- 
cgit v1.2.3