@@ -109,6 +109,7 @@ pub struct App<'a, 'v, 'ab, 'u, 'h, 'ar> {
109
109
needs_short_help : bool ,
110
110
needs_short_version : bool ,
111
111
needs_subcmd_help : bool ,
112
+ subcmds_neg_reqs : bool ,
112
113
required : HashSet < & ' ar str > ,
113
114
short_list : HashSet < char > ,
114
115
long_list : HashSet < & ' ar str > ,
@@ -156,6 +157,7 @@ impl<'a, 'v, 'ab, 'u, 'h, 'ar> App<'a, 'v, 'ab, 'u, 'h, 'ar>{
156
157
blacklist : HashSet :: new ( ) ,
157
158
bin_name : None ,
158
159
groups : HashMap :: new ( ) ,
160
+ subcmds_neg_reqs : false
159
161
}
160
162
}
161
163
@@ -209,6 +211,25 @@ impl<'a, 'v, 'ab, 'u, 'h, 'ar> App<'a, 'v, 'ab, 'u, 'h, 'ar>{
209
211
self
210
212
}
211
213
214
+ /// Allows subcommands to override all requirements of the parent (this command). For example
215
+ /// if you had a subcommand or even top level application which had a required arguments that
216
+ /// is only required if no subcommand is used.
217
+ ///
218
+ /// **NOTE:** This defaults to false (using subcommand does *not* negate requirements)
219
+ ///
220
+ /// # Example
221
+ ///
222
+ /// ```no_run
223
+ /// # use clap::App;
224
+ /// # let app = App::new("myprog")
225
+ /// .subcommands_negate_reqs(true)
226
+ /// # .get_matches();
227
+ /// ```
228
+ pub fn subcommands_negate_reqs ( mut self , n : bool ) -> App < ' a , ' v , ' ab , ' u , ' h , ' ar > {
229
+ self . subcmds_neg_reqs = n;
230
+ self
231
+ }
232
+
212
233
/// Sets a string of the version number to be displayed when displaying version or help
213
234
/// information.
214
235
///
@@ -1602,7 +1623,7 @@ impl<'a, 'v, 'ab, 'u, 'h, 'ar> App<'a, 'v, 'ab, 'u, 'h, 'ar>{
1602
1623
self . validate_blacklist ( matches) ;
1603
1624
self . validate_num_args ( matches) ;
1604
1625
1605
- if !self . required . is_empty ( ) {
1626
+ if !self . required . is_empty ( ) && ! self . subcmds_neg_reqs {
1606
1627
if self . validate_required ( & matches) {
1607
1628
self . report_error ( format ! ( "The following required arguments were not \
1608
1629
supplied:{}",
0 commit comments