From d49e8292b026b06e2b70447cd9f08299f4fcba76 Mon Sep 17 00:00:00 2001 From: Richard Janis Goldschmidt Date: Tue, 21 Mar 2017 11:00:30 +0100 Subject: [PATCH] api(App::name): adds the ability to change the name of the App instance after creation Closes #908 --- src/app/mod.rs | 27 +++++++++++++++++++++++++++ tests/app_settings.rs | 5 +++-- 2 files changed, 30 insertions(+), 2 deletions(-) diff --git a/src/app/mod.rs b/src/app/mod.rs index 4473f9a1d29..428bcdbd3b6 100644 --- a/src/app/mod.rs +++ b/src/app/mod.rs @@ -215,6 +215,33 @@ impl<'a, 'b> App<'a, 'b> { self } + /// Sets the program's name. This will be displayed when displaying help information. + /// + /// **Pro-top:** This function is particularly useful when configuring a program via + /// [`App::from_yaml`] in conjunction with the [`crate_name!`] macro to derive the program's + /// name from its `Cargo.toml`. + /// + /// # Examples + /// ```ignore + /// # #[macro_use] + /// # extern crate clap; + /// # use clap::App; + /// # fn main() { + /// let yml = load_yaml!("app.yml"); + /// let app = App::from_yaml(yml) + /// .name(crate_name!()); + /// + /// // continued logic goes here, such as `app.get_matches()` etc. + /// # } + /// ``` + /// + /// [`App::from_yaml`]: ./struct.App.html#method.from_yaml + /// [`crate_name!`]: ./macro.crate_name.html + pub fn name>(mut self, name: S) -> Self { + self.p.meta.name = name.into(); + self + } + /// Adds additional help information to be displayed in addition to auto-generated help. This /// information is displayed **after** the auto-generated help information. This is often used /// to describe how to use the arguments, or caveats to be noted. diff --git a/tests/app_settings.rs b/tests/app_settings.rs index c8cb90a747a..3b2a34bc92a 100644 --- a/tests/app_settings.rs +++ b/tests/app_settings.rs @@ -256,7 +256,8 @@ fn no_bin_name() { #[test] fn unified_help() { - let app = App::new("test") + let app = App::new("myTest") + .name("test") .author("Kevin K.") .about("tests stuff") .version("1.3") @@ -618,4 +619,4 @@ fn allow_missing_positional() { let m = m.unwrap(); assert_eq!(m.value_of("src"), Some("src")); assert_eq!(m.value_of("dest"), Some("file")); -} \ No newline at end of file +}