From d69902a3a733a08a8c63f15ffb1fb6da633e1546 Mon Sep 17 00:00:00 2001 From: Kirk Turner Date: Thu, 30 May 2019 00:58:25 +0800 Subject: [PATCH] Optionally add paw support for structopt as a feature This adds a feature that can be turned on that will include the ParseArgs impl when generating the struct and enums --- CHANGELOG.md | 2 ++ Cargo.toml | 1 + src/lib.rs | 12 ++++++++++++ structopt-derive/Cargo.toml | 1 + structopt-derive/src/lib.rs | 23 +++++++++++++++++++++++ 5 files changed, 39 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1459b235..6e902038 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,5 @@ +* Add optional feature to support `paw` by [@gameldar](https://github.com/gameldar) + ([#187](https://github.com/TeXitoi/structopt/issues/187)) * Support optional vectors of arguments for distinguishing between `-o 1 2`, `-o` and no option provided at all by [@sphynx](https://github.com/sphynx) ([#180](https://github.com/TeXitoi/structopt/issues/188)) diff --git a/Cargo.toml b/Cargo.toml index 568adaad..c48908df 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -21,6 +21,7 @@ lints = ["clap/lints"] debug = ["clap/debug"] no_cargo = ["clap/no_cargo"] doc = ["clap/doc"] +paw = ["structopt-derive/paw"] [badges] travis-ci = { repository = "TeXitoi/structopt" } diff --git a/src/lib.rs b/src/lib.rs index 36735107..d15ed674 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -21,6 +21,18 @@ //! structopt = { version = "0.2", default-features = false } //! ``` //! +//! +//! Support for [`paw`](https://github.com/rust-cli/paw) (the +//! `Command line argument paw-rser abstraction for main`) is disabled +//! by default, but can be enabled in the `structopt` dependency +//! with the feature `paw`: +//! +//! ```toml +//! [dependencies] +//! structopt = { version = "0.2", features = [ "paw" ] } +//! paw = "1.0" +//! ``` +//! //! ## How to `derive(StructOpt)` //! //! First, let's look at an example: diff --git a/structopt-derive/Cargo.toml b/structopt-derive/Cargo.toml index 912f26d5..fc5484e1 100644 --- a/structopt-derive/Cargo.toml +++ b/structopt-derive/Cargo.toml @@ -20,6 +20,7 @@ heck = "^0.3.0" [features] nightly = ["proc-macro2/nightly"] +paw = [] [lib] proc-macro = true diff --git a/structopt-derive/src/lib.rs b/structopt-derive/src/lib.rs index 53c5c870..58f4e905 100644 --- a/structopt-derive/src/lib.rs +++ b/structopt-derive/src/lib.rs @@ -433,6 +433,23 @@ fn gen_from_subcommand( } } +#[cfg(feature = "paw")] +fn gen_paw_impl(name: &Ident) -> TokenStream { + quote! { + impl paw::ParseArgs for #name { + type Error = std::io::Error; + + fn parse_args() -> Result { + Ok(<#name as ::structopt::StructOpt>::from_args()) + } + } + } +} +#[cfg(not(feature = "paw"))] +fn gen_paw_impl(_: &Ident) -> TokenStream { + TokenStream::new() +} + fn impl_structopt_for_struct( name: &Ident, fields: &Punctuated, @@ -441,6 +458,7 @@ fn impl_structopt_for_struct( let basic_clap_app_gen = gen_clap_struct(attrs); let augment_clap = gen_augment_clap(fields, &basic_clap_app_gen.attrs); let from_clap = gen_from_clap(name, fields, &basic_clap_app_gen.attrs); + let paw_impl = gen_paw_impl(name); let clap_tokens = basic_clap_app_gen.tokens; quote! { @@ -456,6 +474,8 @@ fn impl_structopt_for_struct( #augment_clap pub fn is_subcommand() -> bool { false } } + + #paw_impl } } @@ -469,6 +489,7 @@ fn impl_structopt_for_enum( let augment_clap = gen_augment_clap_enum(variants, &basic_clap_app_gen.attrs); let from_clap = gen_from_clap_enum(name); let from_subcommand = gen_from_subcommand(name, variants, &basic_clap_app_gen.attrs); + let paw_impl = gen_paw_impl(name); let clap_tokens = basic_clap_app_gen.tokens; quote! { @@ -484,6 +505,8 @@ fn impl_structopt_for_enum( #from_subcommand pub fn is_subcommand() -> bool { true } } + + #paw_impl } }