From 984f5fadd1bfcd0184af8bddc4973acfe8affb64 Mon Sep 17 00:00:00 2001 From: Daniel Mueller Date: Tue, 15 Jan 2019 15:26:56 -0800 Subject: 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. --- nitrocli/src/args.rs | 49 +++++++++++++++++++++++++++++-------------------- 1 file changed, 29 insertions(+), 20 deletions(-) (limited to 'nitrocli/src/args.rs') 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) -> Result<()> { fn otp_get(ctx: &mut ExecCtx<'_>, args: Vec) -> 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 = 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) -> 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) -> Result<()> { pub fn otp_set(ctx: &mut ExecCtx<'_>, args: Vec) -> 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) -> 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) -> Result<()> { fn otp_clear(ctx: &mut ExecCtx<'_>, args: Vec) -> 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) -> 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) -> Result<()> { /// Change a PIN. fn pin_set(ctx: &mut ExecCtx<'_>, args: Vec) -> 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); -- cgit v1.2.1