Skip to content

Commit

Permalink
Respect ctx.std{out,err} for version output
Browse files Browse the repository at this point in the history
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] tailhook/rust-argparse#50
  • Loading branch information
robinkrahl authored and d-e-s-o committed Feb 18, 2019
1 parent 20274ad commit d3fea96
Showing 1 changed file with 26 additions and 20 deletions.
46 changes: 26 additions & 20 deletions nitrocli/src/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 ({})",
Expand All @@ -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(
Expand Down Expand Up @@ -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)
}
}

0 comments on commit d3fea96

Please sign in to comment.