add default

This commit is contained in:
Aiden McClelland
2021-07-29 13:52:51 -06:00
parent 1e9ded9a31
commit d1594997fd
8 changed files with 52 additions and 8 deletions

View File

@@ -5,6 +5,8 @@ name = "rpc-toolkit-macro-internals"
version = "0.1.0"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[features]
cli-cookies = []
[dependencies]
proc-macro2 = "1.0.26"

View File

@@ -225,7 +225,11 @@ fn build_app(name: LitStr, opt: &mut Options, params: &mut [ParamType]) -> Token
modifications.extend(quote_spanned! { ty_span =>
arg = arg.takes_value(true);
});
if p.path.segments.last().unwrap().ident == "Option" {
if let Some(default) = &arg.default {
modifications.extend(quote_spanned! { ty_span =>
arg = arg.default_value(#default);
});
} else if p.path.segments.last().unwrap().ident == "Option" {
arg.optional = true;
modifications.extend(quote_spanned! { ty_span =>
arg = arg.required(false);

View File

@@ -81,6 +81,7 @@ pub struct ArgOptions {
short: Option<LitStr>,
long: Option<LitStr>,
parse: Option<Path>,
default: Option<LitStr>,
count: Option<Path>,
multiple: Option<Path>,
stdin: Option<Path>,

View File

@@ -394,6 +394,7 @@ pub fn parse_arg_attr(attr: Attribute, arg: PatType) -> Result<ArgOptions> {
short: None,
long: None,
parse: None,
default: None,
count: None,
multiple: None,
stdin: None,
@@ -627,6 +628,28 @@ pub fn parse_arg_attr(attr: Attribute, arg: PatType) -> Result<ArgOptions> {
NestedMeta::Meta(Meta::Path(p)) if p.is_ident("long") => {
return Err(Error::new(p.span(), "`long` must be assigned to"));
}
NestedMeta::Meta(Meta::NameValue(nv)) if nv.path.is_ident("default") => {
if let Lit::Str(default) = nv.lit {
if opt.default.is_some() {
return Err(Error::new(
default.span(),
"duplicate argument `default`",
));
}
opt.default = Some(default);
} else {
return Err(Error::new(nv.lit.span(), "`default` must be a string"));
}
}
NestedMeta::Meta(Meta::List(list)) if list.path.is_ident("default") => {
return Err(Error::new(
list.path.span(),
"`default` does not take any arguments",
));
}
NestedMeta::Meta(Meta::Path(p)) if p.is_ident("default") => {
return Err(Error::new(p.span(), "`default` must be assigned to"));
}
_ => {
return Err(Error::new(arg.span(), "unknown argument"));
}