From b51f06bce1c8a0b4c2fb9a9f600334e84c009e54 Mon Sep 17 00:00:00 2001 From: Nora Widdecke Date: Mon, 30 May 2022 22:54:22 +0200 Subject: sq: Adapt armor subcommand to clap3's derive style. - This is part of the effort of moving to clap3's derive API and profit from the added type safety. --- sq/src/sq.rs | 11 +++++++---- sq/src/sq_cli.rs | 40 +++++++++++++++++++++++++++++++++------- 2 files changed, 40 insertions(+), 11 deletions(-) (limited to 'sq') diff --git a/sq/src/sq.rs b/sq/src/sq.rs index 308e49f2..94ec6e11 100644 --- a/sq/src/sq.rs +++ b/sq/src/sq.rs @@ -570,8 +570,11 @@ fn main() -> Result<()> { }, Some(("armor", m)) => { - let input = open_or_stdin(m.value_of("input"))?; - let mut want_kind = parse_armor_kind(m.value_of("kind")); + use clap::FromArgMatches; + let command = sq_cli::ArmorCommand::from_arg_matches(m)?; + + let input = open_or_stdin(command.input.as_deref())?; + let mut want_kind: Option = command.kind.into(); // Peek at the data. If it looks like it is armored // data, avoid armoring it again. @@ -590,7 +593,7 @@ fn main() -> Result<()> { { // It is already armored and has the correct kind. let mut output = - config.create_or_stdout_safe(m.value_of("output"))?; + config.create_or_stdout_safe(command.output.as_deref())?; io::copy(&mut input, &mut output)?; return Ok(()); } @@ -605,7 +608,7 @@ fn main() -> Result<()> { let want_kind = want_kind.expect("given or detected"); let mut output = - config.create_or_stdout_pgp(m.value_of("output"), + config.create_or_stdout_pgp(command.output.as_deref(), false, want_kind)?; if already_armored { diff --git a/sq/src/sq_cli.rs b/sq/src/sq_cli.rs index 66aa4180..be01fc2e 100644 --- a/sq/src/sq_cli.rs +++ b/sq/src/sq_cli.rs @@ -1,7 +1,6 @@ /// Command-line parser for sq. - -use clap::{Command, Arg, ArgGroup}; -use clap::{Parser, CommandFactory}; +use clap::{Arg, ArgGroup, Command, ArgEnum}; +use clap::{CommandFactory, Parser}; pub fn build() -> Command<'static> { configure(Command::new("sq"), @@ -1863,7 +1862,6 @@ $ sq autocrypt encode-sender --prefer-encrypt mutual juliet.pgp app } -// TODO: convert possible values to enum // TODO?: Option<_> conflicts with default value // TODO: Use PathBuf as input type for more type safety? Investigate conversion // TODO: use indoc to transparently (de-)indent static strings @@ -1906,10 +1904,38 @@ pub struct ArmorCommand { long = "label", value_name = "LABEL", help = "Selects the kind of armor header", - possible_values = &["auto", "message", "cert", "key", "sig", "file"], - default_value = "auto", + default_value_t = CliArmorKind::Auto, + arg_enum )] - kind: String, + pub kind: CliArmorKind, +} + +#[derive(ArgEnum)] +#[derive(Debug, Clone)] +pub enum CliArmorKind { + Auto, + Message, + #[clap(name = "cert")] + PublicKey, + #[clap(name = "key")] + SecretKey, + #[clap(name = "sig")] + Signature, + File, +} + +use sequoia_openpgp::armor::Kind as OpenPGPArmorKind; +impl From for Option { + fn from(c: CliArmorKind) -> Self { + match c { + CliArmorKind::Auto => None, + CliArmorKind::Message => Some(OpenPGPArmorKind::Message), + CliArmorKind::PublicKey => Some(OpenPGPArmorKind::PublicKey), + CliArmorKind::SecretKey => Some(OpenPGPArmorKind::SecretKey), + CliArmorKind::Signature => Some(OpenPGPArmorKind::Signature), + CliArmorKind::File => Some(OpenPGPArmorKind::File), + } + } } #[derive(Parser, Debug)] -- cgit v1.2.3