diff options
author | Nora Widdecke <nora@sequoia-pgp.org> | 2022-06-02 15:07:32 +0200 |
---|---|---|
committer | Nora Widdecke <nora@sequoia-pgp.org> | 2022-06-08 14:49:43 +0200 |
commit | e5e30e23f77ff9d9c74b7c2f377c8a471619fc15 (patch) | |
tree | 732dcaec59452f566938da78fb3a6b1438c7dae8 | |
parent | b7c5c9416d2a7ee8fdc6dab1df8f10c061ea029f (diff) |
sq: Make input and output arguments reusable.
- Extract input and output arguments, including help texts, so they
can be easily reused by other sq subcommands.
- This is part of the effort of moving to clap3's derive API and
profit from the added type safety.
-rw-r--r-- | sq/src/sq.rs | 12 | ||||
-rw-r--r-- | sq/src/sq_cli.rs | 48 |
2 files changed, 26 insertions, 34 deletions
diff --git a/sq/src/sq.rs b/sq/src/sq.rs index f08513c0..09565961 100644 --- a/sq/src/sq.rs +++ b/sq/src/sq.rs @@ -491,8 +491,8 @@ fn main() -> Result<()> { use clap::FromArgMatches; let command = sq_cli::SignCommand::from_arg_matches(m)?; - let mut input = open_or_stdin(command.input.as_deref())?; - let output = command.output.as_deref(); + let mut input = open_or_stdin(command.io.input.as_deref())?; + let output = command.io.output.as_deref(); let detached = command.detached; let binary = command.binary; let append = command.append; @@ -560,9 +560,9 @@ fn main() -> Result<()> { let command = sq_cli::VerifyCommand::from_arg_matches(m)?; // TODO: Fix interface of open_or_stdin, create_or_stdout_safe, etc. - let mut input = open_or_stdin(command.input.as_deref())?; + let mut input = open_or_stdin(command.io.input.as_deref())?; let mut output = - config.create_or_stdout_safe(command.output.as_deref())?; + config.create_or_stdout_safe(command.io.output.as_deref())?; let mut detached = if let Some(f) = command.detached { Some(File::open(f)?) } else { @@ -634,9 +634,9 @@ fn main() -> Result<()> { use clap::FromArgMatches; let command = sq_cli::DearmorCommand::from_arg_matches(m)?; - let mut input = open_or_stdin(command.input.as_deref())?; + let mut input = open_or_stdin(command.io.input.as_deref())?; let mut output = - config.create_or_stdout_safe(command.output.as_deref())?; + config.create_or_stdout_safe(command.io.output.as_deref())?; let mut filter = armor::Reader::from_reader(&mut input, None); io::copy(&mut filter, &mut output)?; }, diff --git a/sq/src/sq_cli.rs b/sq/src/sq_cli.rs index 06e830a3..1b328612 100644 --- a/sq/src/sq_cli.rs +++ b/sq/src/sq_cli.rs @@ -1,5 +1,5 @@ /// Command-line parser for sq. -use clap::{Arg, ArgGroup, Command, ArgEnum}; +use clap::{Arg, ArgGroup, Command, ArgEnum, Args}; use clap::{CommandFactory, Parser}; pub fn build() -> Command<'static> { @@ -1716,6 +1716,19 @@ $ sq autocrypt encode-sender --prefer-encrypt mutual juliet.pgp app } +#[derive(Debug, Args)] +pub struct IoArgs { + #[clap(value_name = "FILE", help = "Reads from FILE or stdin if omitted")] + pub input: Option<String>, + #[clap( + short, + long, + value_name = "FILE", + help = "Writes to FILE or stdout if omitted" + )] + pub output: Option<String>, +} + // 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 @@ -1818,15 +1831,8 @@ $ sq dearmor ascii-message.pgp ", )] pub struct DearmorCommand { - #[clap(value_name = "FILE", help = "Reads from FILE or stdin if omitted")] - pub input: Option<String>, - #[clap( - short, - long, - value_name = "FILE", - help = "Writes to FILE or stdout if omitted" - )] - pub output: Option<String>, + #[clap(flatten)] + pub io: IoArgs, } #[derive(Parser, Debug)] @@ -1868,15 +1874,8 @@ signatures, consider using sequoia-sqv. ", )] pub struct VerifyCommand { - #[clap(value_name = "FILE", help = "Reads from FILE or stdin if omitted")] - pub input: Option<String>, - #[clap( - short, - long, - value_name = "FILE", - help = "Writes to FILE or stdout if omitted" - )] - pub output: Option<String>, + #[clap(flatten)] + pub io: IoArgs, #[clap( long = "detached", value_name = "SIG", @@ -1931,15 +1930,8 @@ $ sq sign --detached --signer-key juliet.pgp message.txt ", )] pub struct SignCommand { - #[clap(value_name = "FILE", help = "Reads from FILE or stdin if omitted")] - pub input: Option<String>, - #[clap( - short, - long, - value_name = "FILE", - help = "Writes to FILE or stdout if omitted" - )] - pub output: Option<String>, + #[clap(flatten)] + pub io: IoArgs, // TODO: Why capital B? #[clap( short = 'B', |