Skip to content

Commit

Permalink
fix(derive): Allow opting in to the original deprecations
Browse files Browse the repository at this point in the history
  • Loading branch information
epage committed Jun 14, 2022
1 parent ae81b09 commit 11fe3ce
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 6 deletions.
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,8 @@ color = ["atty", "termcolor"]
suggestions = ["strsim"]

# Optional
deprecated = [] # Guided experience to prepare for next breaking release (at different stages of development, this may become default)
# note: this will always enable clap_derive, change this to `clap_derive?/unstable-v4` when MSRV is bigger than 1.60
deprecated = ["clap_derive/deprecated"] # Guided experience to prepare for next breaking release (at different stages of development, this may become default)
derive = ["clap_derive", "once_cell"]
cargo = ["once_cell"] # Disable if you're not using Cargo, enables Cargo-env-var-dependent macros
wrap_help = ["terminal_size", "textwrap/terminal_size"]
Expand Down
4 changes: 3 additions & 1 deletion clap_derive/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,6 @@ proc-macro-error = "1"
[features]
default = []
debug = []
unstable-v4 = []
unstable-v4 = ["deprecated"]
deprecated = []
raw-deprecated = ["deprecated"]
18 changes: 16 additions & 2 deletions clap_derive/src/derives/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ pub fn gen_from_arg_matches_for_struct(

let constructor = gen_constructor(fields, &attrs);
let updater = gen_updater(fields, &attrs, true);
let raw_deprecated = raw_deprecated();

let (impl_generics, ty_generics, where_clause) = generics.split_for_impl();

Expand All @@ -136,7 +137,7 @@ pub fn gen_from_arg_matches_for_struct(
}

fn from_arg_matches_mut(__clap_arg_matches: &mut clap::ArgMatches) -> ::std::result::Result<Self, clap::Error> {
#[allow(deprecated)] // Assuming any deprecation in here will be related to a deprecation in `Args`
#raw_deprecated
let v = #struct_name #constructor;
::std::result::Result::Ok(v)
}
Expand All @@ -146,7 +147,7 @@ pub fn gen_from_arg_matches_for_struct(
}

fn update_from_arg_matches_mut(&mut self, __clap_arg_matches: &mut clap::ArgMatches) -> ::std::result::Result<(), clap::Error> {
#[allow(deprecated)] // Assuming any deprecation in here will be related to a deprecation in `Args`
#raw_deprecated
#updater
::std::result::Result::Ok(())
}
Expand Down Expand Up @@ -737,3 +738,16 @@ fn gen_parsers(
quote_spanned!(field.span()=> #field_name: #field_value )
}
}

#[cfg(feature = "raw-deprecated")]
pub fn raw_deprecated() -> TokenStream {
quote! {}
}

#[cfg(not(feature = "raw-deprecated"))]
pub fn raw_deprecated() -> TokenStream {
quote! {
#![allow(deprecated)] // Assuming any deprecation in here will be related to a deprecation in `Args`

}
}
6 changes: 4 additions & 2 deletions clap_derive/src/derives/subcommand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -540,9 +540,10 @@ fn gen_from_arg_matches(
},
};

let raw_deprecated = args::raw_deprecated();
quote! {
fn from_arg_matches_mut(__clap_arg_matches: &mut clap::ArgMatches) -> ::std::result::Result<Self, clap::Error> {
#[allow(deprecated)] // Assuming any deprecation in here will be related to a deprecation in `Subcommand`
#raw_deprecated

#( #child_subcommands )else*

Expand Down Expand Up @@ -654,12 +655,13 @@ fn gen_update_from_arg_matches(
}
});

let raw_deprecated = args::raw_deprecated();
quote! {
fn update_from_arg_matches_mut<'b>(
&mut self,
__clap_arg_matches: &mut clap::ArgMatches,
) -> ::std::result::Result<(), clap::Error> {
#[allow(deprecated)] // Assuming any deprecation in here will be related to a deprecation in `Subcommand`
#raw_deprecated

if let Some(__clap_name) = __clap_arg_matches.subcommand_name() {
match self {
Expand Down

0 comments on commit 11fe3ce

Please sign in to comment.