From 38fb59abf480eb2b6feca269097412f8b00b5b54 Mon Sep 17 00:00:00 2001 From: "Roman A. Taycher" Date: Mon, 11 Apr 2016 16:17:44 -0700 Subject: [PATCH 1/3] feat(Authors Macro): adds a crate_authors macro Adds a crate_authors! macro that fetches crate authors from a (recently added) cargo enviromental variable populated from the Cargo file. Like the crate_version macro. Closes #447 --- examples/19_auto_authors.rs | 23 +++++++++++++++++++++++ src/app/mod.rs | 10 ++++++++-- src/macros.rs | 24 +++++++++++++++++++++++- 3 files changed, 54 insertions(+), 3 deletions(-) create mode 100644 examples/19_auto_authors.rs diff --git a/examples/19_auto_authors.rs b/examples/19_auto_authors.rs new file mode 100644 index 00000000..d616dd53 --- /dev/null +++ b/examples/19_auto_authors.rs @@ -0,0 +1,23 @@ +#[macro_use] +extern crate clap; + +use clap::App; + +#[cfg(feature = "unstable")] +fn main() { + App::new("myapp") + .about("does awesome things") + // use crate_authors! to pull the author(s) names from the Cargo.toml + .author(crate_authors!()) + .get_matches(); + + // running the this app with the -h will display whatever author(s) are in your + // Cargo.toml +} + +#[cfg(not(feature = "unstable"))] +fn main() { + // if clap is not compiled with the unstable feature, it is disabled. + println!("unstable feature disabled."); + println!("Pass --features unstable to cargo when trying this example."); +} \ No newline at end of file diff --git a/src/app/mod.rs b/src/app/mod.rs index 17ff3d5f..2ba232c9 100644 --- a/src/app/mod.rs +++ b/src/app/mod.rs @@ -110,9 +110,15 @@ impl<'a, 'b> App<'a, 'b> { App::from(yaml) } - /// Sets a string of author(s) that will be displayed to the user when they request the help - /// information with `--help` or `-h`. + /// Sets a string of author(s) that will be displayed to the user when they + /// request the help information with `--help` or `-h`. /// + /// **Pro-tip:** If you turn on unstable features you can use `clap`s + /// convienience macro `crate_authors!` to automatically set your + /// application's author to the same thing as your crate at compile time. + /// See the `examples/` + /// directory for more information + /// /// # Examples /// /// ```no_run diff --git a/src/macros.rs b/src/macros.rs index f7d50505..781cd5d8 100644 --- a/src/macros.rs +++ b/src/macros.rs @@ -350,7 +350,7 @@ macro_rules! arg_enum { }; } -/// Allows you pull the version for an from your Cargo.toml at compile time as +/// Allows you pull the version from your Cargo.toml at compile time as /// MAJOR.MINOR.PATCH_PKGVERSION_PRE /// /// # Examples @@ -372,6 +372,28 @@ macro_rules! crate_version { }; } +/// Allows you pull the authors for the app from your Cargo.toml at compile time as +/// "author1 lastname. ", "author2 lastname. " +/// +/// # Examples +/// +/// ```no_run +/// # #[macro_use] +/// # extern crate clap; +/// # use clap::App; +/// # fn main() { +/// let m = App::new("app") +/// .author(crate_authors!()) +/// .get_matches(); +/// # } +/// ``` +#[cfg_attr(feature = "unstable", macro_export)] +macro_rules! crate_authors { + () => { + env!("CARGO_PKG_AUTHORS") + }; +} + /// App, Arg, SubCommand and Group builder macro (Usage-string like input) must be compiled with /// the `unstable` feature in order to use. #[cfg_attr(feature = "unstable", macro_export)] From ac468643818a52c949c6fab20923ef685088f190 Mon Sep 17 00:00:00 2001 From: "Roman A. Taycher" Date: Mon, 11 Apr 2016 16:49:39 -0700 Subject: [PATCH 2/3] move import inside function to avoid conditional compilation warning --- examples/19_auto_authors.rs | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/examples/19_auto_authors.rs b/examples/19_auto_authors.rs index d616dd53..9d2b060e 100644 --- a/examples/19_auto_authors.rs +++ b/examples/19_auto_authors.rs @@ -1,10 +1,9 @@ #[macro_use] extern crate clap; -use clap::App; - #[cfg(feature = "unstable")] fn main() { + use clap::App; App::new("myapp") .about("does awesome things") // use crate_authors! to pull the author(s) names from the Cargo.toml @@ -20,4 +19,4 @@ fn main() { // if clap is not compiled with the unstable feature, it is disabled. println!("unstable feature disabled."); println!("Pass --features unstable to cargo when trying this example."); -} \ No newline at end of file +} From d0f1d204a0eb3c17f8cef6a8a8f90eefe5a75f64 Mon Sep 17 00:00:00 2001 From: "Roman A. Taycher" Date: Mon, 11 Apr 2016 22:37:36 -0700 Subject: [PATCH 3/3] minor comment fixes --- src/macros.rs | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/macros.rs b/src/macros.rs index 781cd5d8..a463d26e 100644 --- a/src/macros.rs +++ b/src/macros.rs @@ -350,7 +350,7 @@ macro_rules! arg_enum { }; } -/// Allows you pull the version from your Cargo.toml at compile time as +/// Allows you to pull the version from your Cargo.toml at compile time as /// MAJOR.MINOR.PATCH_PKGVERSION_PRE /// /// # Examples @@ -372,8 +372,10 @@ macro_rules! crate_version { }; } -/// Allows you pull the authors for the app from your Cargo.toml at compile time as -/// "author1 lastname. ", "author2 lastname. " +/// Allows you to pull the authors for the app from your Cargo.toml at +/// compile time as +/// "author1 lastname. ", +/// "author2 lastname. " /// /// # Examples ///