mirror of
https://github.com/clap-rs/clap
synced 2024-12-13 14:22:34 +00:00
fix(derive): Provide deprecations for bald action/value_parser
These will be going away, so we should tell the user. This is mostly a test bed for our new deprecation plumbing.
This commit is contained in:
parent
edce5c2119
commit
7eaa226526
4 changed files with 109 additions and 8 deletions
|
@ -291,9 +291,16 @@ pub fn gen_augment(
|
|||
|
||||
let id = item.id();
|
||||
let explicit_methods = item.field_methods(true);
|
||||
let deprecations = if !override_required {
|
||||
item.deprecations()
|
||||
} else {
|
||||
quote!()
|
||||
};
|
||||
|
||||
Some(quote_spanned! { field.span()=>
|
||||
let #app_var = #app_var.arg({
|
||||
#deprecations
|
||||
|
||||
#[allow(deprecated)]
|
||||
let arg = clap::Arg::new(#id)
|
||||
#implicit_methods;
|
||||
|
@ -307,9 +314,15 @@ pub fn gen_augment(
|
|||
}
|
||||
});
|
||||
|
||||
let deprecations = if !override_required {
|
||||
parent_item.deprecations()
|
||||
} else {
|
||||
quote!()
|
||||
};
|
||||
let initial_app_methods = parent_item.initial_top_level_methods();
|
||||
let final_app_methods = parent_item.final_top_level_methods();
|
||||
quote! {{
|
||||
#deprecations
|
||||
let #app_var = #app_var #initial_app_methods;
|
||||
#( #args )*
|
||||
#subcmd
|
||||
|
|
|
@ -143,9 +143,15 @@ fn gen_augment(
|
|||
or `Vec<OsString>`."
|
||||
),
|
||||
};
|
||||
let deprecations = if !override_required {
|
||||
item.deprecations()
|
||||
} else {
|
||||
quote!()
|
||||
};
|
||||
let subcommand = match subty_if_name(ty, "Vec") {
|
||||
Some(subty) => {
|
||||
quote_spanned! { kind.span()=>
|
||||
#deprecations
|
||||
let #app_var = #app_var.external_subcommand_value_parser(clap::value_parser!(#subty));
|
||||
}
|
||||
}
|
||||
|
@ -162,11 +168,17 @@ fn gen_augment(
|
|||
Kind::Flatten => match variant.fields {
|
||||
Unnamed(FieldsUnnamed { ref unnamed, .. }) if unnamed.len() == 1 => {
|
||||
let ty = &unnamed[0];
|
||||
let deprecations = if !override_required {
|
||||
item.deprecations()
|
||||
} else {
|
||||
quote!()
|
||||
};
|
||||
let old_heading_var = format_ident!("__clap_old_heading");
|
||||
let next_help_heading = item.next_help_heading();
|
||||
let next_display_order = item.next_display_order();
|
||||
let subcommand = if override_required {
|
||||
quote! {
|
||||
#deprecations
|
||||
let #old_heading_var = #app_var.get_next_help_heading().map(|s| clap::builder::Str::from(s.to_owned()));
|
||||
let #app_var = #app_var #next_help_heading #next_display_order;
|
||||
let #app_var = <#ty as clap::Subcommand>::augment_subcommands_for_update(#app_var);
|
||||
|
@ -174,6 +186,7 @@ fn gen_augment(
|
|||
}
|
||||
} else {
|
||||
quote! {
|
||||
#deprecations
|
||||
let #old_heading_var = #app_var.get_next_help_heading().map(|s| clap::builder::Str::from(s.to_owned()));
|
||||
let #app_var = #app_var #next_help_heading #next_display_order;
|
||||
let #app_var = <#ty as clap::Subcommand>::augment_subcommands(#app_var);
|
||||
|
@ -217,10 +230,16 @@ fn gen_augment(
|
|||
};
|
||||
|
||||
let name = item.cased_name();
|
||||
let deprecations = if !override_required {
|
||||
item.deprecations()
|
||||
} else {
|
||||
quote!()
|
||||
};
|
||||
let initial_app_methods = item.initial_top_level_methods();
|
||||
let final_from_attrs = item.final_top_level_methods();
|
||||
let subcommand = quote! {
|
||||
let #app_var = #app_var.subcommand({
|
||||
#deprecations;
|
||||
let #subcommand_var = clap::Command::new(#name);
|
||||
let #subcommand_var = #subcommand_var #initial_app_methods;
|
||||
let #subcommand_var = #arg_block;
|
||||
|
@ -286,9 +305,15 @@ fn gen_augment(
|
|||
}
|
||||
};
|
||||
|
||||
let deprecations = if !override_required {
|
||||
item.deprecations()
|
||||
} else {
|
||||
quote!()
|
||||
};
|
||||
let name = item.cased_name();
|
||||
let subcommand = quote! {
|
||||
let #app_var = #app_var.subcommand({
|
||||
#deprecations
|
||||
let #subcommand_var = clap::Command::new(#name);
|
||||
#sub_augment
|
||||
});
|
||||
|
@ -299,12 +324,18 @@ fn gen_augment(
|
|||
})
|
||||
.collect();
|
||||
|
||||
let deprecations = if !override_required {
|
||||
parent_item.deprecations()
|
||||
} else {
|
||||
quote!()
|
||||
};
|
||||
let initial_app_methods = parent_item.initial_top_level_methods();
|
||||
let final_app_methods = parent_item.final_top_level_methods();
|
||||
quote! {
|
||||
let #app_var = #app_var #initial_app_methods;
|
||||
#( #subcommands )*;
|
||||
#app_var #final_app_methods
|
||||
#deprecations;
|
||||
let #app_var = #app_var #initial_app_methods;
|
||||
#( #subcommands )*;
|
||||
#app_var #final_app_methods
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -41,10 +41,10 @@ pub fn derive_value_enum(input: &DeriveInput) -> TokenStream {
|
|||
}
|
||||
}
|
||||
|
||||
pub fn gen_for_enum(_item: &Item, item_name: &Ident, variants: &[(&Variant, Item)]) -> TokenStream {
|
||||
pub fn gen_for_enum(item: &Item, item_name: &Ident, variants: &[(&Variant, Item)]) -> TokenStream {
|
||||
let lits = lits(variants);
|
||||
let value_variants = gen_value_variants(&lits);
|
||||
let to_possible_value = gen_to_possible_value(&lits);
|
||||
let to_possible_value = gen_to_possible_value(item, &lits);
|
||||
|
||||
quote! {
|
||||
#[allow(dead_code, unreachable_code, unused_variables, unused_braces)]
|
||||
|
@ -78,12 +78,14 @@ fn lits(variants: &[(&Variant, Item)]) -> Vec<(TokenStream, Ident)> {
|
|||
abort!(variant.span(), "`#[derive(ValueEnum)]` only supports unit variants. Non-unit variants must be skipped");
|
||||
}
|
||||
let fields = item.field_methods(false);
|
||||
let deprecations = item.deprecations();
|
||||
let name = item.cased_name();
|
||||
Some((
|
||||
quote_spanned! { variant.span()=>
|
||||
quote_spanned! { variant.span()=> {
|
||||
#deprecations
|
||||
clap::builder::PossibleValue::new(#name)
|
||||
#fields
|
||||
},
|
||||
}},
|
||||
variant.ident.clone(),
|
||||
))
|
||||
}
|
||||
|
@ -101,11 +103,14 @@ fn gen_value_variants(lits: &[(TokenStream, Ident)]) -> TokenStream {
|
|||
}
|
||||
}
|
||||
|
||||
fn gen_to_possible_value(lits: &[(TokenStream, Ident)]) -> TokenStream {
|
||||
fn gen_to_possible_value(item: &Item, lits: &[(TokenStream, Ident)]) -> TokenStream {
|
||||
let (lit, variant): (Vec<TokenStream>, Vec<Ident>) = lits.iter().cloned().unzip();
|
||||
|
||||
let deprecations = item.deprecations();
|
||||
|
||||
quote! {
|
||||
fn to_possible_value<'a>(&self) -> ::std::option::Option<clap::builder::PossibleValue> {
|
||||
#deprecations
|
||||
match self {
|
||||
#(Self::#variant => Some(#lit),)*
|
||||
_ => None
|
||||
|
|
|
@ -41,6 +41,7 @@ pub struct Item {
|
|||
ty: Option<Type>,
|
||||
doc_comment: Vec<Method>,
|
||||
methods: Vec<Method>,
|
||||
deprecations: Vec<Deprecation>,
|
||||
value_parser: Option<ValueParser>,
|
||||
action: Option<Action>,
|
||||
verbatim_doc_comment: bool,
|
||||
|
@ -409,6 +410,7 @@ impl Item {
|
|||
env_casing,
|
||||
doc_comment: vec![],
|
||||
methods: vec![],
|
||||
deprecations: vec![],
|
||||
value_parser: None,
|
||||
action: None,
|
||||
verbatim_doc_comment: false,
|
||||
|
@ -512,11 +514,23 @@ impl Item {
|
|||
|
||||
#[cfg(not(feature = "unstable-v5"))]
|
||||
Some(MagicAttrName::ValueParser) if attr.value.is_none() => {
|
||||
self.deprecations.push(Deprecation {
|
||||
span: attr.name.span(),
|
||||
id: "bare_value_parser",
|
||||
version: "4.0.0",
|
||||
description: "`#[clap(value_parser)]` is now the default and is no longer needed`".to_owned(),
|
||||
});
|
||||
self.value_parser = Some(ValueParser::Implicit(attr.name.clone()));
|
||||
}
|
||||
|
||||
#[cfg(not(feature = "unstable-v5"))]
|
||||
Some(MagicAttrName::Action) if attr.value.is_none() => {
|
||||
self.deprecations.push(Deprecation {
|
||||
span: attr.name.span(),
|
||||
id: "bare_action",
|
||||
version: "4.0.0",
|
||||
description: "`#[clap(action)]` is now the default and is no longer needed`".to_owned(),
|
||||
});
|
||||
self.action = Some(Action::Implicit(attr.name.clone()));
|
||||
}
|
||||
|
||||
|
@ -903,6 +917,11 @@ impl Item {
|
|||
}
|
||||
}
|
||||
|
||||
pub fn deprecations(&self) -> proc_macro2::TokenStream {
|
||||
let deprecations = &self.deprecations;
|
||||
quote!( #(#deprecations)* )
|
||||
}
|
||||
|
||||
pub fn next_display_order(&self) -> TokenStream {
|
||||
let next_display_order = self.next_display_order.as_ref().into_iter();
|
||||
quote!( #(#next_display_order)* )
|
||||
|
@ -1155,6 +1174,39 @@ impl ToTokens for Method {
|
|||
}
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct Deprecation {
|
||||
pub span: Span,
|
||||
pub id: &'static str,
|
||||
pub version: &'static str,
|
||||
pub description: String,
|
||||
}
|
||||
|
||||
impl ToTokens for Deprecation {
|
||||
fn to_tokens(&self, ts: &mut proc_macro2::TokenStream) {
|
||||
let tokens = if cfg!(feature = "deprecated") {
|
||||
let Deprecation {
|
||||
span,
|
||||
id,
|
||||
version,
|
||||
description,
|
||||
} = self;
|
||||
let span = *span;
|
||||
let id = Ident::new(id, span);
|
||||
|
||||
quote_spanned!(span=> {
|
||||
#[deprecated(since = #version, note = #description)]
|
||||
fn #id() {}
|
||||
#id();
|
||||
})
|
||||
} else {
|
||||
quote!()
|
||||
};
|
||||
|
||||
tokens.to_tokens(ts);
|
||||
}
|
||||
}
|
||||
|
||||
/// replace all `:` with `, ` when not inside the `<>`
|
||||
///
|
||||
/// `"author1:author2:author3" => "author1, author2, author3"`
|
||||
|
|
Loading…
Reference in a new issue