summaryrefslogtreecommitdiffstats
path: root/sq
diff options
context:
space:
mode:
authorNora Widdecke <nora@sequoia-pgp.org>2022-05-30 22:54:22 +0200
committerNora Widdecke <nora@sequoia-pgp.org>2022-06-08 14:49:43 +0200
commitb51f06bce1c8a0b4c2fb9a9f600334e84c009e54 (patch)
treed2a1a9a664ec8aef80502211be80207e41d76232 /sq
parent2339c33fb944dfab027c4eeb13db146d5069177b (diff)
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.
Diffstat (limited to 'sq')
-rw-r--r--sq/src/sq.rs11
-rw-r--r--sq/src/sq_cli.rs40
2 files changed, 40 insertions, 11 deletions
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<armor::Kind> = 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<CliArmorKind> for Option<OpenPGPArmorKind> {
+ 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)]