From 5881bd02cae000164a4df42be099811de44a28b7 Mon Sep 17 00:00:00 2001 From: Ed Page Date: Tue, 14 Jun 2022 14:29:26 -0500 Subject: [PATCH] fix(derive): Provide derive-friendly deprecation messages This is a step towards #3822. I'd say this fixes it but I'd want some user acceptance before doing so. --- clap_derive/src/attrs.rs | 4 +++ clap_derive/src/derives/args.rs | 43 +++++++++++++++++++++++++++++++++ 2 files changed, 47 insertions(+) diff --git a/clap_derive/src/attrs.rs b/clap_derive/src/attrs.rs index 39f1735557a..fb35d31f2d1 100644 --- a/clap_derive/src/attrs.rs +++ b/clap_derive/src/attrs.rs @@ -825,6 +825,10 @@ impl Attrs { self.value_parser.is_some() || self.action.is_some() } + pub fn explicit_parser(&self) -> bool { + self.parser.is_some() + } + pub fn parser(&self, field_type: &Type) -> Sp { self.parser .clone() diff --git a/clap_derive/src/derives/args.rs b/clap_derive/src/derives/args.rs index 23a2260e644..4195a081082 100644 --- a/clap_derive/src/derives/args.rs +++ b/clap_derive/src/derives/args.rs @@ -275,6 +275,45 @@ pub fn gen_augment( quote!() } }; + let parse_deprecation = match *parser.kind { + _ if !attrs.explicit_parser() || cfg!(not(feature = "deprecated")) => quote!(), + ParserKind::FromStr => quote_spanned! { func.span()=> + #[deprecated(since = "3.2.0", note = "Replaced with `#[clap(value_parser = ...)]`")] + fn parse_from_str() { + } + parse_from_str(); + }, + ParserKind::TryFromStr => quote_spanned! { func.span()=> + #[deprecated(since = "3.2.0", note = "Replaced with `#[clap(value_parser = ...)]`")] + fn parse_try_from_str() { + } + parse_try_from_str(); + }, + ParserKind::FromOsStr => quote_spanned! { func.span()=> + #[deprecated(since = "3.2.0", note = "Replaced with `#[clap(value_parser)]` for `PathBuf` or `#[clap(value_parser = ...)]` with a custom `TypedValueParser`")] + fn parse_from_os_str() { + } + parse_from_os_str(); + }, + ParserKind::TryFromOsStr => quote_spanned! { func.span()=> + #[deprecated(since = "3.2.0", note = "Replaced with `#[clap(value_parser = ...)]` with a custom `TypedValueParser`")] + fn parse_try_from_os_str() { + } + parse_try_from_os_str(); + }, + ParserKind::FromFlag => quote_spanned! { func.span()=> + #[deprecated(since = "3.2.0", note = "Replaced with `#[clap(action = ArgAction::SetTrue)]`")] + fn parse_from_flag() { + } + parse_from_flag(); + }, + ParserKind::FromOccurrences => quote_spanned! { func.span()=> + #[deprecated(since = "3.2.0", note = "Replaced with `#[clap(action = ArgAction::Count)]` with a field type of `u8`")] + fn parse_from_occurrences() { + } + parse_from_occurrences(); + }, + }; let value_name = attrs.value_name(); let possible_values = if attrs.is_enum() && !attrs.ignore_parser() { @@ -408,8 +447,12 @@ pub fn gen_augment( Some(quote_spanned! { field.span()=> let #app_var = #app_var.arg({ + #parse_deprecation + + #[allow(deprecated)] let arg = clap::Arg::new(#id) #implicit_methods; + let arg = arg #explicit_methods; arg