Skip to content

Commit

Permalink
feat(Global Args): allows args that propagate down to child commands
Browse files Browse the repository at this point in the history
Closes #131
  • Loading branch information
kbknapp committed May 22, 2015
1 parent 28b7385 commit 2bcc613
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 6 deletions.
22 changes: 20 additions & 2 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ pub struct App<'a, 'v, 'ab, 'u, 'h, 'ar> {
bin_name: Option<String>,
usage: Option<String>,
groups: HashMap<&'ar str, ArgGroup<'ar, 'ar>>,
global_args: Vec<Arg<'ar, 'ar, 'ar, 'ar, 'ar, 'ar>>,
no_sc_error: bool
}

Expand Down Expand Up @@ -161,6 +162,7 @@ impl<'a, 'v, 'ab, 'u, 'h, 'ar> App<'a, 'v, 'ab, 'u, 'h, 'ar>{
bin_name: None,
groups: HashMap::new(),
subcmds_neg_reqs: false,
global_args: vec![],
no_sc_error: false
}
}
Expand Down Expand Up @@ -419,6 +421,7 @@ impl<'a, 'v, 'ab, 'u, 'h, 'ar> App<'a, 'v, 'ab, 'u, 'h, 'ar>{
min_vals: a.min_vals,
max_vals: a.max_vals,
help: a.help,
global: a.global,
empty_vals: a.empty_vals
};
if pb.min_vals.is_some() && !pb.multiple {
Expand Down Expand Up @@ -470,11 +473,12 @@ impl<'a, 'v, 'ab, 'u, 'h, 'ar> App<'a, 'v, 'ab, 'u, 'h, 'ar>{
multiple: a.multiple,
blacklist: None,
help: a.help,
global: a.global,
possible_vals: None,
num_vals: a.num_vals,
min_vals: a.min_vals,
max_vals: a.max_vals,
val_names: a.val_names,
val_names: a.val_names.clone(),
requires: None,
required: a.required,
empty_vals: a.empty_vals
Expand Down Expand Up @@ -540,6 +544,7 @@ impl<'a, 'v, 'ab, 'u, 'h, 'ar> App<'a, 'v, 'ab, 'u, 'h, 'ar>{
long: a.long,
help: a.help,
blacklist: None,
global: a.global,
multiple: a.multiple,
requires: None,
};
Expand All @@ -560,6 +565,12 @@ impl<'a, 'v, 'ab, 'u, 'h, 'ar> App<'a, 'v, 'ab, 'u, 'h, 'ar>{
}
self.flags.insert(a.name, fb);
}
if a.global {
if a.required {
panic!("Global arguments cannot be required.\n\n\t'{}' is marked as global and required", a.name);
}
self.global_args.push(a);
}
self
}

Expand Down Expand Up @@ -749,9 +760,14 @@ impl<'a, 'v, 'ab, 'u, 'h, 'ar> App<'a, 'v, 'ab, 'u, 'h, 'ar>{
/// // Additional subcommand configuration goes here, such as other arguments...
/// # .get_matches();
/// ```
pub fn subcommand(mut self, subcmd: 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; }
{
while let Some(a) = self.global_args.pop() {
subcmd = subcmd.arg(a);
}
}
self.subcommands.insert(subcmd.name.clone(), subcmd);
self
}
Expand Down Expand Up @@ -1763,6 +1779,7 @@ impl<'a, 'v, 'ab, 'u, 'h, 'ar> App<'a, 'v, 'ab, 'u, 'h, 'ar>{
help: Some("Prints help information"),
blacklist: None,
multiple: false,
global: false,
requires: None,
};
if self.needs_short_help {
Expand All @@ -1780,6 +1797,7 @@ impl<'a, 'v, 'ab, 'u, 'h, 'ar> App<'a, 'v, 'ab, 'u, 'h, 'ar>{
help: Some("Prints version information"),
blacklist: None,
multiple: false,
global: false,
requires: None,
};
if self.needs_short_version {
Expand Down
8 changes: 6 additions & 2 deletions src/args/arg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ impl<'n, 'l, 'h, 'g, 'p, 'r> Arg<'n, 'l, 'h, 'g, 'p, 'r> {
val_names: None,
max_vals: None,
min_vals: None,
global: false,
empty_vals: true,
}
}
Expand Down Expand Up @@ -173,6 +174,7 @@ impl<'n, 'l, 'h, 'g, 'p, 'r> Arg<'n, 'l, 'h, 'g, 'p, 'r> {
max_vals: None,
val_names: None,
group: None,
global: false,
empty_vals: true
}
}
Expand Down Expand Up @@ -330,6 +332,7 @@ impl<'n, 'l, 'h, 'g, 'p, 'r> Arg<'n, 'l, 'h, 'g, 'p, 'r> {
max_vals: None,
min_vals: None,
group: None,
global: false,
empty_vals: true
}
}
Expand Down Expand Up @@ -646,8 +649,9 @@ impl<'n, 'l, 'h, 'g, 'p, 'r> Arg<'n, 'l, 'h, 'g, 'p, 'r> {
self
}

/// Specifies that an argument applies to and will be available to all subcommands, both
/// children, and parent commands.
/// Specifies that an argument applies to and will be available to all child subcommands.
///
/// **NOTE:** Global arguments *only* propagate down, **not** up (to parent commands)
///
/// **NOTE:** Global arguments *cannot* be required.
///
Expand Down
1 change: 1 addition & 0 deletions src/args/argbuilder/flag.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ pub struct FlagBuilder<'n> {
/// The short version (i.e. single character)
/// of the argument, no preceding `-`
pub short: Option<char>,
pub global: bool,
}

impl<'n> Display for FlagBuilder<'n> {
Expand Down
3 changes: 2 additions & 1 deletion src/args/argbuilder/option.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ pub struct OptBuilder<'n> {
pub min_vals: Option<u8>,
pub max_vals: Option<u8>,
pub val_names: Option<Vec<&'n str>>,
pub empty_vals: bool
pub empty_vals: bool,
pub global: bool,
}

impl<'n> Display for OptBuilder<'n> {
Expand Down
3 changes: 2 additions & 1 deletion src/args/argbuilder/positional.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ pub struct PosBuilder<'n> {
pub num_vals: Option<u8>,
pub max_vals: Option<u8>,
pub min_vals: Option<u8>,
pub empty_vals: bool
pub empty_vals: bool,
pub global: bool
}

impl<'n> Display for PosBuilder<'n> {
Expand Down

0 comments on commit 2bcc613

Please sign in to comment.