Skip to content

Commit

Permalink
feat(Authors Macro): adds a crate_authors macro
Browse files Browse the repository at this point in the history
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
  • Loading branch information
rtaycher committed Apr 11, 2016
1 parent d9372d0 commit 38fb59a
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 3 deletions.
23 changes: 23 additions & 0 deletions examples/19_auto_authors.rs
Original file line number Diff line number Diff line change
@@ -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.");
}
10 changes: 8 additions & 2 deletions src/app/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
24 changes: 23 additions & 1 deletion src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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. <[email protected]>", "author2 lastname. <[email protected]>"
///
/// # 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)]
Expand Down

0 comments on commit 38fb59a

Please sign in to comment.