diff options
author | Robin Krahl <robin.krahl@ireas.org> | 2019-02-17 13:34:44 +0000 |
---|---|---|
committer | Daniel Mueller <deso@posteo.net> | 2019-02-17 19:50:52 -0800 |
commit | d3fea96b4467cc6fa22c9e7dfe6b6ce6375848b0 (patch) | |
tree | ec4c414312685421c2a64f00b2cf721add02ab28 | |
parent | 20274ad9381b28d1dd4de5b9e255fed6f88c03ad (diff) | |
download | nitrocli-d3fea96b4467cc6fa22c9e7dfe6b6ce6375848b0.tar.gz nitrocli-d3fea96b4467cc6fa22c9e7dfe6b6ce6375848b0.tar.bz2 |
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
-rw-r--r-- | nitrocli/src/args.rs | 46 |
1 files 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) + } } |