Skip to content

Commit

Permalink
feat(Settings): one can now set an AppSetting which is propogated dow…
Browse files Browse the repository at this point in the history
…n through child subcommands

Closes #519
  • Loading branch information
kbknapp committed Jun 4, 2016
1 parent cd44080 commit e234183
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 0 deletions.
47 changes: 47 additions & 0 deletions src/app/mod.rs
Expand Up @@ -435,6 +435,53 @@ impl<'a, 'b> App<'a, 'b> {
self
}

/// Enables a single setting that is propogated *down* through all child [`SubCommand`]s.
///
/// See [`AppSettings`] for a full list of possibilities and examples.
///
/// **NOTE**: The setting is *only* propogated *down* and not up through parent commands.
///
/// # Examples
///
/// ```no_run
/// # use clap::{App, Arg, AppSettings};
/// App::new("myprog")
/// .global_setting(AppSettings::SubcommandRequired)
/// # ;
/// ```
/// [`SubCommand`]: ./struct.SubCommand.html
/// [`AppSettings`]: ./enum.AppSettings.html
pub fn global_setting(mut self, setting: AppSettings) -> Self {
self.p.set(setting);
self.p.g_settings.push(setting);
self
}

/// Enables multiple settings which are propogated *down* through all child [`SubCommand`]s.
///
/// See [`AppSettings`] for a full list of possibilities and examples.
///
/// **NOTE**: The setting is *only* propogated *down* and not up through parent commands.
///
/// # Examples
///
/// ```no_run
/// # use clap::{App, Arg, AppSettings};
/// App::new("myprog")
/// .global_settings(&[AppSettings::SubcommandRequired,
/// AppSettings::ColoredHelp])
/// # ;
/// ```
/// [`SubCommand`]: ./struct.SubCommand.html
/// [`AppSettings`]: ./enum.AppSettings.html
pub fn global_settings(mut self, settings: &[AppSettings]) -> Self {
for s in settings {
self.p.set(*s);
self.p.g_settings.push(*s)
}
self
}

/// Adds an [argument] to the list of valid possibilties.
///
/// # Examples
Expand Down
7 changes: 7 additions & 0 deletions src/app/parser.rs
Expand Up @@ -49,6 +49,7 @@ pub struct Parser<'a, 'b>
help_short: Option<char>,
version_short: Option<char>,
settings: AppFlags,
pub g_settings: Vec<AppSettings>,
pub meta: AppMeta<'b>,
}

Expand All @@ -68,6 +69,7 @@ impl<'a, 'b> Default for Parser<'a, 'b> {
groups: HashMap::new(),
global_args: vec![],
overrides: vec![],
g_settings: vec![],
settings: AppFlags::new(),
meta: AppMeta::new(),
}
Expand Down Expand Up @@ -217,6 +219,10 @@ impl<'a, 'b> Parser<'a, 'b>
if self.settings.is_set(AppSettings::DeriveDisplayOrder) {
subcmd.p.meta.disp_ord = self.subcommands.len();
}
for s in &self.g_settings {
subcmd.p.set(*s);
subcmd.p.g_settings.push(*s);
}
self.subcommands.push(subcmd);
}

Expand Down Expand Up @@ -1661,6 +1667,7 @@ impl<'a, 'b> Clone for Parser<'a, 'b>
help_short: self.help_short,
version_short: self.version_short,
settings: self.settings.clone(),
g_settings: self.g_settings.clone(),
meta: self.meta.clone(),
}
}
Expand Down

0 comments on commit e234183

Please sign in to comment.