diff --git a/examples/08_subcommands.rs b/examples/08_subcommands.rs index 7381b0b6adb..fe6cdae3105 100644 --- a/examples/08_subcommands.rs +++ b/examples/08_subcommands.rs @@ -15,7 +15,7 @@ fn main() { // // Just like arg() and args(), subcommands can be specified one at a time via subcommand() or // multiple ones at once with a Vec<> provided to subcommands(). - let matches = App::new("MyApp") + let app = App::new("MyApp") // Normal App and Arg configuration goes here... // In the following example assume we wanted an application which // supported an "add" subcommand, this "add" subcommand also took @@ -33,8 +33,19 @@ fn main() { .index(1) .required(true), ), - ) - .get_matches(); + ); + + let app = if true { + let about_text = "i-am-an-about-text".to_string() + "-for-foobar"; + app.subcommand( + App::new("foobar") + .about(about_text) + ) + } else { + app + }; + + let matches = app.get_matches(); // You can check if a subcommand was used like normal if matches.is_present("add") { diff --git a/src/build/app/mod.rs b/src/build/app/mod.rs index b22893d48ba..515a039abdb 100644 --- a/src/build/app/mod.rs +++ b/src/build/app/mod.rs @@ -8,6 +8,7 @@ pub use self::settings::AppSettings; // Std use std::{ + borrow::Cow, collections::HashMap, env, ffi::OsString, @@ -71,7 +72,7 @@ pub struct App<'help> { pub(crate) version: Option<&'help str>, pub(crate) long_version: Option<&'help str>, pub(crate) license: Option<&'help str>, - pub(crate) about: Option<&'help str>, + pub(crate) about: Option>, pub(crate) long_about: Option<&'help str>, pub(crate) before_help: Option<&'help str>, pub(crate) before_long_help: Option<&'help str>, @@ -460,7 +461,7 @@ impl<'help> App<'help> { /// [long format]: App::long_about() /// [short format]: App::about() /// [`App::about`]: App::about() - pub fn about>(mut self, about: S) -> Self { + pub fn about>>(mut self, about: S) -> Self { self.about = Some(about.into()); self } diff --git a/src/output/help.rs b/src/output/help.rs index e8a9b7d6bdc..6f37300644e 100644 --- a/src/output/help.rs +++ b/src/output/help.rs @@ -646,9 +646,12 @@ impl<'help, 'app, 'parser, 'writer> Help<'help, 'app, 'parser, 'writer> { fn write_about(&mut self, before_new_line: bool, after_new_line: bool) -> io::Result<()> { let about = if self.use_long { - self.parser.app.long_about.or(self.parser.app.about) + self.parser + .app + .long_about + .or_else(|| self.parser.app.about.as_ref().map(|a| a.as_ref())) } else { - self.parser.app.about + self.parser.app.about.as_ref().map(|a| a.as_ref()) }; if let Some(output) = about { if before_new_line { @@ -702,9 +705,13 @@ impl<'help, 'app, 'parser, 'writer> Help<'help, 'app, 'parser, 'writer> { let spec_vals = &self.sc_spec_vals(app); let about = if self.use_long { - app.long_about.unwrap_or_else(|| app.about.unwrap_or("")) + app.long_about + .unwrap_or_else(|| app.about.as_ref().map(|a| a.as_ref()).unwrap_or("")) } else { - app.about.unwrap_or_else(|| app.long_about.unwrap_or("")) + app.about + .as_ref() + .map(|a| a.as_ref()) + .unwrap_or_else(|| app.long_about.unwrap_or("")) }; self.subcmd(sc_str, next_line_help, longest)?; @@ -745,7 +752,7 @@ impl<'help, 'app, 'parser, 'writer> Help<'help, 'app, 'parser, 'writer> { true } else { // force_next_line - let h = app.about.unwrap_or(""); + let h = app.about.as_ref().map(|a| a.as_ref()).unwrap_or(""); let h_w = display_width(h) + display_width(spec_vals); let taken = longest + 12; self.term_w >= taken