Skip to content

Commit

Permalink
feat(Help): allows one to fully override the auto-generated help message
Browse files Browse the repository at this point in the history
Allows overriding the entire help message so that nothing is
auto-generated

Closes #141
  • Loading branch information
kbknapp committed Jun 30, 2015
1 parent bf5fa2b commit 26d5ae3
Showing 1 changed file with 45 additions and 0 deletions.
45 changes: 45 additions & 0 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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();
Expand Down

0 comments on commit 26d5ae3

Please sign in to comment.