Skip to content

Commit

Permalink
feat: allows stating all subcommands should *not* have --version flags
Browse files Browse the repository at this point in the history
Allows subcommands to disable the `--version` flag by using the
`App::versionless_subcommands(true)` method.

Closes #156
  • Loading branch information
kbknapp committed Jul 16, 2015
1 parent bc66d3c commit 336c476
Showing 1 changed file with 36 additions and 4 deletions.
40 changes: 36 additions & 4 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,8 @@ pub struct App<'a, 'v, 'ab, 'u, 'h, 'ar> {
subcmds_neg_reqs: bool,
help_on_no_sc: bool,
global_ver: bool,
// None = not set, Some(true) set for all children, Some(false) = disable version
versionless_scs: Option<bool>
}

impl<'a, 'v, 'ab, 'u, 'h, 'ar> App<'a, 'v, 'ab, 'u, 'h, 'ar>{
Expand Down Expand Up @@ -169,6 +171,7 @@ impl<'a, 'v, 'ab, 'u, 'h, 'ar> App<'a, 'v, 'ab, 'u, 'h, 'ar>{
help_on_no_args: false,
help_on_no_sc: false,
global_ver: false,
versionless_scs: None
}
}

Expand Down Expand Up @@ -409,7 +412,6 @@ impl<'a, 'v, 'ab, 'u, 'h, 'ar> App<'a, 'v, 'ab, 'u, 'h, 'ar>{
///
/// **NOTE:** Subcommands count as arguments
///
///
/// # Example
///
/// ```no_run
Expand All @@ -423,9 +425,11 @@ impl<'a, 'v, 'ab, 'u, 'h, 'ar> App<'a, 'v, 'ab, 'u, 'h, 'ar>{
self
}

/// Uses version of the current command for all subcommands.
/// Uses version of the current command for all subcommands. (Defaults to false; subcommands
/// have independant version strings)
///
/// **NOTE:** The version for the current command must be set **prior** adding any subcommands
/// **NOTE:** The version for the current command and this setting must be set **prior** to
/// adding any subcommands
///
/// # Example
///
Expand All @@ -444,6 +448,29 @@ impl<'a, 'v, 'ab, 'u, 'h, 'ar> App<'a, 'v, 'ab, 'u, 'h, 'ar>{
self
}

/// Disables `-V` and `--version` for all subcommands (Defaults to false; subcommands have
/// version flags)
///
/// **NOTE:** This setting must be set **prior** adding any subcommands
///
/// **NOTE:** Do not set this value to false, it will have undesired results!
///
/// # Example
///
/// ```no_run
/// # use clap::{App, Arg, SubCommand};
/// App::new("myprog")
/// .version("v1.1")
/// .versionless_subcommands(true)
/// .subcommand(SubCommand::with_name("test"))
/// .get_matches();
/// // running `myprog test --version` will display unknown argument error
/// ```
pub fn versionless_subcommands(mut self, vers: bool) -> App<'a, 'v, 'ab, 'u, 'h, 'ar> {
self.versionless_scs = Some(vers);
self
}

/// Will display a message "Press [ENTER]/[RETURN] to continue..." and wait user before
/// exiting
///
Expand Down Expand Up @@ -953,6 +980,9 @@ impl<'a, 'v, 'ab, 'u, 'h, 'ar> App<'a, 'v, 'ab, 'u, 'h, 'ar>{
pub fn subcommand(mut self, mut subcmd: App<'a, 'v, 'ab, 'u, 'h, 'ar>)
-> App<'a, 'v, 'ab, 'u, 'h, 'ar> {
if subcmd.name == "help" { self.needs_subcmd_help = false; }
if self.versionless_scs.is_some() && self.versionless_scs.unwrap() {
subcmd.versionless_scs = Some(false);
}
if self.global_ver && subcmd.version.is_none() && self.version.is_some() {
subcmd.version = Some(self.version.unwrap());
}
Expand Down Expand Up @@ -2024,7 +2054,9 @@ impl<'a, 'v, 'ab, 'u, 'h, 'ar> App<'a, 'v, 'ab, 'u, 'h, 'ar>{
self.long_list.insert("help");
self.flags.insert("hclap_help", arg);
}
if self.needs_long_version {
if self.needs_long_version
&& self.versionless_scs.is_some()
&& (self.versionless_scs.unwrap()) {
if self.version_short.is_none() && !self.short_list.contains(&'V') {
self.version_short = Some('V');
}
Expand Down

0 comments on commit 336c476

Please sign in to comment.