From d7f1557f103dfa9c6eea644517e42161cbd6b11f Mon Sep 17 00:00:00 2001 From: Robin Krahl Date: Tue, 7 Jan 2020 12:22:36 +0000 Subject: Use strings instead of Command variants In one of the next patches, we will add fields to some Command variants to be able to use them with structopt. Then we will no longer be able to instantiate them directly, so we replace these instances for the transition. This patch also removes the cmd_help! macro that is no longer needed. --- src/arg_util.rs | 13 ------------- src/args.rs | 44 +++++++++++++++----------------------------- 2 files changed, 15 insertions(+), 42 deletions(-) diff --git a/src/arg_util.rs b/src/arg_util.rs index b96f653..6d1bc50 100644 --- a/src/arg_util.rs +++ b/src/arg_util.rs @@ -147,19 +147,6 @@ macro_rules! fmt_enum { }}; } -/// A macro for generating the help text for a command/subcommand. The -/// argument is the variable representing the command (which in turn is -/// an enum). -/// Note that the name of this variable is embedded into the help text! -macro_rules! cmd_help { - ( $cmd:ident ) => { - format!( - concat!("The ", stringify!($cmd), " to execute ({})"), - fmt_enum!($cmd) - ) - }; -} - #[cfg(test)] mod tests { Enum! {Command, [ diff --git a/src/args.rs b/src/args.rs index 0c8feb3..4ab332e 100644 --- a/src/args.rs +++ b/src/args.rs @@ -269,7 +269,7 @@ Enum! {UnencryptedVolumeMode, [ /// Execute an unencrypted subcommand. fn unencrypted(ctx: &mut ExecCtx<'_>, args: Vec) -> Result<()> { let mut subcommand = UnencryptedCommand::Set; - let help = cmd_help!(subcommand); + let help = "".to_string(); let mut subargs = vec![]; let mut parser = argparse::ArgumentParser::new(); parser.set_description("Interacts with the device's unencrypted volume"); @@ -288,12 +288,7 @@ fn unencrypted(ctx: &mut ExecCtx<'_>, args: Vec) -> Result<()> { subargs.insert( 0, - format!( - "{} {} {}", - crate::NITROCLI, - Command::Unencrypted, - subcommand, - ), + format!("{} {} {}", crate::NITROCLI, "unencrypted", subcommand,), ); subcommand.execute(ctx, subargs) } @@ -322,7 +317,7 @@ Command! {EncryptedCommand, [ /// Execute an encrypted subcommand. fn encrypted(ctx: &mut ExecCtx<'_>, args: Vec) -> Result<()> { let mut subcommand = EncryptedCommand::Open; - let help = cmd_help!(subcommand); + let help = "".to_string(); let mut subargs = vec![]; let mut parser = argparse::ArgumentParser::new(); parser.set_description("Interacts with the device's encrypted volume"); @@ -341,7 +336,7 @@ fn encrypted(ctx: &mut ExecCtx<'_>, args: Vec) -> Result<()> { subargs.insert( 0, - format!("{} {} {}", crate::NITROCLI, Command::Encrypted, subcommand), + format!("{} {} {}", crate::NITROCLI, "encrypted", subcommand), ); subcommand.execute(ctx, subargs) } @@ -373,7 +368,7 @@ Command! {HiddenCommand, [ /// Execute a hidden subcommand. fn hidden(ctx: &mut ExecCtx<'_>, args: Vec) -> Result<()> { let mut subcommand = HiddenCommand::Open; - let help = cmd_help!(subcommand); + let help = "".to_string(); let mut subargs = vec![]; let mut parser = argparse::ArgumentParser::new(); parser.set_description("Interacts with the device's hidden volume"); @@ -392,7 +387,7 @@ fn hidden(ctx: &mut ExecCtx<'_>, args: Vec) -> Result<()> { subargs.insert( 0, - format!("{} {} {}", crate::NITROCLI, Command::Hidden, subcommand), + format!("{} {} {}", crate::NITROCLI, "hidden", subcommand), ); subcommand.execute(ctx, subargs) } @@ -444,7 +439,7 @@ fn hidden_close(ctx: &mut ExecCtx<'_>, args: Vec) -> Result<()> { /// Execute a config subcommand. fn config(ctx: &mut ExecCtx<'_>, args: Vec) -> Result<()> { let mut subcommand = ConfigCommand::Get; - let help = cmd_help!(subcommand); + let help = "".to_string(); let mut subargs = vec![]; let mut parser = argparse::ArgumentParser::new(); parser.set_description("Reads or writes the device configuration"); @@ -463,7 +458,7 @@ fn config(ctx: &mut ExecCtx<'_>, args: Vec) -> Result<()> { subargs.insert( 0, - format!("{} {} {}", crate::NITROCLI, Command::Config, subcommand), + format!("{} {} {}", crate::NITROCLI, "config", subcommand), ); subcommand.execute(ctx, subargs) } @@ -556,7 +551,7 @@ fn lock(ctx: &mut ExecCtx<'_>, args: Vec) -> Result<()> { /// Execute an OTP subcommand. fn otp(ctx: &mut ExecCtx<'_>, args: Vec) -> Result<()> { let mut subcommand = OtpCommand::Get; - let help = cmd_help!(subcommand); + let help = "".to_string(); let mut subargs = vec![]; let mut parser = argparse::ArgumentParser::new(); parser.set_description("Accesses one-time passwords"); @@ -573,10 +568,7 @@ fn otp(ctx: &mut ExecCtx<'_>, args: Vec) -> Result<()> { parser.stop_on_first_argument(true); parse(ctx, parser, args)?; - subargs.insert( - 0, - format!("{} {} {}", crate::NITROCLI, Command::Otp, subcommand), - ); + subargs.insert(0, format!("{} {} {}", crate::NITROCLI, "otp", subcommand)); subcommand.execute(ctx, subargs) } @@ -725,7 +717,7 @@ fn otp_status(ctx: &mut ExecCtx<'_>, args: Vec) -> Result<()> { /// Execute a PIN subcommand. fn pin(ctx: &mut ExecCtx<'_>, args: Vec) -> Result<()> { let mut subcommand = PinCommand::Clear; - let help = cmd_help!(subcommand); + let help = "".to_string(); let mut subargs = vec![]; let mut parser = argparse::ArgumentParser::new(); parser.set_description("Manages the Nitrokey PINs"); @@ -742,10 +734,7 @@ fn pin(ctx: &mut ExecCtx<'_>, args: Vec) -> Result<()> { parser.stop_on_first_argument(true); parse(ctx, parser, args)?; - subargs.insert( - 0, - format!("{} {} {}", crate::NITROCLI, Command::Pin, subcommand), - ); + subargs.insert(0, format!("{} {} {}", crate::NITROCLI, "pin", subcommand)); subcommand.execute(ctx, subargs) } @@ -786,7 +775,7 @@ fn pin_unblock(ctx: &mut ExecCtx<'_>, args: Vec) -> Result<()> { fn pws(ctx: &mut ExecCtx<'_>, args: Vec) -> Result<()> { let mut subcommand = PwsCommand::Get; let mut subargs = vec![]; - let help = cmd_help!(subcommand); + let help = "".to_string(); let mut parser = argparse::ArgumentParser::new(); parser.set_description("Accesses the password safe"); let _ = @@ -802,10 +791,7 @@ fn pws(ctx: &mut ExecCtx<'_>, args: Vec) -> Result<()> { parser.stop_on_first_argument(true); parse(ctx, parser, args)?; - subargs.insert( - 0, - format!("{} {} {}", crate::NITROCLI, Command::Pws, subcommand), - ); + subargs.insert(0, format!("{} {} {}", crate::NITROCLI, "pws", subcommand)); subcommand.execute(ctx, subargs) } @@ -923,7 +909,7 @@ pub(crate) fn handle_arguments(ctx: &mut RunCtx<'_>, args: Vec) -> Resul ); let mut verbosity = 0; let mut command = Command::Status; - let cmd_help = cmd_help!(command); + let cmd_help = "".to_string(); let mut subargs = vec![]; let mut parser = argparse::ArgumentParser::new(); let _ = parser.refer(&mut version).add_option( -- cgit v1.2.3