Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add from_flag parser #271

Merged
merged 2 commits into from
Oct 10, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -684,6 +684,7 @@
//! | `from_os_str` | `fn(&OsStr) -> T` | `::std::convert::From::from` |
//! | `try_from_os_str` | `fn(&OsStr) -> Result<T, OsString>` | (no default function) |
//! | `from_occurrences`| `fn(u64) -> T` | `value as T` |
//! | `from_flag` | `fn(bool) -> T` | `::std::convert::From::from` |
//!
//! The `from_occurrences` parser is special. Using `parse(from_occurrences)`
//! results in the _number of flags occurrences_ being stored in the relevant
Expand All @@ -692,6 +693,10 @@
//! `.takes_value(false).multiple(true)`. Note that the default parser can only
//! be used with fields of integer types (`u8`, `usize`, `i64`, etc.).
//!
//! The `from_flag` parser is also special. Using `parse(from_flag)` or
//! `parse(from_flag = some_func)` will result in the field being treated as a
//! flag even if it does not have type `bool`.
//!
//! When supplying a custom string parser, `bool` will not be treated specially:
//!
//! Type | Effect | Added method call to `clap::Arg`
Expand Down
3 changes: 3 additions & 0 deletions structopt-derive/src/attrs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ pub enum ParserKind {
FromOsStr,
TryFromOsStr,
FromOccurrences,
FromFlag,
}

/// Defines the casing for the attributes long representation.
Expand Down Expand Up @@ -141,6 +142,7 @@ impl Parser {
"from_os_str" => FromOsStr,
"try_from_os_str" => TryFromOsStr,
"from_occurrences" => FromOccurrences,
"from_flag" => FromFlag,
s => span_error!(spec.kind.span(), "unsupported parser `{}`", s),
};

Expand All @@ -155,6 +157,7 @@ impl Parser {
"cannot omit parser function name with `try_from_os_str`"
),
FromOccurrences => quote_spanned!(spec.kind.span()=> { |v| v as _ }),
FromFlag => quote_spanned!(spec.kind.span()=> ::std::convert::From::from),
},

Some(func) => match func {
Expand Down
16 changes: 16 additions & 0 deletions structopt-derive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ fn gen_augmentation(
};

let occurrences = *attrs.parser().kind == ParserKind::FromOccurrences;
let flag = *attrs.parser().kind == ParserKind::FromFlag;

let parser = attrs.parser();
let func = &parser.func;
Expand Down Expand Up @@ -174,6 +175,11 @@ fn gen_augmentation(
.multiple(true)
},

Ty::Other if flag => quote_spanned! { ty.span()=>
.takes_value(false)
.multiple(false)
},

Ty::Other => {
let required = !attrs.has_method("default_value");
quote_spanned! { ty.span()=>
Expand Down Expand Up @@ -267,8 +273,14 @@ fn gen_constructor(fields: &Punctuated<Field, Comma>, parent_attribute: &Attrs)
quote!(),
func.clone(),
),
FromFlag => (
quote!(),
quote!(),
func.clone(),
),
};

let flag = *attrs.parser().kind == ParserKind::FromFlag;
let occurrences = *attrs.parser().kind == ParserKind::FromOccurrences;
let name = attrs.cased_name();
let field_value = match **ty {
Expand Down Expand Up @@ -305,6 +317,10 @@ fn gen_constructor(fields: &Punctuated<Field, Comma>, parent_attribute: &Attrs)
#parse(matches.#value_of(#name))
},

Ty::Other if flag => quote_spanned! { ty.span()=>
#parse(matches.is_present(#name))
},

Ty::Other => quote_spanned! { ty.span()=>
matches.#value_of(#name)
.map(#parse)
Expand Down
31 changes: 31 additions & 0 deletions tests/flags.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,37 @@ fn multiple_flag() {
.is_err());
}

fn parse_from_flag(b: bool) -> std::sync::atomic::AtomicBool {
std::sync::atomic::AtomicBool::new(b)
}

#[test]
fn non_bool_flags() {
#[derive(StructOpt, Debug)]
struct Opt {
#[structopt(short = "a", long = "alice", parse(from_flag = parse_from_flag))]
alice: std::sync::atomic::AtomicBool,
#[structopt(short = "b", long = "bob", parse(from_flag))]
bob: std::sync::atomic::AtomicBool,
}

let falsey = Opt::from_clap(&Opt::clap().get_matches_from(&["test"]));
assert!(!falsey.alice.load(std::sync::atomic::Ordering::Relaxed));
assert!(!falsey.bob.load(std::sync::atomic::Ordering::Relaxed));

let alice = Opt::from_clap(&Opt::clap().get_matches_from(&["test", "-a"]));
assert!(alice.alice.load(std::sync::atomic::Ordering::Relaxed));
assert!(!alice.bob.load(std::sync::atomic::Ordering::Relaxed));

let bob = Opt::from_clap(&Opt::clap().get_matches_from(&["test", "-b"]));
assert!(!bob.alice.load(std::sync::atomic::Ordering::Relaxed));
assert!(bob.bob.load(std::sync::atomic::Ordering::Relaxed));

let both = Opt::from_clap(&Opt::clap().get_matches_from(&["test", "-b", "-a"]));
assert!(both.alice.load(std::sync::atomic::Ordering::Relaxed));
assert!(both.bob.load(std::sync::atomic::Ordering::Relaxed));
}

#[test]
fn combined_flags() {
#[derive(StructOpt, PartialEq, Debug)]
Expand Down