2020-01-07 10:17:23 +00:00
|
|
|
use std::iter::FromIterator;
|
|
|
|
|
|
|
|
use proc_macro_error::{abort, ResultExt};
|
2019-12-21 12:20:57 +00:00
|
|
|
use quote::ToTokens;
|
2020-01-07 10:17:23 +00:00
|
|
|
use syn::{
|
|
|
|
self, parenthesized,
|
|
|
|
parse::{Parse, ParseBuffer, ParseStream},
|
|
|
|
punctuated::Punctuated,
|
|
|
|
Attribute, Expr, ExprLit, Ident, Lit, LitBool, LitStr, Token,
|
|
|
|
};
|
|
|
|
|
2020-01-31 11:05:12 +00:00
|
|
|
#[allow(clippy::large_enum_variant)]
|
2020-01-07 10:17:23 +00:00
|
|
|
pub enum ClapAttr {
|
|
|
|
// single-identifier attributes
|
|
|
|
Short(Ident),
|
|
|
|
Long(Ident),
|
2019-12-06 09:48:32 +00:00
|
|
|
Env(Ident),
|
2020-01-07 10:17:23 +00:00
|
|
|
Flatten(Ident),
|
2020-04-22 07:25:41 +00:00
|
|
|
ArgEnum(Ident),
|
2020-07-19 20:31:59 +00:00
|
|
|
FromGlobal(Ident),
|
2020-01-07 10:17:23 +00:00
|
|
|
Subcommand(Ident),
|
2019-12-21 10:39:56 +00:00
|
|
|
VerbatimDocComment(Ident),
|
2020-04-30 17:20:21 +00:00
|
|
|
ExternalSubcommand(Ident),
|
2020-01-07 10:17:23 +00:00
|
|
|
|
|
|
|
// ident [= "string literal"]
|
|
|
|
About(Ident, Option<LitStr>),
|
|
|
|
Author(Ident, Option<LitStr>),
|
2020-02-05 11:12:39 +00:00
|
|
|
Version(Ident, Option<LitStr>),
|
2020-01-07 10:17:23 +00:00
|
|
|
|
|
|
|
// ident = "string literal"
|
2019-12-06 09:48:32 +00:00
|
|
|
RenameAllEnv(Ident, LitStr),
|
2020-01-07 10:17:23 +00:00
|
|
|
RenameAll(Ident, LitStr),
|
|
|
|
NameLitStr(Ident, LitStr),
|
|
|
|
|
|
|
|
// parse(parser_kind [= parser_func])
|
|
|
|
Parse(Ident, ParserSpec),
|
|
|
|
|
|
|
|
// ident [= arbitrary_expr]
|
|
|
|
Skip(Ident, Option<Expr>),
|
|
|
|
|
|
|
|
// ident = arbitrary_expr
|
|
|
|
NameExpr(Ident, Expr),
|
2021-07-28 13:53:32 +00:00
|
|
|
DefaultValueT(Ident, Option<Expr>),
|
2020-01-07 10:17:23 +00:00
|
|
|
|
|
|
|
// ident(arbitrary_expr,*)
|
|
|
|
MethodCall(Ident, Vec<Expr>),
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Parse for ClapAttr {
|
|
|
|
fn parse(input: ParseStream) -> syn::Result<Self> {
|
|
|
|
use self::ClapAttr::*;
|
|
|
|
|
|
|
|
let name: Ident = input.parse()?;
|
|
|
|
let name_str = name.to_string();
|
|
|
|
|
|
|
|
if input.peek(Token![=]) {
|
|
|
|
// `name = value` attributes.
|
|
|
|
let assign_token = input.parse::<Token![=]>()?; // skip '='
|
|
|
|
|
|
|
|
if input.peek(LitStr) {
|
|
|
|
let lit: LitStr = input.parse()?;
|
|
|
|
let lit_str = lit.value();
|
|
|
|
|
|
|
|
let check_empty_lit = |s| {
|
|
|
|
if lit_str.is_empty() {
|
|
|
|
abort!(
|
2020-02-13 18:44:43 +00:00
|
|
|
lit,
|
2020-01-07 10:17:23 +00:00
|
|
|
"`#[clap({} = \"\")` is deprecated, \
|
|
|
|
now it's default behavior",
|
|
|
|
s
|
|
|
|
);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-01-31 11:05:12 +00:00
|
|
|
match &*name_str {
|
2020-01-07 10:17:23 +00:00
|
|
|
"rename_all" => Ok(RenameAll(name, lit)),
|
2019-12-06 09:48:32 +00:00
|
|
|
"rename_all_env" => Ok(RenameAllEnv(name, lit)),
|
2020-01-07 10:17:23 +00:00
|
|
|
|
|
|
|
"version" => {
|
|
|
|
check_empty_lit("version");
|
2020-02-05 11:12:39 +00:00
|
|
|
Ok(Version(name, Some(lit)))
|
2020-01-07 10:17:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
"author" => {
|
|
|
|
check_empty_lit("author");
|
|
|
|
Ok(Author(name, Some(lit)))
|
|
|
|
}
|
|
|
|
|
|
|
|
"about" => {
|
|
|
|
check_empty_lit("about");
|
|
|
|
Ok(About(name, Some(lit)))
|
|
|
|
}
|
|
|
|
|
|
|
|
"skip" => {
|
|
|
|
let expr = ExprLit {
|
|
|
|
attrs: vec![],
|
|
|
|
lit: Lit::Str(lit),
|
|
|
|
};
|
|
|
|
let expr = Expr::Lit(expr);
|
|
|
|
Ok(Skip(name, Some(expr)))
|
|
|
|
}
|
|
|
|
|
|
|
|
_ => Ok(NameLitStr(name, lit)),
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
match input.parse::<Expr>() {
|
2021-07-28 13:53:32 +00:00
|
|
|
Ok(expr) => match &*name_str {
|
|
|
|
"skip" => Ok(Skip(name, Some(expr))),
|
|
|
|
"default_value_t" => Ok(DefaultValueT(name, Some(expr))),
|
|
|
|
_ => Ok(NameExpr(name, expr)),
|
|
|
|
},
|
2020-01-07 10:17:23 +00:00
|
|
|
|
|
|
|
Err(_) => abort! {
|
2020-02-13 18:44:43 +00:00
|
|
|
assign_token,
|
2020-01-07 10:17:23 +00:00
|
|
|
"expected `string literal` or `expression` after `=`"
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if input.peek(syn::token::Paren) {
|
|
|
|
// `name(...)` attributes.
|
|
|
|
let nested;
|
|
|
|
parenthesized!(nested in input);
|
|
|
|
|
|
|
|
match name_str.as_ref() {
|
|
|
|
"parse" => {
|
|
|
|
let parser_specs: Punctuated<ParserSpec, Token![,]> =
|
|
|
|
nested.parse_terminated(ParserSpec::parse)?;
|
|
|
|
|
|
|
|
if parser_specs.len() == 1 {
|
|
|
|
Ok(Parse(name, parser_specs[0].clone()))
|
|
|
|
} else {
|
2020-02-13 18:44:43 +00:00
|
|
|
abort!(name, "parse must have exactly one argument")
|
2020-01-07 10:17:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
"raw" => match nested.parse::<LitBool>() {
|
|
|
|
Ok(bool_token) => {
|
|
|
|
let expr = ExprLit {
|
|
|
|
attrs: vec![],
|
|
|
|
lit: Lit::Bool(bool_token),
|
|
|
|
};
|
|
|
|
let expr = Expr::Lit(expr);
|
|
|
|
Ok(MethodCall(name, vec![expr]))
|
|
|
|
}
|
|
|
|
|
|
|
|
Err(_) => {
|
2020-02-13 18:44:43 +00:00
|
|
|
abort!(name,
|
2020-01-07 10:17:23 +00:00
|
|
|
"`#[clap(raw(...))` attributes are removed, \
|
|
|
|
they are replaced with raw methods";
|
|
|
|
help = "if you meant to call `clap::Arg::raw()` method \
|
|
|
|
you should use bool literal, like `raw(true)` or `raw(false)`";
|
|
|
|
note = raw_method_suggestion(nested);
|
|
|
|
);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
_ => {
|
|
|
|
let method_args: Punctuated<_, Token![,]> =
|
|
|
|
nested.parse_terminated(Expr::parse)?;
|
|
|
|
Ok(MethodCall(name, Vec::from_iter(method_args)))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Attributes represented with a sole identifier.
|
|
|
|
match name_str.as_ref() {
|
|
|
|
"long" => Ok(Long(name)),
|
|
|
|
"short" => Ok(Short(name)),
|
2019-12-06 09:48:32 +00:00
|
|
|
"env" => Ok(Env(name)),
|
2020-01-07 10:17:23 +00:00
|
|
|
"flatten" => Ok(Flatten(name)),
|
2020-04-22 07:25:41 +00:00
|
|
|
"arg_enum" => Ok(ArgEnum(name)),
|
2020-07-19 20:31:59 +00:00
|
|
|
"from_global" => Ok(FromGlobal(name)),
|
2020-01-07 10:17:23 +00:00
|
|
|
"subcommand" => Ok(Subcommand(name)),
|
2020-04-30 17:20:21 +00:00
|
|
|
"external_subcommand" => Ok(ExternalSubcommand(name)),
|
2019-12-21 10:39:56 +00:00
|
|
|
"verbatim_doc_comment" => Ok(VerbatimDocComment(name)),
|
2020-01-07 10:17:23 +00:00
|
|
|
|
2021-07-28 13:53:32 +00:00
|
|
|
"default_value" => {
|
|
|
|
abort!(name,
|
|
|
|
"`#[clap(default_value)` attribute (without a value) has been replaced by `#[clap(default_value_t)]`.";
|
|
|
|
help = "Change the attribute to `#[clap(default_value_t)]`";
|
|
|
|
)
|
|
|
|
}
|
|
|
|
"default_value_t" => Ok(DefaultValueT(name, None)),
|
2020-01-07 10:17:23 +00:00
|
|
|
"about" => (Ok(About(name, None))),
|
|
|
|
"author" => (Ok(Author(name, None))),
|
2020-02-05 11:12:39 +00:00
|
|
|
"version" => Ok(Version(name, None)),
|
2020-01-07 10:17:23 +00:00
|
|
|
|
|
|
|
"skip" => Ok(Skip(name, None)),
|
|
|
|
|
2020-02-13 18:44:43 +00:00
|
|
|
_ => abort!(name, "unexpected attribute: {}", name_str),
|
2020-01-07 10:17:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Clone)]
|
|
|
|
pub struct ParserSpec {
|
|
|
|
pub kind: Ident,
|
|
|
|
pub eq_token: Option<Token![=]>,
|
|
|
|
pub parse_func: Option<Expr>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Parse for ParserSpec {
|
|
|
|
fn parse(input: ParseStream<'_>) -> syn::Result<Self> {
|
|
|
|
let kind = input
|
|
|
|
.parse()
|
|
|
|
.map_err(|_| input.error("parser specification must start with identifier"))?;
|
|
|
|
let eq_token = input.parse()?;
|
|
|
|
let parse_func = match eq_token {
|
|
|
|
None => None,
|
|
|
|
Some(_) => Some(input.parse()?),
|
|
|
|
};
|
|
|
|
Ok(ParserSpec {
|
|
|
|
kind,
|
|
|
|
eq_token,
|
|
|
|
parse_func,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn raw_method_suggestion(ts: ParseBuffer) -> String {
|
2020-01-20 22:03:55 +00:00
|
|
|
let do_parse = move || -> Result<(Ident, Punctuated<Expr, Token![,]>), syn::Error> {
|
2020-01-07 10:17:23 +00:00
|
|
|
let name = ts.parse()?;
|
|
|
|
let _eq: Token![=] = ts.parse()?;
|
|
|
|
let val: LitStr = ts.parse()?;
|
2020-01-20 22:03:55 +00:00
|
|
|
let exprs = val.parse_with(Punctuated::<Expr, Token![,]>::parse_terminated)?;
|
|
|
|
Ok((name, exprs))
|
2020-01-07 10:17:23 +00:00
|
|
|
};
|
2019-12-21 12:20:57 +00:00
|
|
|
|
|
|
|
fn to_string<T: ToTokens>(val: &T) -> String {
|
|
|
|
val.to_token_stream()
|
|
|
|
.to_string()
|
|
|
|
.replace(" ", "")
|
|
|
|
.replace(",", ", ")
|
|
|
|
}
|
|
|
|
|
2020-01-20 22:03:55 +00:00
|
|
|
if let Ok((name, exprs)) = do_parse() {
|
2019-12-21 12:20:57 +00:00
|
|
|
let suggestion = if exprs.len() == 1 {
|
|
|
|
let val = to_string(&exprs[0]);
|
|
|
|
format!(" = {}", val)
|
|
|
|
} else {
|
|
|
|
let val = exprs
|
|
|
|
.into_iter()
|
|
|
|
.map(|expr| to_string(&expr))
|
|
|
|
.collect::<Vec<_>>()
|
|
|
|
.join(", ");
|
|
|
|
|
|
|
|
format!("({})", val)
|
|
|
|
};
|
|
|
|
|
2020-01-07 10:17:23 +00:00
|
|
|
format!(
|
|
|
|
"if you need to call `clap::Arg/App::{}` method you \
|
2019-12-21 12:20:57 +00:00
|
|
|
can do it like this: #[clap({}{})]",
|
|
|
|
name, name, suggestion
|
2020-01-07 10:17:23 +00:00
|
|
|
)
|
|
|
|
} else {
|
|
|
|
"if you need to call some method from `clap::Arg/App` \
|
|
|
|
you should use raw method, see \
|
|
|
|
https://docs.rs/structopt/0.3/structopt/#raw-methods"
|
|
|
|
.into()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn parse_clap_attributes(all_attrs: &[Attribute]) -> Vec<ClapAttr> {
|
|
|
|
all_attrs
|
|
|
|
.iter()
|
|
|
|
.filter(|attr| attr.path.is_ident("clap"))
|
|
|
|
.flat_map(|attr| {
|
2020-01-20 21:19:37 +00:00
|
|
|
attr.parse_args_with(Punctuated::<ClapAttr, Token![,]>::parse_terminated)
|
|
|
|
.unwrap_or_abort()
|
2020-01-07 10:17:23 +00:00
|
|
|
})
|
|
|
|
.collect()
|
|
|
|
}
|