Skip to content

Commit e234183

Browse files
committed
feat(Settings): one can now set an AppSetting which is propogated down through child subcommands
Closes #519
1 parent cd44080 commit e234183

File tree

2 files changed

+54
-0
lines changed

2 files changed

+54
-0
lines changed

src/app/mod.rs

+47
Original file line numberDiff line numberDiff line change
@@ -435,6 +435,53 @@ impl<'a, 'b> App<'a, 'b> {
435435
self
436436
}
437437

438+
/// Enables a single setting that is propogated *down* through all child [`SubCommand`]s.
439+
///
440+
/// See [`AppSettings`] for a full list of possibilities and examples.
441+
///
442+
/// **NOTE**: The setting is *only* propogated *down* and not up through parent commands.
443+
///
444+
/// # Examples
445+
///
446+
/// ```no_run
447+
/// # use clap::{App, Arg, AppSettings};
448+
/// App::new("myprog")
449+
/// .global_setting(AppSettings::SubcommandRequired)
450+
/// # ;
451+
/// ```
452+
/// [`SubCommand`]: ./struct.SubCommand.html
453+
/// [`AppSettings`]: ./enum.AppSettings.html
454+
pub fn global_setting(mut self, setting: AppSettings) -> Self {
455+
self.p.set(setting);
456+
self.p.g_settings.push(setting);
457+
self
458+
}
459+
460+
/// Enables multiple settings which are propogated *down* through all child [`SubCommand`]s.
461+
///
462+
/// See [`AppSettings`] for a full list of possibilities and examples.
463+
///
464+
/// **NOTE**: The setting is *only* propogated *down* and not up through parent commands.
465+
///
466+
/// # Examples
467+
///
468+
/// ```no_run
469+
/// # use clap::{App, Arg, AppSettings};
470+
/// App::new("myprog")
471+
/// .global_settings(&[AppSettings::SubcommandRequired,
472+
/// AppSettings::ColoredHelp])
473+
/// # ;
474+
/// ```
475+
/// [`SubCommand`]: ./struct.SubCommand.html
476+
/// [`AppSettings`]: ./enum.AppSettings.html
477+
pub fn global_settings(mut self, settings: &[AppSettings]) -> Self {
478+
for s in settings {
479+
self.p.set(*s);
480+
self.p.g_settings.push(*s)
481+
}
482+
self
483+
}
484+
438485
/// Adds an [argument] to the list of valid possibilties.
439486
///
440487
/// # Examples

src/app/parser.rs

+7
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ pub struct Parser<'a, 'b>
4949
help_short: Option<char>,
5050
version_short: Option<char>,
5151
settings: AppFlags,
52+
pub g_settings: Vec<AppSettings>,
5253
pub meta: AppMeta<'b>,
5354
}
5455

@@ -68,6 +69,7 @@ impl<'a, 'b> Default for Parser<'a, 'b> {
6869
groups: HashMap::new(),
6970
global_args: vec![],
7071
overrides: vec![],
72+
g_settings: vec![],
7173
settings: AppFlags::new(),
7274
meta: AppMeta::new(),
7375
}
@@ -217,6 +219,10 @@ impl<'a, 'b> Parser<'a, 'b>
217219
if self.settings.is_set(AppSettings::DeriveDisplayOrder) {
218220
subcmd.p.meta.disp_ord = self.subcommands.len();
219221
}
222+
for s in &self.g_settings {
223+
subcmd.p.set(*s);
224+
subcmd.p.g_settings.push(*s);
225+
}
220226
self.subcommands.push(subcmd);
221227
}
222228

@@ -1661,6 +1667,7 @@ impl<'a, 'b> Clone for Parser<'a, 'b>
16611667
help_short: self.help_short,
16621668
version_short: self.version_short,
16631669
settings: self.settings.clone(),
1670+
g_settings: self.g_settings.clone(),
16641671
meta: self.meta.clone(),
16651672
}
16661673
}

0 commit comments

Comments
 (0)