Skip to content

Commit

Permalink
fix: Rename IntoApp to CommandFactory
Browse files Browse the repository at this point in the history
This is part of the `App` rename.

Previously, I was concerned about not being able to deprecate

For backwards compatibility, we still expose the `IntoApp` name.
  • Loading branch information
epage committed Feb 15, 2022
1 parent c3fec1f commit ddac492
Show file tree
Hide file tree
Showing 15 changed files with 56 additions and 46 deletions.
2 changes: 1 addition & 1 deletion clap_complete/examples/value_hints_derive.rs
Expand Up @@ -12,7 +12,7 @@
//! . ./value_hints_derive.fish
//! ./target/debug/examples/value_hints_derive --<TAB>
//! ```
use clap::{Command, IntoApp, Parser, ValueHint};
use clap::{Command, CommandFactory, Parser, ValueHint};
use clap_complete::{generate, Generator, Shell};
use std::ffi::OsString;
use std::io;
Expand Down
4 changes: 2 additions & 2 deletions clap_derive/src/derives/into_app.rs
Expand Up @@ -57,7 +57,7 @@ pub fn gen_for_struct(
)]
#[deny(clippy::correctness)]
#[allow(deprecated)]
impl #impl_generics clap::IntoApp for #struct_name #ty_generics #where_clause {
impl #impl_generics clap::CommandFactory for #struct_name #ty_generics #where_clause {
fn into_app<'b>() -> clap::Command<'b> {
let #app_var = clap::Command::new(#name);
<Self as clap::Args>::augment_args(#app_var)
Expand Down Expand Up @@ -102,7 +102,7 @@ pub fn gen_for_enum(enum_name: &Ident, generics: &Generics, attrs: &[Attribute])
clippy::suspicious_else_formatting,
)]
#[deny(clippy::correctness)]
impl #impl_generics clap::IntoApp for #enum_name #ty_generics #where_clause {
impl #impl_generics clap::CommandFactory for #enum_name #ty_generics #where_clause {
fn into_app<'b>() -> clap::Command<'b> {
#[allow(deprecated)]
let #app_var = clap::Command::new(#name)
Expand Down
2 changes: 1 addition & 1 deletion clap_derive/src/dummies.rs
Expand Up @@ -19,7 +19,7 @@ pub fn parser_enum(name: &Ident) {
pub fn into_app(name: &Ident) {
append_dummy(quote! {
#[allow(deprecated)]
impl clap::IntoApp for #name {
impl clap::CommandFactory for #name {
fn into_app<'b>() -> clap::Command<'b> {
unimplemented!()
}
Expand Down
2 changes: 1 addition & 1 deletion examples/tutorial_derive/04_04_custom.rs
@@ -1,4 +1,4 @@
use clap::{ErrorKind, IntoApp, Parser};
use clap::{CommandFactory, ErrorKind, Parser};

#[derive(Parser)]
#[clap(author, version, about, long_about = None)]
Expand Down
2 changes: 1 addition & 1 deletion examples/tutorial_derive/05_01_assert.rs
Expand Up @@ -16,6 +16,6 @@ fn main() {

#[test]
fn verify_app() {
use clap::IntoApp;
use clap::CommandFactory;
Cli::command().debug_assert()
}
2 changes: 1 addition & 1 deletion src/build/command.rs
Expand Up @@ -40,7 +40,7 @@ use crate::build::debug_asserts::assert_app;
/// arguments (or lack thereof).
///
/// When deriving a [`Parser`][crate::Parser], you can use
/// [`IntoApp::into_app`][crate::IntoApp::into_app] to access the
/// [`CommandFactory::into_app`][crate::CommandFactory::into_app] to access the
/// `Command`.
///
/// # Examples
Expand Down
47 changes: 25 additions & 22 deletions src/derive.rs
Expand Up @@ -12,7 +12,7 @@ use std::ffi::OsString;
/// into concrete instance of the user struct.
///
/// This trait is primarily a convenience on top of [`FromArgMatches`] +
/// [`IntoApp`] which uses those two underlying traits to build the two
/// [`CommandFactory`] which uses those two underlying traits to build the two
/// fundamental functions `parse` which uses the `std::env::args_os` iterator,
/// and `parse_from` which allows the consumer to supply the iterator (along
/// with fallible options for each).
Expand Down Expand Up @@ -76,10 +76,10 @@ use std::ffi::OsString;
/// }
/// ```
///
pub trait Parser: FromArgMatches + IntoApp + Sized {
pub trait Parser: FromArgMatches + CommandFactory + Sized {
/// Parse from `std::env::args_os()`, exit on error
fn parse() -> Self {
let matches = <Self as IntoApp>::command().get_matches();
let matches = <Self as CommandFactory>::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>::command().try_get_matches()?;
let matches = <Self as CommandFactory>::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>::command().get_matches_from(itr);
let matches = <Self as CommandFactory>::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>::command().try_get_matches_from(itr)?;
let matches = <Self as CommandFactory>::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>::command().get_matches_from(itr);
let matches = <Self as CommandFactory>::command_for_update().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,7 +149,7 @@ pub trait Parser: FromArgMatches + IntoApp + Sized {
I: IntoIterator<Item = T>,
T: Into<OsString> + Clone,
{
let matches = <Self as IntoApp>::command().try_get_matches_from(itr)?;
let matches = <Self as CommandFactory>::command_for_update().try_get_matches_from(itr)?;
<Self as FromArgMatches>::update_from_arg_matches(self, &matches)
.map_err(format_error::<Self>)
}
Expand All @@ -162,7 +162,7 @@ pub trait Parser: FromArgMatches + IntoApp + Sized {
)]
#[doc(hidden)]
fn clap<'help>() -> Command<'help> {
<Self as IntoApp>::command()
<Self as CommandFactory>::command()
}

/// Deprecated, `StructOpt::from_clap` replaced with [`FromArgMatches::from_arg_matches`] (derive as part of
Expand Down Expand Up @@ -229,16 +229,16 @@ pub trait Parser: FromArgMatches + IntoApp + Sized {
/// Create an [`Command`] relevant for a user-defined container.
///
/// Derived as part of [`Parser`].
pub trait IntoApp: Sized {
pub trait CommandFactory: 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")]
/// Deprecated, replaced with `CommandFactory::command`
#[deprecated(since = "3.1.0", note = "Replaced with `CommandFactory::command")]
fn into_app<'help>() -> Command<'help>;
/// Build an [`Command`] that can update `self`.
///
Expand All @@ -247,8 +247,11 @@ pub trait IntoApp: Sized {
#[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")]
/// Deprecated, replaced with `CommandFactory::command_for_update`
#[deprecated(
since = "3.1.0",
note = "Replaced with `CommandFactory::command_for_update"
)]
fn into_app_for_update<'help>() -> Command<'help>;
}

Expand Down Expand Up @@ -327,13 +330,13 @@ pub trait FromArgMatches: Sized {
pub trait Args: FromArgMatches + Sized {
/// Append to [`Command`] so it can instantiate `Self`.
///
/// See also [`IntoApp`].
/// See also [`CommandFactory`].
fn augment_args(cmd: Command<'_>) -> Command<'_>;
/// Append to [`Command`] so it can update `self`.
///
/// This is used to implement `#[clap(flatten)]`
///
/// See also [`IntoApp`].
/// See also [`CommandFactory`].
fn augment_args_for_update(cmd: Command<'_>) -> Command<'_>;
}

Expand Down Expand Up @@ -371,13 +374,13 @@ pub trait Args: FromArgMatches + Sized {
pub trait Subcommand: FromArgMatches + Sized {
/// Append to [`Command`] so it can instantiate `Self`.
///
/// See also [`IntoApp`].
/// See also [`CommandFactory`].
fn augment_subcommands(cmd: Command<'_>) -> Command<'_>;
/// Append to [`Command`] so it can update `self`.
///
/// This is used to implement `#[clap(flatten)]`
///
/// See also [`IntoApp`].
/// See also [`CommandFactory`].
fn augment_subcommands_for_update(cmd: Command<'_>) -> Command<'_>;
/// Test whether `Self` can parse a specific subcommand
fn has_subcommand(name: &str) -> bool;
Expand Down Expand Up @@ -464,12 +467,12 @@ impl<T: Parser> Parser for Box<T> {
}

#[allow(deprecated)]
impl<T: IntoApp> IntoApp for Box<T> {
impl<T: CommandFactory> CommandFactory for Box<T> {
fn into_app<'help>() -> Command<'help> {
<T as IntoApp>::into_app()
<T as CommandFactory>::into_app()
}
fn into_app_for_update<'help>() -> Command<'help> {
<T as IntoApp>::into_app_for_update()
<T as CommandFactory>::into_app_for_update()
}
}

Expand Down Expand Up @@ -503,7 +506,7 @@ impl<T: Subcommand> Subcommand for Box<T> {
}
}

fn format_error<I: IntoApp>(err: crate::Error) -> crate::Error {
fn format_error<I: CommandFactory>(err: crate::Error) -> crate::Error {
let mut cmd = I::command();
err.format(&mut cmd)
}
6 changes: 5 additions & 1 deletion src/lib.rs
Expand Up @@ -35,7 +35,7 @@ pub use crate::parse::{ArgMatches, Indices, OsValues, ValueSource, Values};
#[cfg(feature = "color")]
pub use crate::util::color::ColorChoice;

pub use crate::derive::{ArgEnum, Args, FromArgMatches, IntoApp, Parser, Subcommand};
pub use crate::derive::{ArgEnum, Args, CommandFactory, FromArgMatches, Parser, Subcommand};

pub use crate::error::{ErrorKind, Result};

Expand All @@ -55,6 +55,10 @@ pub use yaml_rust::YamlLoader;
#[doc(hidden)]
pub use clap_derive::{self, *};

/// Deprecated, replaced with [`CommandFactory`]
#[deprecated(since = "3.0.0", note = "Replaced with `CommandFactory`")]
#[doc(hidden)]
pub use CommandFactory as IntoApp;
/// Deprecated, replaced with [`Parser`]
#[deprecated(since = "3.0.0", note = "Replaced with `Parser`")]
#[doc(hidden)]
Expand Down
2 changes: 1 addition & 1 deletion tests/derive/app_name.rs
@@ -1,4 +1,4 @@
use clap::IntoApp;
use clap::CommandFactory;
use clap::Parser;
#[test]
fn app_name_in_short_help_from_struct() {
Expand Down
2 changes: 1 addition & 1 deletion tests/derive/arguments.rs
Expand Up @@ -12,7 +12,7 @@
// commit#ea76fa1b1b273e65e3b0b1046643715b49bec51f which is licensed under the
// MIT/Apache 2.0 license.

use clap::IntoApp;
use clap::CommandFactory;
use clap::Parser;

#[test]
Expand Down
3 changes: 2 additions & 1 deletion tests/derive/custom_string_parsers.rs
Expand Up @@ -144,7 +144,8 @@ fn update_every_custom_parser() {
d: "D",
};

opt.update_from(&["test", "-a=?", "-b=?", "-d=?"]);
opt.try_update_from(&["test", "-a=?", "-b=?", "-d=?"])
.unwrap();

assert_eq!(
NoOpOpt {
Expand Down
2 changes: 1 addition & 1 deletion tests/derive/default_value.rs
@@ -1,4 +1,4 @@
use clap::{IntoApp, Parser};
use clap::{CommandFactory, Parser};

use crate::utils;

Expand Down
2 changes: 1 addition & 1 deletion tests/derive/doc_comments_help.rs
Expand Up @@ -14,7 +14,7 @@

use crate::utils;

use clap::{ArgEnum, IntoApp, Parser};
use clap::{ArgEnum, CommandFactory, Parser};

#[test]
fn doc_comments() {
Expand Down
8 changes: 4 additions & 4 deletions tests/derive/help.rs
@@ -1,4 +1,4 @@
use clap::{AppSettings, Args, IntoApp, Parser, Subcommand};
use clap::{AppSettings, Args, CommandFactory, Parser, Subcommand};

#[test]
fn arg_help_heading_applied() {
Expand Down Expand Up @@ -277,7 +277,7 @@ OPTIONS:
option_b: Option<String>,
}

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

let mut buffer: Vec<u8> = Default::default();
Expand Down Expand Up @@ -334,7 +334,7 @@ OPTIONS:
option_b: Option<String>,
}

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

let mut buffer: Vec<u8> = Default::default();
Expand Down Expand Up @@ -390,7 +390,7 @@ OPTIONS:
option_b: Option<String>,
}

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

let mut buffer: Vec<u8> = Default::default();
Expand Down
16 changes: 9 additions & 7 deletions tests/derive/utils.rs
Expand Up @@ -5,11 +5,13 @@
// Accept and endure. Do not touch.
#![allow(unused)]

use clap::IntoApp;
use clap::CommandFactory;

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

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

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

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

0 comments on commit ddac492

Please sign in to comment.