Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make App::about accept Cow #2504

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
17 changes: 14 additions & 3 deletions examples/08_subcommands.rs
Expand Up @@ -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
Expand All @@ -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") {
Expand Down
5 changes: 3 additions & 2 deletions src/build/app/mod.rs
Expand Up @@ -8,6 +8,7 @@ pub use self::settings::AppSettings;

// Std
use std::{
borrow::Cow,
collections::HashMap,
env,
ffi::OsString,
Expand Down Expand Up @@ -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<Cow<'help, str>>,
pub(crate) long_about: Option<&'help str>,
pub(crate) before_help: Option<&'help str>,
pub(crate) before_long_help: Option<&'help str>,
Expand Down Expand Up @@ -460,7 +461,7 @@ impl<'help> App<'help> {
/// [long format]: App::long_about()
/// [short format]: App::about()
/// [`App::about`]: App::about()
pub fn about<S: Into<&'help str>>(mut self, about: S) -> Self {
pub fn about<S: Into<Cow<'help, str>>>(mut self, about: S) -> Self {
self.about = Some(about.into());
self
}
Expand Down
17 changes: 12 additions & 5 deletions src/output/help.rs
Expand Up @@ -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())
Copy link

@miDeb miDeb Jun 18, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Quick suggestion: This could be written simpler:

Suggested change
self.parser.app.about.as_ref().map(|a| a.as_ref())
self.parser.app.about.as_deref()

(applies to other occurences of .as_ref().map(|a| a.as_ref()) as well, they can all be .as_deref())

};
if let Some(output) = about {
if before_new_line {
Expand Down Expand Up @@ -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)?;
Expand Down Expand Up @@ -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
Expand Down