Skip to content

Commit

Permalink
Optionally add paw support for structopt as a feature (#192)
Browse files Browse the repository at this point in the history
This adds a feature that can be turned on that will include the
ParseArgs impl when generating the struct and enums
  • Loading branch information
gameldar authored and TeXitoi committed Jun 1, 2019
1 parent 57b33d3 commit 8068f96
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 0 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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))
Expand Down
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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" }
Expand Down
12 changes: 12 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
1 change: 1 addition & 0 deletions structopt-derive/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ heck = "^0.3.0"

[features]
nightly = ["proc-macro2/nightly"]
paw = []

[lib]
proc-macro = true
23 changes: 23 additions & 0 deletions structopt-derive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Self, Self::Error> {
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<Field, Comma>,
Expand All @@ -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! {
Expand All @@ -456,6 +474,8 @@ fn impl_structopt_for_struct(
#augment_clap
pub fn is_subcommand() -> bool { false }
}

#paw_impl
}
}

Expand All @@ -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! {
Expand All @@ -484,6 +505,8 @@ fn impl_structopt_for_enum(
#from_subcommand
pub fn is_subcommand() -> bool { true }
}

#paw_impl
}
}

Expand Down

0 comments on commit 8068f96

Please sign in to comment.