diff options
author | Daniel Mueller <deso@posteo.net> | 2019-01-04 18:27:45 -0800 |
---|---|---|
committer | Daniel Mueller <deso@posteo.net> | 2019-01-04 18:27:45 -0800 |
commit | 1af4c0771034aa5d5f0a1f558dabc58c87fa3b19 (patch) | |
tree | c5013b6302fa17de4eaa820d365da9356e66b23c /nitrocli/src/args.rs | |
parent | e569fec44940add141cc526ce77f67dfe976c8e0 (diff) | |
download | nitrocli-1af4c0771034aa5d5f0a1f558dabc58c87fa3b19.tar.gz nitrocli-1af4c0771034aa5d5f0a1f558dabc58c87fa3b19.tar.bz2 |
Introduce -v/--verbose option
This change introduces a new option, -v/--verbose, that can be used to
increase the log level of libnitrokey. The option can be supplied
multiple times, with each occurrence increasing the verbosity of the
logging.
On the implementation side, the option is set as part of connecting the
device (piggy-backing on the previously introduced command execution
context), although it describes global state that strictly speaking could
be set anywhere. It is bad enough that libnitrokey just prints log
messages to stderr (and does not accept a file handle) and that it does
not track the log level on a per-device basis, but we don't want setting
of global state from arbitrary locations inside the program. Instead,
let's do that along with what pretty much is the first call into
libnitrokey anyway: the connection to the device.
Diffstat (limited to 'nitrocli/src/args.rs')
-rw-r--r-- | nitrocli/src/args.rs | 12 |
1 files changed, 10 insertions, 2 deletions
diff --git a/nitrocli/src/args.rs b/nitrocli/src/args.rs index cb66bb2..869734c 100644 --- a/nitrocli/src/args.rs +++ b/nitrocli/src/args.rs @@ -31,7 +31,9 @@ type Result<T> = result::Result<T, Error>; /// A command execution context that captures additional data pertaining /// the command execution. #[derive(Debug)] -pub struct ExecCtx {} +pub struct ExecCtx { + pub verbosity: u64, +} /// A top-level command for nitrocli. #[derive(Debug)] @@ -950,9 +952,15 @@ fn pws_status(ctx: &ExecCtx, args: Vec<String>) -> Result<()> { /// Parse the command-line arguments and return the selected command and /// the remaining arguments for the command. fn parse_arguments(args: Vec<String>) -> Result<(Command, ExecCtx, Vec<String>)> { + let mut verbosity = 0; let mut command = Command::Status; let mut subargs = vec![]; let mut parser = argparse::ArgumentParser::new(); + let _ = parser.refer(&mut verbosity).add_option( + &["-v", "--verbose"], + argparse::IncrBy::<u64>(1), + "Increase the log level (can be supplied multiple times)", + ); parser.set_description("Provides access to a Nitrokey device"); let _ = parser.refer(&mut command).required().add_argument( "command", @@ -970,7 +978,7 @@ fn parse_arguments(args: Vec<String>) -> Result<(Command, ExecCtx, Vec<String>)> subargs.insert(0, format!("nitrocli {}", command)); - let ctx = ExecCtx { }; + let ctx = ExecCtx { verbosity }; Ok((command, ctx, subargs)) } |