diff options
author | Robin Krahl <robin.krahl@ireas.org> | 2020-01-07 15:15:38 +0000 |
---|---|---|
committer | Daniel Mueller <deso@posteo.net> | 2020-01-07 15:15:38 +0000 |
commit | c1f35ab538dbdf3002a6a9aa0932ada687160787 (patch) | |
tree | 965ade0fc53cebb3ad8da04144592031a68d0b26 /src/error.rs | |
parent | b9fbc489819a111f21eecb2b9a07a931dca1b8ce (diff) | |
download | nitrocli-c1f35ab538dbdf3002a6a9aa0932ada687160787.tar.gz nitrocli-c1f35ab538dbdf3002a6a9aa0932ada687160787.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 'src/error.rs')
-rw-r--r-- | src/error.rs | 14 |
1 files changed, 11 insertions, 3 deletions
diff --git a/src/error.rs b/src/error.rs index 819bed8..3e458a6 100644 --- a/src/error.rs +++ b/src/error.rs @@ -1,7 +1,7 @@ // error.rs // ************************************************************************* -// * Copyright (C) 2017-2019 Daniel Mueller (deso@posteo.net) * +// * Copyright (C) 2017-2020 Daniel Mueller (deso@posteo.net) * // * * // * This program is free software: you can redistribute it and/or modify * // * it under the terms of the GNU General Public License as published by * @@ -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)?; |