Skip to content

Commit 2b0506e

Browse files
gameldarTeXitoi
authored andcommitted
Optionally add paw support for structopt as a feature (#192)
This adds a feature that can be turned on that will include the ParseArgs impl when generating the struct and enums
1 parent 18c60e1 commit 2b0506e

5 files changed

Lines changed: 42 additions & 0 deletions

File tree

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
# v0.2.18 (2019-06-26)
2+
3+
* Add optional feature to support `paw` by [@gameldar](https://github.com/gameldar)
4+
([#187](https://github.com/TeXitoi/structopt/issues/187))
5+
16
# v0.2.16 (2019-05-29)
27

38
* Support `Option<Option<T>>` type for fields by [@sphynx](https://github.com/sphynx)

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ lints = ["clap/lints"]
2121
debug = ["clap/debug"]
2222
no_cargo = ["clap/no_cargo"]
2323
doc = ["clap/doc"]
24+
paw = ["structopt-derive/paw"]
2425

2526
[badges]
2627
travis-ci = { repository = "TeXitoi/structopt" }

src/lib.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,18 @@
2121
//! structopt = { version = "0.2", default-features = false }
2222
//! ```
2323
//!
24+
//!
25+
//! Support for [`paw`](https://github.com/rust-cli/paw) (the
26+
//! `Command line argument paw-rser abstraction for main`) is disabled
27+
//! by default, but can be enabled in the `structopt` dependency
28+
//! with the feature `paw`:
29+
//!
30+
//! ```toml
31+
//! [dependencies]
32+
//! structopt = { version = "0.2", features = [ "paw" ] }
33+
//! paw = "1.0"
34+
//! ```
35+
//!
2436
//! ## How to `derive(StructOpt)`
2537
//!
2638
//! First, let's look at an example:

structopt-derive/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ heck = "^0.3.0"
2020

2121
[features]
2222
nightly = ["proc-macro2/nightly"]
23+
paw = []
2324

2425
[lib]
2526
proc-macro = true

structopt-derive/src/lib.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -421,6 +421,23 @@ fn gen_from_subcommand(
421421
}
422422
}
423423

424+
#[cfg(feature = "paw")]
425+
fn gen_paw_impl(name: &Ident) -> TokenStream {
426+
quote! {
427+
impl paw::ParseArgs for #name {
428+
type Error = std::io::Error;
429+
430+
fn parse_args() -> Result<Self, Self::Error> {
431+
Ok(<#name as ::structopt::StructOpt>::from_args())
432+
}
433+
}
434+
}
435+
}
436+
#[cfg(not(feature = "paw"))]
437+
fn gen_paw_impl(_: &Ident) -> TokenStream {
438+
TokenStream::new()
439+
}
440+
424441
fn impl_structopt_for_struct(
425442
name: &Ident,
426443
fields: &Punctuated<Field, Comma>,
@@ -429,6 +446,7 @@ fn impl_structopt_for_struct(
429446
let basic_clap_app_gen = gen_clap_struct(attrs);
430447
let augment_clap = gen_augment_clap(fields, &basic_clap_app_gen.attrs);
431448
let from_clap = gen_from_clap(name, fields, &basic_clap_app_gen.attrs);
449+
let paw_impl = gen_paw_impl(name);
432450

433451
let clap_tokens = basic_clap_app_gen.tokens;
434452
quote! {
@@ -444,6 +462,8 @@ fn impl_structopt_for_struct(
444462
#augment_clap
445463
pub fn is_subcommand() -> bool { false }
446464
}
465+
466+
#paw_impl
447467
}
448468
}
449469

@@ -457,6 +477,7 @@ fn impl_structopt_for_enum(
457477
let augment_clap = gen_augment_clap_enum(variants, &basic_clap_app_gen.attrs);
458478
let from_clap = gen_from_clap_enum(name);
459479
let from_subcommand = gen_from_subcommand(name, variants, &basic_clap_app_gen.attrs);
480+
let paw_impl = gen_paw_impl(name);
460481

461482
let clap_tokens = basic_clap_app_gen.tokens;
462483
quote! {
@@ -472,6 +493,8 @@ fn impl_structopt_for_enum(
472493
#from_subcommand
473494
pub fn is_subcommand() -> bool { true }
474495
}
496+
497+
#paw_impl
475498
}
476499
}
477500

0 commit comments

Comments
 (0)