-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(Help): allows one to fully override the auto-generated help message
Allows overriding the entire help message so that nothing is auto-generated Closes #141
- Loading branch information
Showing
1 changed file
with
45 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -121,6 +121,7 @@ pub struct App<'a, 'v, 'ab, 'u, 'h, 'ar> { | |
global_args: Vec<Arg<'ar, 'ar, 'ar, 'ar, 'ar, 'ar>>, | ||
no_sc_error: bool, | ||
help_on_no_args: bool, | ||
help_str: Option<&'u str>, | ||
help_on_no_sc: bool | ||
} | ||
|
||
|
@@ -166,6 +167,7 @@ impl<'a, 'v, 'ab, 'u, 'h, 'ar> App<'a, 'v, 'ab, 'u, 'h, 'ar>{ | |
subcmds_neg_reqs: false, | ||
global_args: vec![], | ||
no_sc_error: false, | ||
help_str: None, | ||
help_on_no_args: false, | ||
help_on_no_sc: false | ||
} | ||
|
@@ -337,6 +339,45 @@ impl<'a, 'v, 'ab, 'u, 'h, 'ar> App<'a, 'v, 'ab, 'u, 'h, 'ar>{ | |
self | ||
} | ||
|
||
/// Sets a custom help message and overrides the auto-generated one. This should only be used | ||
/// when the auto-gererated message does not suffice. | ||
/// | ||
/// This will be displayed to the user when they use the defailt `--help` or `-h` | ||
/// | ||
/// **NOTE:** This replaces the **entire** help message, so nothing will be auto-gererated. | ||
/// | ||
/// **NOTE:** This **only** replaces the help message for the current command, meaning if you | ||
/// are using subcommands, those help messages will still be auto-gererated unless you | ||
/// specify a `.help()` for them as well. | ||
/// | ||
/// | ||
/// # Example | ||
/// | ||
/// ```no_run | ||
/// # use clap::{App, Arg}; | ||
/// App::new("myapp") | ||
/// .help("myapp v1.0\n\ | ||
/// Does awesome things\n\ | ||
/// (C) [email protected]\n\n\ | ||
/// | ||
/// USAGE: myapp <opts> <comamnd>\n\n\ | ||
/// | ||
/// Options:\n\ | ||
/// -h, --helpe Dispay this message\n\ | ||
/// -V, --version Display version info\n\ | ||
/// -s <stuff> Do something with stuff\n\ | ||
/// -v Be verbose\n\n\ | ||
/// | ||
/// Commmands:\n\ | ||
/// help Prints this message\n\ | ||
/// work Do some work") | ||
/// # ; | ||
/// ``` | ||
pub fn help(mut self, h: &'u str) -> App<'a, 'v, 'ab, 'u, 'h, 'ar> { | ||
self.help_str = Some(h); | ||
self | ||
} | ||
|
||
/// Sets the short version of the `help` argument without the preceding `-`. | ||
/// | ||
/// By default `clap` automatically assigns `h`, but this can be overridden | ||
|
@@ -1161,6 +1202,10 @@ impl<'a, 'v, 'ab, 'u, 'h, 'ar> App<'a, 'v, 'ab, 'u, 'h, 'ar>{ | |
|
||
// Prints the full help message to the user | ||
fn print_help(&self) { | ||
if let Some(h) = self.help_str { | ||
println!("{}", h); | ||
return | ||
} | ||
self.print_version(false); | ||
let flags = !self.flags.is_empty(); | ||
let pos = !self.positionals_idx.is_empty(); | ||
|