Skip to content

Commit

Permalink
fix: Update app_from_crate for App rename
Browse files Browse the repository at this point in the history
Instead of just renaming it, I reconsidered what the API should look
like.  A custom separator for author does not make sense positionally
but accepting a name, and defaulting it, does fit with what someone
would expect.

I removed the `_from_crate` suffix because it doesn't seem necessary.
We don't have this kind of naming for the derive.  I feel it cleans
things up this way.
  • Loading branch information
epage committed Feb 15, 2022
1 parent 7aa4566 commit 65b9c88
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 7 deletions.
33 changes: 26 additions & 7 deletions src/macros.rs
Expand Up @@ -283,15 +283,10 @@ macro_rules! crate_name {

/// Allows you to build the `Command` instance from your Cargo.toml at compile time.
///
/// Equivalent to using the `crate_*!` macros with their respective fields.
///
/// Provided separator is for the [`crate_authors!`] macro,
/// refer to the documentation therefor.
///
/// **NOTE:** Changing the values in your `Cargo.toml` does not trigger a re-build automatically,
/// and therefore won't change the generated output until you recompile.
///
/// **Pro Tip:** In some cases you can "trick" the compiler into triggering a rebuild when your
/// In some cases you can "trick" the compiler into triggering a rebuild when your
/// `Cargo.toml` is changed by including this in your `src/main.rs` file
/// `include_str!("../Cargo.toml");`
///
Expand All @@ -301,11 +296,35 @@ macro_rules! crate_name {
/// # #[macro_use]
/// # extern crate clap;
/// # fn main() {
/// let m = app_from_crate!().get_matches();
/// let m = command!().get_matches();
/// # }
/// ```
#[cfg(feature = "cargo")]
#[macro_export]
macro_rules! command {
() => {{
$crate::command!($crate::crate_name!())
}};
($name:expr) => {{
let mut cmd = $crate::Command::new($name).version($crate::crate_version!());

let author = $crate::crate_authors!();
if !author.is_empty() {
cmd = cmd.author(author)
}

let about = $crate::crate_description!();
if !about.is_empty() {
cmd = cmd.about(about)
}

cmd
}};
}

/// Deprecated, replaced with [`clap::command!`][crate::command]
#[cfg(feature = "cargo")]
#[macro_export]
macro_rules! app_from_crate {
() => {{
let mut cmd = $crate::Command::new($crate::crate_name!()).version($crate::crate_version!());
Expand Down
27 changes: 27 additions & 0 deletions tests/builder/command.rs
@@ -0,0 +1,27 @@
#![cfg(feature = "cargo")]

use clap::{command, error::ErrorKind};

static EVERYTHING: &str = "clap {{version}}
A simple to use, efficient, and full-featured Command Line Argument Parser
USAGE:
clap
OPTIONS:
-h, --help Print help information
-V, --version Print version information
";

#[test]
fn command() {
let res = command!().try_get_matches_from(vec!["clap", "--help"]);

assert!(res.is_err());
let err = res.unwrap_err();
assert_eq!(err.kind(), ErrorKind::DisplayHelp);
assert_eq!(
err.to_string(),
EVERYTHING.replace("{{version}}", env!("CARGO_PKG_VERSION"))
);
}
1 change: 1 addition & 0 deletions tests/builder/main.rs
Expand Up @@ -6,6 +6,7 @@ mod arg_matcher_assertions;
mod arg_settings;
mod borrowed;
mod cargo;
mod command;
mod conflicts;
mod default_missing_vals;
mod default_vals;
Expand Down

0 comments on commit 65b9c88

Please sign in to comment.