From 9164286525fbe5449012f09fcdcff97f278ff51e Mon Sep 17 00:00:00 2001 From: Nora Widdecke Date: Fri, 13 May 2022 00:06:14 +0200 Subject: add a subcommands I tried to have both subcommands in one Enum, that derives Parser, but that adds another layer of subcommands for which I haven't found a workaround. --- sq/src/sq-usage.rs | 30 ++++++++++++++++++++++++++++++ sq/src/sq.rs | 8 ++++++++ sq/src/sq_cli.rs | 23 ++++++++++++++++++++++- 3 files changed, 60 insertions(+), 1 deletion(-) diff --git a/sq/src/sq-usage.rs b/sq/src/sq-usage.rs index 66ba9d38..1977f77d 100644 --- a/sq/src/sq-usage.rs +++ b/sq/src/sq-usage.rs @@ -66,6 +66,10 @@ //! Low-level packet manipulation //! revoke //! Generates revocation certificates +//! baz +//! baz help text +//! foo +//! foo help text //! help //! Print this message or the help of the given subcommand(s) //! ``` @@ -2038,6 +2042,32 @@ //! //! For more information try --help //! ``` +//! +//! ## Subcommand baz +//! +//! ```text +//! baz help text +//! +//! USAGE: +//! sq baz --quux +//! +//! OPTIONS: +//! -h, --help Print help information +//! -q, --quux quux argument +//! ``` +//! +//! ## Subcommand foo +//! +//! ```text +//! foo help text +//! +//! USAGE: +//! sq foo [OPTIONS] +//! +//! OPTIONS: +//! -b, --bar bar flag +//! -h, --help Print help information +//! ``` #![doc(html_favicon_url = "https://docs.sequoia-pgp.org/favicon.png")] #![doc(html_logo_url = "https://docs.sequoia-pgp.org/logo.svg")] diff --git a/sq/src/sq.rs b/sq/src/sq.rs index bb481065..326958b0 100644 --- a/sq/src/sq.rs +++ b/sq/src/sq.rs @@ -706,6 +706,14 @@ fn main() -> Result<()> { commands::certify::certify(config, m)?; }, + Some(("foo", m)) => { + use clap::FromArgMatches; + let fc = sq_cli::FooCommand::from_arg_matches(m)?; + println!("hello foo :), {:?}", fc) + } + Some((_, m)) => { + println!("hello :), {:?}", m) + } _ => unreachable!(), } diff --git a/sq/src/sq_cli.rs b/sq/src/sq_cli.rs index 4c9d3846..2a8b037b 100644 --- a/sq/src/sq_cli.rs +++ b/sq/src/sq_cli.rs @@ -1,6 +1,7 @@ /// Command-line parser for sq. use clap::{Command, Arg, ArgGroup}; +use clap::{Parser, CommandFactory}; pub fn build() -> Command<'static> { configure(Command::new("sq"), @@ -1923,7 +1924,27 @@ $ sq autocrypt encode-sender --prefer-encrypt mutual juliet.pgp attribute")) ) ) - }; + } + .subcommand(FooCommand::command()) + .subcommand(BazCommand::command()); app } + +#[derive(Parser, Debug)] +#[clap(name = "foo")] +#[clap(about="foo help text")] +pub struct FooCommand { + /// bar flag + #[clap(short, long)] + bar: bool, +} + +#[derive(Parser, Debug)] +#[clap(name = "baz")] +#[clap(about="baz help text")] +pub struct BazCommand { + /// quux argument + #[clap(short, long)] + quux: String, +} -- cgit v1.2.3