Skip to content

Commit bc66d3c

Browse files
committed
feat: allows setting version number to auto-propagate through subcommands
The version number can now be auto-propgated down through subcommands with the App::global_version(true) method Closes #157
1 parent 8889689 commit bc66d3c

File tree

1 file changed

+34
-8
lines changed

1 file changed

+34
-8
lines changed

src/app.rs

+34-8
Original file line numberDiff line numberDiff line change
@@ -99,12 +99,8 @@ pub struct App<'a, 'v, 'ab, 'u, 'h, 'ar> {
9999
positionals_name: HashMap<&'ar str, u8>,
100100
// A list of subcommands
101101
subcommands: BTreeMap<String, App<'a, 'v, 'ab, 'u, 'h, 'ar>>,
102-
needs_long_help: bool,
103-
needs_long_version: bool,
104102
help_short: Option<char>,
105103
version_short: Option<char>,
106-
needs_subcmd_help: bool,
107-
subcmds_neg_reqs: bool,
108104
required: HashSet<&'ar str>,
109105
short_list: HashSet<char>,
110106
long_list: HashSet<&'ar str>,
@@ -114,11 +110,16 @@ pub struct App<'a, 'v, 'ab, 'u, 'h, 'ar> {
114110
usage: Option<String>,
115111
groups: HashMap<&'ar str, ArgGroup<'ar, 'ar>>,
116112
global_args: Vec<Arg<'ar, 'ar, 'ar, 'ar, 'ar, 'ar>>,
113+
help_str: Option<&'u str>,
117114
no_sc_error: bool,
118115
wait_on_error: bool,
119116
help_on_no_args: bool,
120-
help_str: Option<&'u str>,
121-
help_on_no_sc: bool
117+
needs_long_help: bool,
118+
needs_long_version: bool,
119+
needs_subcmd_help: bool,
120+
subcmds_neg_reqs: bool,
121+
help_on_no_sc: bool,
122+
global_ver: bool,
122123
}
123124

124125
impl<'a, 'v, 'ab, 'u, 'h, 'ar> App<'a, 'v, 'ab, 'u, 'h, 'ar>{
@@ -166,7 +167,8 @@ impl<'a, 'v, 'ab, 'u, 'h, 'ar> App<'a, 'v, 'ab, 'u, 'h, 'ar>{
166167
help_str: None,
167168
wait_on_error: false,
168169
help_on_no_args: false,
169-
help_on_no_sc: false
170+
help_on_no_sc: false,
171+
global_ver: false,
170172
}
171173
}
172174

@@ -421,6 +423,27 @@ impl<'a, 'v, 'ab, 'u, 'h, 'ar> App<'a, 'v, 'ab, 'u, 'h, 'ar>{
421423
self
422424
}
423425

426+
/// Uses version of the current command for all subcommands.
427+
///
428+
/// **NOTE:** The version for the current command must be set **prior** adding any subcommands
429+
///
430+
/// # Example
431+
///
432+
/// ```no_run
433+
/// # use clap::{App, Arg, SubCommand};
434+
/// App::new("myprog")
435+
/// .version("v1.1")
436+
/// .global_version(true)
437+
/// .subcommand(SubCommand::with_name("test"))
438+
/// .get_matches();
439+
/// // running `myprog test --version` will display
440+
/// // "myprog-test v1.1"
441+
/// ```
442+
pub fn global_version(mut self, gv: bool) -> App<'a, 'v, 'ab, 'u, 'h, 'ar> {
443+
self.global_ver = gv;
444+
self
445+
}
446+
424447
/// Will display a message "Press [ENTER]/[RETURN] to continue..." and wait user before
425448
/// exiting
426449
///
@@ -927,9 +950,12 @@ impl<'a, 'v, 'ab, 'u, 'h, 'ar> App<'a, 'v, 'ab, 'u, 'h, 'ar>{
927950
/// // Additional subcommand configuration goes here, such as other arguments...
928951
/// # ;
929952
/// ```
930-
pub fn subcommand(mut self, subcmd: App<'a, 'v, 'ab, 'u, 'h, 'ar>)
953+
pub fn subcommand(mut self, mut subcmd: App<'a, 'v, 'ab, 'u, 'h, 'ar>)
931954
-> App<'a, 'v, 'ab, 'u, 'h, 'ar> {
932955
if subcmd.name == "help" { self.needs_subcmd_help = false; }
956+
if self.global_ver && subcmd.version.is_none() && self.version.is_some() {
957+
subcmd.version = Some(self.version.unwrap());
958+
}
933959
self.subcommands.insert(subcmd.name.clone(), subcmd);
934960
self
935961
}

0 commit comments

Comments
 (0)