@@ -99,12 +99,8 @@ pub struct App<'a, 'v, 'ab, 'u, 'h, 'ar> {
99
99
positionals_name : HashMap < & ' ar str , u8 > ,
100
100
// A list of subcommands
101
101
subcommands : BTreeMap < String , App < ' a , ' v , ' ab , ' u , ' h , ' ar > > ,
102
- needs_long_help : bool ,
103
- needs_long_version : bool ,
104
102
help_short : Option < char > ,
105
103
version_short : Option < char > ,
106
- needs_subcmd_help : bool ,
107
- subcmds_neg_reqs : bool ,
108
104
required : HashSet < & ' ar str > ,
109
105
short_list : HashSet < char > ,
110
106
long_list : HashSet < & ' ar str > ,
@@ -114,11 +110,16 @@ pub struct App<'a, 'v, 'ab, 'u, 'h, 'ar> {
114
110
usage : Option < String > ,
115
111
groups : HashMap < & ' ar str , ArgGroup < ' ar , ' ar > > ,
116
112
global_args : Vec < Arg < ' ar , ' ar , ' ar , ' ar , ' ar , ' ar > > ,
113
+ help_str : Option < & ' u str > ,
117
114
no_sc_error : bool ,
118
115
wait_on_error : bool ,
119
116
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 ,
122
123
}
123
124
124
125
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>{
166
167
help_str : None ,
167
168
wait_on_error : false ,
168
169
help_on_no_args : false ,
169
- help_on_no_sc : false
170
+ help_on_no_sc : false ,
171
+ global_ver : false ,
170
172
}
171
173
}
172
174
@@ -421,6 +423,27 @@ impl<'a, 'v, 'ab, 'u, 'h, 'ar> App<'a, 'v, 'ab, 'u, 'h, 'ar>{
421
423
self
422
424
}
423
425
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
+
424
447
/// Will display a message "Press [ENTER]/[RETURN] to continue..." and wait user before
425
448
/// exiting
426
449
///
@@ -927,9 +950,12 @@ impl<'a, 'v, 'ab, 'u, 'h, 'ar> App<'a, 'v, 'ab, 'u, 'h, 'ar>{
927
950
/// // Additional subcommand configuration goes here, such as other arguments...
928
951
/// # ;
929
952
/// ```
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 > )
931
954
-> App < ' a , ' v , ' ab , ' u , ' h , ' ar > {
932
955
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
+ }
933
959
self . subcommands . insert ( subcmd. name . clone ( ) , subcmd) ;
934
960
self
935
961
}
0 commit comments