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

Use proper span when generating matches token #398

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
25 changes: 13 additions & 12 deletions structopt-derive/src/lib.rs
Expand Up @@ -248,6 +248,7 @@ fn gen_constructor(fields: &Punctuated<Field, Comma>, parent_attribute: &Attrs)
);
let field_name = field.ident.as_ref().unwrap();
let kind = attrs.kind();
let matches = quote! { matches };
match &*kind {
Kind::ExternalSubcommand => abort!(
kind.span(),
Expand All @@ -265,13 +266,13 @@ fn gen_constructor(fields: &Punctuated<Field, Comma>, parent_attribute: &Attrs)
};
quote_spanned! { kind.span()=>
#field_name: <#subcmd_type as ::structopt::StructOptInternal>::from_subcommand(
matches.subcommand())
#matches.subcommand())
#unwrapper
}
}

Kind::Flatten => quote_spanned! { kind.span()=>
#field_name: ::structopt::StructOpt::from_clap(matches)
#field_name: ::structopt::StructOpt::from_clap(#matches)
},

Kind::Skip(val) => match val {
Expand Down Expand Up @@ -318,45 +319,45 @@ fn gen_constructor(fields: &Punctuated<Field, Comma>, parent_attribute: &Attrs)
let occurrences = *attrs.parser().kind == ParserKind::FromOccurrences;
let name = attrs.cased_name();
let field_value = match **ty {
Ty::Bool => quote_spanned!(ty.span()=> matches.is_present(#name)),
Ty::Bool => quote_spanned!(ty.span()=> #matches.is_present(#name)),

Ty::Option => quote_spanned! { ty.span()=>
matches.#value_of(#name)
#matches.#value_of(#name)
.map(#parse)
},

Ty::OptionOption => quote_spanned! { ty.span()=>
if matches.is_present(#name) {
Some(matches.#value_of(#name).map(#parse))
if #matches.is_present(#name) {
Some(#matches.#value_of(#name).map(#parse))
} else {
None
}
},

Ty::OptionVec => quote_spanned! { ty.span()=>
if matches.is_present(#name) {
Some(matches.#values_of(#name)
if #matches.is_present(#name) {
Some(#matches.#values_of(#name)
.map_or_else(Vec::new, |v| v.map(#parse).collect()))
} else {
None
}
},

Ty::Vec => quote_spanned! { ty.span()=>
matches.#values_of(#name)
#matches.#values_of(#name)
.map_or_else(Vec::new, |v| v.map(#parse).collect())
},

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

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

Ty::Other => quote_spanned! { ty.span()=>
matches.#value_of(#name)
#matches.#value_of(#name)
.map(#parse)
.unwrap()
},
Expand Down