aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDaniel Mueller <deso@posteo.net>2019-01-15 15:26:56 -0800
committerDaniel Mueller <deso@posteo.net>2019-01-15 15:26:56 -0800
commit984f5fadd1bfcd0184af8bddc4973acfe8affb64 (patch)
treeb2a637f7b261964a949ccc3a2f80970cd39ea7a6
parent74b9b3845846e0fbc2efd37d80b5527be1bee564 (diff)
downloadnitrocli-984f5fadd1bfcd0184af8bddc4973acfe8affb64.tar.gz
nitrocli-984f5fadd1bfcd0184af8bddc4973acfe8affb64.tar.bz2
Auto-generate help text for enum-backed optional arguments
This change continues the effort of auto-generating more of the help text content by extending the logic to optional arguments. We make use of the fmt_enum macro to format the description of the argument with the available variants (as well as the default, if any) interpolated.
-rw-r--r--nitrocli/src/args.rs49
-rw-r--r--nitrocli/src/pinentry.rs23
2 files changed, 33 insertions, 39 deletions
diff --git a/nitrocli/src/args.rs b/nitrocli/src/args.rs
index 83a0999..e6267e7 100644
--- a/nitrocli/src/args.rs
+++ b/nitrocli/src/args.rs
@@ -389,6 +389,11 @@ fn otp(ctx: &mut ExecCtx<'_>, args: Vec<String>) -> Result<()> {
fn otp_get(ctx: &mut ExecCtx<'_>, args: Vec<String>) -> Result<()> {
let mut slot: u8 = 0;
let mut algorithm = OtpAlgorithm::Totp;
+ let help = format!(
+ "The OTP algorithm to use ({}, default: {})",
+ fmt_enum!(algorithm),
+ algorithm
+ );
let mut time: Option<u64> = None;
let mut parser = argparse::ArgumentParser::new();
parser.set_description("Generates a one-time password");
@@ -397,11 +402,9 @@ fn otp_get(ctx: &mut ExecCtx<'_>, args: Vec<String>) -> Result<()> {
.refer(&mut slot)
.required()
.add_argument("slot", argparse::Store, "The OTP slot to use");
- let _ = parser.refer(&mut algorithm).add_option(
- &["-a", "--algorithm"],
- argparse::Store,
- "The OTP algorithm to use (hotp|totp, default: totp)",
- );
+ let _ = parser
+ .refer(&mut algorithm)
+ .add_option(&["-a", "--algorithm"], argparse::Store, &help);
let _ = parser.refer(&mut time).add_option(
&["-t", "--time"],
argparse::StoreOption,
@@ -417,6 +420,11 @@ fn otp_get(ctx: &mut ExecCtx<'_>, args: Vec<String>) -> Result<()> {
pub fn otp_set(ctx: &mut ExecCtx<'_>, args: Vec<String>) -> Result<()> {
let mut slot: u8 = 0;
let mut algorithm = OtpAlgorithm::Totp;
+ let help = format!(
+ "The OTP algorithm to use ({}, default: {})",
+ fmt_enum!(algorithm),
+ algorithm
+ );
let mut name = "".to_owned();
let mut secret = "".to_owned();
let mut digits = OtpMode::SixDigits;
@@ -431,11 +439,9 @@ pub fn otp_set(ctx: &mut ExecCtx<'_>, args: Vec<String>) -> Result<()> {
.refer(&mut slot)
.required()
.add_argument("slot", argparse::Store, "The OTP slot to use");
- let _ = parser.refer(&mut algorithm).add_option(
- &["-a", "--algorithm"],
- argparse::Store,
- "The OTP algorithm to use (hotp|totp, default: totp)",
- );
+ let _ = parser
+ .refer(&mut algorithm)
+ .add_option(&["-a", "--algorithm"], argparse::Store, &help);
let _ = parser.refer(&mut name).required().add_argument(
"name",
argparse::Store,
@@ -504,6 +510,11 @@ pub fn otp_set(ctx: &mut ExecCtx<'_>, args: Vec<String>) -> Result<()> {
fn otp_clear(ctx: &mut ExecCtx<'_>, args: Vec<String>) -> Result<()> {
let mut slot: u8 = 0;
let mut algorithm = OtpAlgorithm::Totp;
+ let help = format!(
+ "The OTP algorithm to use ({}, default: {})",
+ fmt_enum!(algorithm),
+ algorithm
+ );
let mut parser = argparse::ArgumentParser::new();
parser.set_description("Clears a one-time password slot");
let _ = parser.refer(&mut slot).required().add_argument(
@@ -511,11 +522,9 @@ fn otp_clear(ctx: &mut ExecCtx<'_>, args: Vec<String>) -> Result<()> {
argparse::Store,
"The OTP slot to clear",
);
- let _ = parser.refer(&mut algorithm).add_option(
- &["-a", "--algorithm"],
- argparse::Store,
- "The OTP algorithm to use (hotp|totp, default: totp)",
- );
+ let _ = parser
+ .refer(&mut algorithm)
+ .add_option(&["-a", "--algorithm"], argparse::Store, &help);
parse(ctx, &parser, args)?;
drop(parser);
@@ -575,13 +584,13 @@ fn pin_clear(ctx: &mut ExecCtx<'_>, args: Vec<String>) -> Result<()> {
/// Change a PIN.
fn pin_set(ctx: &mut ExecCtx<'_>, args: Vec<String>) -> Result<()> {
let mut pintype = pinentry::PinType::User;
+ let help = format!("The PIN type to change ({})", fmt_enum!(pintype));
let mut parser = argparse::ArgumentParser::new();
parser.set_description("Changes a PIN");
- let _ = parser.refer(&mut pintype).required().add_argument(
- "type",
- argparse::Store,
- "The PIN type to change (admin|user)",
- );
+ let _ = parser
+ .refer(&mut pintype)
+ .required()
+ .add_argument("type", argparse::Store, &help);
parse(ctx, &parser, args)?;
drop(parser);
diff --git a/nitrocli/src/pinentry.rs b/nitrocli/src/pinentry.rs
index 1eecdd0..d6f000c 100644
--- a/nitrocli/src/pinentry.rs
+++ b/nitrocli/src/pinentry.rs
@@ -26,25 +26,10 @@ use crate::error::Error;
///
/// The available PIN types correspond to the PIN types used by the Nitrokey devices: user and
/// admin.
-#[derive(Clone, Copy, Debug, PartialEq)]
-pub enum PinType {
- /// The admin PIN.
- Admin,
- /// The user PIN.
- User,
-}
-
-impl str::FromStr for PinType {
- type Err = ();
-
- fn from_str(s: &str) -> Result<Self, Self::Err> {
- match s {
- "admin" => Ok(PinType::Admin),
- "user" => Ok(PinType::User),
- _ => Err(()),
- }
- }
-}
+Enum! {PinType, [
+ Admin => "admin",
+ User => "user"
+]}
#[derive(Debug)]
pub struct PinEntry {