Skip to content

Commit

Permalink
feat: can now specify that an app or subcommand should display help o…
Browse files Browse the repository at this point in the history
…n no args or subcommands

Closes #133
  • Loading branch information
kbknapp committed May 26, 2015
1 parent 42560e5 commit 29ca7b2
Showing 1 changed file with 55 additions and 2 deletions.
57 changes: 55 additions & 2 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,9 @@ pub struct App<'a, 'v, 'ab, 'u, 'h, 'ar> {
usage: Option<String>,
groups: HashMap<&'ar str, ArgGroup<'ar, 'ar>>,
global_args: Vec<Arg<'ar, 'ar, 'ar, 'ar, 'ar, 'ar>>,
no_sc_error: bool
no_sc_error: bool,
help_on_no_args: bool,
help_on_no_sc: bool
}

impl<'a, 'v, 'ab, 'u, 'h, 'ar> App<'a, 'v, 'ab, 'u, 'h, 'ar>{
Expand Down Expand Up @@ -162,7 +164,9 @@ impl<'a, 'v, 'ab, 'u, 'h, 'ar> App<'a, 'v, 'ab, 'u, 'h, 'ar>{
groups: HashMap::new(),
subcmds_neg_reqs: false,
global_args: vec![],
no_sc_error: false
no_sc_error: false,
help_on_no_args: false,
help_on_no_sc: false
}
}

Expand Down Expand Up @@ -312,6 +316,48 @@ impl<'a, 'v, 'ab, 'u, 'h, 'ar> App<'a, 'v, 'ab, 'u, 'h, 'ar>{
self
}

/// Specifies that the help text sould be displayed (and then exit gracefully), if no
/// arguments are present at runtime (i.e. an empty run such as, `$ myprog`.
///
/// *NOTE:* Subcommands count as arguments
///
///
/// # Example
///
/// ```no_run
/// # use clap::{App, Arg};
/// # let app = App::new("myprog")
/// .arg_required_else_help(true)
/// # .get_matches();
/// ```
pub fn arg_required_else_help(mut self, tf: bool) -> App<'a, 'v, 'ab, 'u, 'h, 'ar> {
self.help_on_no_args = tf;
self
}

/// Specifies that the help text sould be displayed (and then exit gracefully), if no
/// subcommands are present at runtime (i.e. an empty run such as, `$ myprog`.
///
/// *NOTE:* This should *not* be used with `.subcommand_required()` as they do the same thing,
/// except one prints the help text, and one prints an error.
///
/// *NOTE:* If the user specifies arguments at runtime, but no subcommand the help text will
/// still be displayed and exit. If this is *not* the desired result, consider using
/// `.arg_required_else_help()`
///
/// # Example
///
/// ```no_run
/// # use clap::{App, Arg};
/// # let app = App::new("myprog")
/// .subcommand_required_else_help(true)
/// # .get_matches();
/// ```
pub fn subcommand_required_else_help(mut self, tf: bool) -> App<'a, 'v, 'ab, 'u, 'h, 'ar> {
self.help_on_no_sc = tf;
self
}

/// Adds an argument to the list of valid possibilties manually. This method allows you full
/// control over the arguments settings and options (as well as dynamic generation). It also
/// allows you specify several more advanced configuration options such as relational rules
Expand Down Expand Up @@ -1721,6 +1767,9 @@ impl<'a, 'v, 'ab, 'u, 'h, 'ar> App<'a, 'v, 'ab, 'u, 'h, 'ar>{
println!("USAGE:\n\t{} [SUBCOMMAND]\n\nFor more information re-run with {} or \
'{}'", &bn[..], Format::Good("--help"), Format::Good("help"));
self.exit(1);
} else if self.help_on_no_sc {
self.print_help();
self.exit(1);
}
if !self.required.is_empty() && !self.subcmds_neg_reqs {
if self.validate_required(&matches) {
Expand All @@ -1737,6 +1786,10 @@ impl<'a, 'v, 'ab, 'u, 'h, 'ar> App<'a, 'v, 'ab, 'u, 'h, 'ar>{
Some(matches.args.keys().map(|k| *k).collect()));
}
}
if matches.args.is_empty() && matches.subcommand_name().is_none() && self.help_on_no_args {
self.print_help();
self.exit(1);
}
}

fn blacklisted_from(&self, name: &str, matches: &ArgMatches) -> Option<String> {
Expand Down

0 comments on commit 29ca7b2

Please sign in to comment.