Skip to content

Commit

Permalink
fix: Update derive->Command function name
Browse files Browse the repository at this point in the history
No good solution for transitioning the trate name, unfortnately, since
we can't mark `use`s as deprecated (we can, it just does nothing).

I got rid of the `into` prefix because that implies a `self` parameter
that doesn't exist.
  • Loading branch information
epage committed Feb 14, 2022
1 parent 7aa4566 commit c3fec1f
Show file tree
Hide file tree
Showing 12 changed files with 52 additions and 37 deletions.
2 changes: 1 addition & 1 deletion clap_complete/examples/value_hints_derive.rs
Expand Up @@ -65,7 +65,7 @@ fn main() {
let opt = Opt::parse();

if let Some(generator) = opt.generator {
let mut cmd = Opt::into_app();
let mut cmd = Opt::command();
eprintln!("Generating completion file for {:?}...", generator);
print_completions(generator, &mut cmd);
} else {
Expand Down
1 change: 1 addition & 0 deletions clap_derive/src/derives/into_app.rs
Expand Up @@ -56,6 +56,7 @@ pub fn gen_for_struct(
clippy::suspicious_else_formatting,
)]
#[deny(clippy::correctness)]
#[allow(deprecated)]
impl #impl_generics clap::IntoApp for #struct_name #ty_generics #where_clause {
fn into_app<'b>() -> clap::Command<'b> {
let #app_var = clap::Command::new(#name);
Expand Down
1 change: 1 addition & 0 deletions clap_derive/src/dummies.rs
Expand Up @@ -18,6 +18,7 @@ pub fn parser_enum(name: &Ident) {

pub fn into_app(name: &Ident) {
append_dummy(quote! {
#[allow(deprecated)]
impl clap::IntoApp for #name {
fn into_app<'b>() -> clap::Command<'b> {
unimplemented!()
Expand Down
6 changes: 3 additions & 3 deletions examples/tutorial_derive/04_04_custom.rs
Expand Up @@ -41,7 +41,7 @@ fn main() {
// See if --set-ver was used to set the version manually
let version = if let Some(ver) = cli.set_ver.as_deref() {
if cli.major || cli.minor || cli.patch {
let mut cmd = Cli::into_app();
let mut cmd = Cli::command();
cmd.error(
ErrorKind::ArgumentConflict,
"Can't do relative and absolute version change",
Expand All @@ -57,7 +57,7 @@ fn main() {
(false, true, false) => minor += 1,
(false, false, true) => patch += 1,
_ => {
let mut cmd = Cli::into_app();
let mut cmd = Cli::command();
cmd.error(
ErrorKind::ArgumentConflict,
"Cam only modify one version field",
Expand All @@ -80,7 +80,7 @@ fn main() {
// 'or' is preferred to 'or_else' here since `Option::as_deref` is 'const'
.or(cli.spec_in.as_deref())
.unwrap_or_else(|| {
let mut cmd = Cli::into_app();
let mut cmd = Cli::command();
cmd.error(
ErrorKind::MissingRequiredArgument,
"INPUT_FILE or --spec-in is required when using --config",
Expand Down
2 changes: 1 addition & 1 deletion examples/tutorial_derive/05_01_assert.rs
Expand Up @@ -17,5 +17,5 @@ fn main() {
#[test]
fn verify_app() {
use clap::IntoApp;
Cli::into_app().debug_assert()
Cli::command().debug_assert()
}
33 changes: 23 additions & 10 deletions src/derive.rs
Expand Up @@ -79,7 +79,7 @@ use std::ffi::OsString;
pub trait Parser: FromArgMatches + IntoApp + Sized {
/// Parse from `std::env::args_os()`, exit on error
fn parse() -> Self {
let matches = <Self as IntoApp>::into_app().get_matches();
let matches = <Self as IntoApp>::command().get_matches();
let res =
<Self as FromArgMatches>::from_arg_matches(&matches).map_err(format_error::<Self>);
match res {
Expand All @@ -94,7 +94,7 @@ pub trait Parser: FromArgMatches + IntoApp + Sized {

/// Parse from `std::env::args_os()`, return Err on error.
fn try_parse() -> Result<Self, Error> {
let matches = <Self as IntoApp>::into_app().try_get_matches()?;
let matches = <Self as IntoApp>::command().try_get_matches()?;
<Self as FromArgMatches>::from_arg_matches(&matches).map_err(format_error::<Self>)
}

Expand All @@ -104,7 +104,7 @@ pub trait Parser: FromArgMatches + IntoApp + Sized {
I: IntoIterator<Item = T>,
T: Into<OsString> + Clone,
{
let matches = <Self as IntoApp>::into_app().get_matches_from(itr);
let matches = <Self as IntoApp>::command().get_matches_from(itr);
let res =
<Self as FromArgMatches>::from_arg_matches(&matches).map_err(format_error::<Self>);
match res {
Expand All @@ -123,7 +123,7 @@ pub trait Parser: FromArgMatches + IntoApp + Sized {
I: IntoIterator<Item = T>,
T: Into<OsString> + Clone,
{
let matches = <Self as IntoApp>::into_app().try_get_matches_from(itr)?;
let matches = <Self as IntoApp>::command().try_get_matches_from(itr)?;
<Self as FromArgMatches>::from_arg_matches(&matches).map_err(format_error::<Self>)
}

Expand All @@ -133,7 +133,7 @@ pub trait Parser: FromArgMatches + IntoApp + Sized {
I: IntoIterator<Item = T>,
T: Into<OsString> + Clone,
{
let matches = <Self as IntoApp>::into_app_for_update().get_matches_from(itr);
let matches = <Self as IntoApp>::command().get_matches_from(itr);
let res = <Self as FromArgMatches>::update_from_arg_matches(self, &matches)
.map_err(format_error::<Self>);
if let Err(e) = res {
Expand All @@ -149,20 +149,20 @@ pub trait Parser: FromArgMatches + IntoApp + Sized {
I: IntoIterator<Item = T>,
T: Into<OsString> + Clone,
{
let matches = <Self as IntoApp>::into_app_for_update().try_get_matches_from(itr)?;
let matches = <Self as IntoApp>::command().try_get_matches_from(itr)?;
<Self as FromArgMatches>::update_from_arg_matches(self, &matches)
.map_err(format_error::<Self>)
}

/// Deprecated, `StructOpt::clap` replaced with [`IntoCommand::into_app`] (derive as part of
/// Deprecated, `StructOpt::clap` replaced with [`IntoCommand::command`] (derive as part of
/// [`Parser`])
#[deprecated(
since = "3.0.0",
note = "`StructOpt::clap` is replaced with `IntoCommand::into_app` (derived as part of `Parser`)"
note = "`StructOpt::clap` is replaced with `IntoCommand::command` (derived as part of `Parser`)"
)]
#[doc(hidden)]
fn clap<'help>() -> Command<'help> {
<Self as IntoApp>::into_app()
<Self as IntoApp>::command()
}

/// Deprecated, `StructOpt::from_clap` replaced with [`FromArgMatches::from_arg_matches`] (derive as part of
Expand Down Expand Up @@ -233,10 +233,22 @@ pub trait IntoApp: Sized {
/// Build an [`Command`] that can instantiate `Self`.
///
/// See [`FromArgMatches::from_arg_matches`] for instantiating `Self`.
fn command<'help>() -> Command<'help> {
#[allow(deprecated)]
Self::into_app()
}
/// Deprecated, replaced with `IntoApp::command`
#[deprecated(since = "3.1.0", note = "Replaced with `IntoApp::command")]
fn into_app<'help>() -> Command<'help>;
/// Build an [`Command`] that can update `self`.
///
/// See [`FromArgMatches::update_from_arg_matches`] for updating `self`.
fn command_for_update<'help>() -> Command<'help> {
#[allow(deprecated)]
Self::into_app_for_update()
}
/// Deprecated, replaced with `IntoApp::command_for_update`
#[deprecated(since = "3.1.0", note = "Replaced with `IntoApp::command_for_update")]
fn into_app_for_update<'help>() -> Command<'help>;
}

Expand Down Expand Up @@ -451,6 +463,7 @@ impl<T: Parser> Parser for Box<T> {
}
}

#[allow(deprecated)]
impl<T: IntoApp> IntoApp for Box<T> {
fn into_app<'help>() -> Command<'help> {
<T as IntoApp>::into_app()
Expand Down Expand Up @@ -491,6 +504,6 @@ impl<T: Subcommand> Subcommand for Box<T> {
}

fn format_error<I: IntoApp>(err: crate::Error) -> crate::Error {
let mut cmd = I::into_app();
let mut cmd = I::command();
err.format(&mut cmd)
}
16 changes: 8 additions & 8 deletions tests/derive/app_name.rs
Expand Up @@ -7,7 +7,7 @@ fn app_name_in_short_help_from_struct() {
struct MyApp {}

let mut help = Vec::new();
MyApp::into_app().write_help(&mut help).unwrap();
MyApp::command().write_help(&mut help).unwrap();
let help = String::from_utf8(help).unwrap();

assert!(help.contains("my-cmd"));
Expand All @@ -20,7 +20,7 @@ fn app_name_in_long_help_from_struct() {
struct MyApp {}

let mut help = Vec::new();
MyApp::into_app().write_long_help(&mut help).unwrap();
MyApp::command().write_long_help(&mut help).unwrap();
let help = String::from_utf8(help).unwrap();

assert!(help.contains("my-cmd"));
Expand All @@ -33,7 +33,7 @@ fn app_name_in_short_help_from_enum() {
enum MyApp {}

let mut help = Vec::new();
MyApp::into_app().write_help(&mut help).unwrap();
MyApp::command().write_help(&mut help).unwrap();
let help = String::from_utf8(help).unwrap();

assert!(help.contains("my-cmd"));
Expand All @@ -46,7 +46,7 @@ fn app_name_in_long_help_from_enum() {
enum MyApp {}

let mut help = Vec::new();
MyApp::into_app().write_long_help(&mut help).unwrap();
MyApp::command().write_long_help(&mut help).unwrap();
let help = String::from_utf8(help).unwrap();

assert!(help.contains("my-cmd"));
Expand All @@ -58,7 +58,7 @@ fn app_name_in_short_version_from_struct() {
#[clap(name = "my-cmd")]
struct MyApp {}

let version = MyApp::into_app().render_version();
let version = MyApp::command().render_version();

assert!(version.contains("my-cmd"));
}
Expand All @@ -69,7 +69,7 @@ fn app_name_in_long_version_from_struct() {
#[clap(name = "my-cmd")]
struct MyApp {}

let version = MyApp::into_app().render_long_version();
let version = MyApp::command().render_long_version();

assert!(version.contains("my-cmd"));
}
Expand All @@ -80,7 +80,7 @@ fn app_name_in_short_version_from_enum() {
#[clap(name = "my-cmd")]
enum MyApp {}

let version = MyApp::into_app().render_version();
let version = MyApp::command().render_version();

assert!(version.contains("my-cmd"));
}
Expand All @@ -91,7 +91,7 @@ fn app_name_in_long_version_from_enum() {
#[clap(name = "my-cmd")]
enum MyApp {}

let version = MyApp::into_app().render_long_version();
let version = MyApp::command().render_long_version();

assert!(version.contains("my-cmd"));
}
4 changes: 2 additions & 2 deletions tests/derive/arguments.rs
Expand Up @@ -52,7 +52,7 @@ fn auto_value_name() {
}

let mut help = Vec::new();
Opt::into_app().write_help(&mut help).unwrap();
Opt::command().write_help(&mut help).unwrap();
let help = String::from_utf8(help).unwrap();

assert!(help.contains("MY_SPECIAL_ARG"));
Expand All @@ -72,7 +72,7 @@ fn explicit_value_name() {
}

let mut help = Vec::new();
Opt::into_app().write_help(&mut help).unwrap();
Opt::command().write_help(&mut help).unwrap();
let help = String::from_utf8(help).unwrap();

assert!(help.contains("BROWNIE_POINTS"));
Expand Down
2 changes: 1 addition & 1 deletion tests/derive/default_value.rs
Expand Up @@ -53,5 +53,5 @@ fn detect_os_variant() {
#[clap(default_value_os = ("123".as_ref()))]
x: String,
}
Options::into_app().debug_assert();
Options::command().debug_assert();
}
2 changes: 1 addition & 1 deletion tests/derive/doc_comments_help.rs
Expand Up @@ -232,7 +232,7 @@ fn doc_comment_about_handles_both_abouts() {
Compress { output: String },
}

let cmd = Opts::into_app();
let cmd = Opts::command();
assert_eq!(cmd.get_about(), Some("Opts doc comment summary"));
// clap will fallback to `about` on `None`. The main care about is not providing a `Sub` doc
// comment.
Expand Down
14 changes: 7 additions & 7 deletions tests/derive/help.rs
Expand Up @@ -12,7 +12,7 @@ fn arg_help_heading_applied() {
no_section: u32,
}

let cmd = CliOptions::into_app();
let cmd = CliOptions::command();

let should_be_in_section_a = cmd
.get_arguments()
Expand Down Expand Up @@ -40,7 +40,7 @@ fn app_help_heading_applied() {
should_be_in_default_section: u32,
}

let cmd = CliOptions::into_app();
let cmd = CliOptions::command();

let should_be_in_section_a = cmd
.get_arguments()
Expand Down Expand Up @@ -117,7 +117,7 @@ fn app_help_heading_flattened() {
SubCOne { should_be_in_sub_c: u32 },
}

let cmd = CliOptions::into_app();
let cmd = CliOptions::command();

let should_be_in_section_a = cmd
.get_arguments()
Expand Down Expand Up @@ -178,7 +178,7 @@ fn flatten_field_with_help_heading() {
should_be_in_section_a: u32,
}

let cmd = CliOptions::into_app();
let cmd = CliOptions::command();

let should_be_in_section_a = cmd
.get_arguments()
Expand Down Expand Up @@ -278,7 +278,7 @@ OPTIONS:
}

use clap::IntoApp;
let mut cmd = Args::into_app();
let mut cmd = Args::command();

let mut buffer: Vec<u8> = Default::default();
cmd.write_help(&mut buffer).unwrap();
Expand Down Expand Up @@ -335,7 +335,7 @@ OPTIONS:
}

use clap::IntoApp;
let mut cmd = Args::into_app();
let mut cmd = Args::command();

let mut buffer: Vec<u8> = Default::default();
cmd.write_help(&mut buffer).unwrap();
Expand Down Expand Up @@ -391,7 +391,7 @@ OPTIONS:
}

use clap::IntoApp;
let mut cmd = Args::into_app();
let mut cmd = Args::command();

let mut buffer: Vec<u8> = Default::default();
cmd.write_help(&mut buffer).unwrap();
Expand Down
6 changes: 3 additions & 3 deletions tests/derive/utils.rs
Expand Up @@ -9,7 +9,7 @@ use clap::IntoApp;

pub fn get_help<T: IntoApp>() -> String {
let mut output = Vec::new();
<T as IntoApp>::into_app().write_help(&mut output).unwrap();
<T as IntoApp>::command().write_help(&mut output).unwrap();
let output = String::from_utf8(output).unwrap();

eprintln!("\n%%% HELP %%%:=====\n{}\n=====\n", output);
Expand All @@ -20,7 +20,7 @@ pub fn get_help<T: IntoApp>() -> String {

pub fn get_long_help<T: IntoApp>() -> String {
let mut output = Vec::new();
<T as IntoApp>::into_app()
<T as IntoApp>::command()
.write_long_help(&mut output)
.unwrap();
let output = String::from_utf8(output).unwrap();
Expand All @@ -33,7 +33,7 @@ pub fn get_long_help<T: IntoApp>() -> String {

pub fn get_subcommand_long_help<T: IntoApp>(subcmd: &str) -> String {
let mut output = Vec::new();
<T as IntoApp>::into_app()
<T as IntoApp>::command()
.get_subcommands_mut()
.find(|s| s.get_name() == subcmd)
.unwrap()
Expand Down

0 comments on commit c3fec1f

Please sign in to comment.