Skip to content

Commit 38fb59a

Browse files
committed
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
1 parent d9372d0 commit 38fb59a

File tree

3 files changed

+54
-3
lines changed

3 files changed

+54
-3
lines changed

examples/19_auto_authors.rs

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#[macro_use]
2+
extern crate clap;
3+
4+
use clap::App;
5+
6+
#[cfg(feature = "unstable")]
7+
fn main() {
8+
App::new("myapp")
9+
.about("does awesome things")
10+
// use crate_authors! to pull the author(s) names from the Cargo.toml
11+
.author(crate_authors!())
12+
.get_matches();
13+
14+
// running the this app with the -h will display whatever author(s) are in your
15+
// Cargo.toml
16+
}
17+
18+
#[cfg(not(feature = "unstable"))]
19+
fn main() {
20+
// if clap is not compiled with the unstable feature, it is disabled.
21+
println!("unstable feature disabled.");
22+
println!("Pass --features unstable to cargo when trying this example.");
23+
}

src/app/mod.rs

+8-2
Original file line numberDiff line numberDiff line change
@@ -110,9 +110,15 @@ impl<'a, 'b> App<'a, 'b> {
110110
App::from(yaml)
111111
}
112112

113-
/// Sets a string of author(s) that will be displayed to the user when they request the help
114-
/// information with `--help` or `-h`.
113+
/// Sets a string of author(s) that will be displayed to the user when they
114+
/// request the help information with `--help` or `-h`.
115115
///
116+
/// **Pro-tip:** If you turn on unstable features you can use `clap`s
117+
/// convienience macro `crate_authors!` to automatically set your
118+
/// application's author to the same thing as your crate at compile time.
119+
/// See the `examples/`
120+
/// directory for more information
121+
///
116122
/// # Examples
117123
///
118124
/// ```no_run

src/macros.rs

+23-1
Original file line numberDiff line numberDiff line change
@@ -350,7 +350,7 @@ macro_rules! arg_enum {
350350
};
351351
}
352352

353-
/// Allows you pull the version for an from your Cargo.toml at compile time as
353+
/// Allows you pull the version from your Cargo.toml at compile time as
354354
/// MAJOR.MINOR.PATCH_PKGVERSION_PRE
355355
///
356356
/// # Examples
@@ -372,6 +372,28 @@ macro_rules! crate_version {
372372
};
373373
}
374374

375+
/// Allows you pull the authors for the app from your Cargo.toml at compile time as
376+
/// "author1 lastname. <[email protected]>", "author2 lastname. <[email protected]>"
377+
///
378+
/// # Examples
379+
///
380+
/// ```no_run
381+
/// # #[macro_use]
382+
/// # extern crate clap;
383+
/// # use clap::App;
384+
/// # fn main() {
385+
/// let m = App::new("app")
386+
/// .author(crate_authors!())
387+
/// .get_matches();
388+
/// # }
389+
/// ```
390+
#[cfg_attr(feature = "unstable", macro_export)]
391+
macro_rules! crate_authors {
392+
() => {
393+
env!("CARGO_PKG_AUTHORS")
394+
};
395+
}
396+
375397
/// App, Arg, SubCommand and Group builder macro (Usage-string like input) must be compiled with
376398
/// the `unstable` feature in order to use.
377399
#[cfg_attr(feature = "unstable", macro_export)]

0 commit comments

Comments
 (0)