diff options
author | Robin Krahl <robin.krahl@ireas.org> | 2020-01-07 15:15:38 +0000 |
---|---|---|
committer | Daniel Mueller <deso@posteo.net> | 2020-01-08 09:21:09 -0800 |
commit | 8482335491aa55b0458ffedccd1fc110f092e38a (patch) | |
tree | c6d0605ad875eb7ff8bce76b5609df988a419d44 /nitrocli/src/error.rs | |
parent | 70199fb8c1cff9323c7b60a037f3c6193bb32e29 (diff) | |
download | nitrocli-8482335491aa55b0458ffedccd1fc110f092e38a.tar.gz nitrocli-8482335491aa55b0458ffedccd1fc110f092e38a.tar.bz2 |
Replace argparse with structopt
This patch changes the argument handling code to use structopt instead
of argparse using the data structures we introduced in the last patch.
As part of that transition we replace the old Error::ArgparseError
variant with ClapError that stores a structopt::clap::Error.
Because of that replacement, the format of the help messages changed,
breaking some of the tests. Hence, this change adapts them accordingly.
Also clap currently prints the version output to stdout, so we ignore
the version_option test case for now.
Diffstat (limited to 'nitrocli/src/error.rs')
-rw-r--r-- | nitrocli/src/error.rs | 12 |
1 files changed, 10 insertions, 2 deletions
diff --git a/nitrocli/src/error.rs b/nitrocli/src/error.rs index 819bed8..f613100 100644 --- a/nitrocli/src/error.rs +++ b/nitrocli/src/error.rs @@ -22,6 +22,8 @@ use std::io; use std::str; use std::string; +use structopt::clap; + /// A trait used to simplify error handling in conjunction with the /// try_with_* functions we use for repeatedly asking the user for a /// secret. @@ -40,7 +42,7 @@ where #[derive(Debug)] pub enum Error { - ArgparseError(i32), + ClapError(clap::Error), IoError(io::Error), NitrokeyError(Option<&'static str>, nitrokey::Error), Utf8Error(str::Utf8Error), @@ -62,6 +64,12 @@ impl From<&str> for Error { } } +impl From<clap::Error> for Error { + fn from(e: clap::Error) -> Error { + Error::ClapError(e) + } +} + impl From<nitrokey::Error> for Error { fn from(e: nitrokey::Error) -> Error { Error::NitrokeyError(None, e) @@ -89,7 +97,7 @@ impl From<string::FromUtf8Error> for Error { impl fmt::Display for Error { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { match *self { - Error::ArgparseError(_) => write!(f, "Could not parse arguments"), + Error::ClapError(ref e) => write!(f, "{}", e), Error::NitrokeyError(ref ctx, ref e) => { if let Some(ctx) = ctx { write!(f, "{}: ", ctx)?; |