diff options
author | Daniel Mueller <deso@posteo.net> | 2019-01-15 15:26:56 -0800 |
---|---|---|
committer | Daniel Mueller <deso@posteo.net> | 2019-01-15 15:26:56 -0800 |
commit | 984f5fadd1bfcd0184af8bddc4973acfe8affb64 (patch) | |
tree | b2a637f7b261964a949ccc3a2f80970cd39ea7a6 /nitrocli/src/args.rs | |
parent | 74b9b3845846e0fbc2efd37d80b5527be1bee564 (diff) | |
download | nitrocli-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.
Diffstat (limited to 'nitrocli/src/args.rs')
-rw-r--r-- | nitrocli/src/args.rs | 49 |
1 files changed, 29 insertions, 20 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); |