diff options
author | Robin Krahl <robin.krahl@ireas.org> | 2020-01-07 12:22:36 +0000 |
---|---|---|
committer | Daniel Mueller <deso@posteo.net> | 2020-01-08 09:20:37 -0800 |
commit | 929275b7abda0939eb49e99087af44444bad0a33 (patch) | |
tree | b179b53771ee1725b3fe1568e2ea47d1e9126b5e | |
parent | 30e7ed5ff70766ea4a7de6761d29e012a15f02c9 (diff) | |
download | nitrocli-929275b7abda0939eb49e99087af44444bad0a33.tar.gz nitrocli-929275b7abda0939eb49e99087af44444bad0a33.tar.bz2 |
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.
-rw-r--r-- | nitrocli/src/arg_util.rs | 13 | ||||
-rw-r--r-- | nitrocli/src/args.rs | 44 |
2 files changed, 15 insertions, 42 deletions
diff --git a/nitrocli/src/arg_util.rs b/nitrocli/src/arg_util.rs index 5980f48..fe755f3 100644 --- a/nitrocli/src/arg_util.rs +++ b/nitrocli/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/nitrocli/src/args.rs b/nitrocli/src/args.rs index 0562016..0d84fed 100644 --- a/nitrocli/src/args.rs +++ b/nitrocli/src/args.rs @@ -269,7 +269,7 @@ Enum! {UnencryptedVolumeMode, [ /// Execute an unencrypted subcommand. fn unencrypted(ctx: &mut ExecCtx<'_>, args: Vec<String>) -> 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<String>) -> 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<String>) -> 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<String>) -> 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<String>) -> 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<String>) -> 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<String>) -> Result<()> { /// Execute a config subcommand. fn config(ctx: &mut ExecCtx<'_>, args: Vec<String>) -> 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<String>) -> 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<String>) -> Result<()> { /// Execute an OTP subcommand. fn otp(ctx: &mut ExecCtx<'_>, args: Vec<String>) -> 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<String>) -> 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<String>) -> Result<()> { /// Execute a PIN subcommand. fn pin(ctx: &mut ExecCtx<'_>, args: Vec<String>) -> 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<String>) -> 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<String>) -> Result<()> { fn pws(ctx: &mut ExecCtx<'_>, args: Vec<String>) -> 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<String>) -> 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<String>) -> 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( |