@@ -274,6 +274,51 @@ impl<'help> App<'help> {
274
274
self
275
275
}
276
276
277
+ /// Allows one to mutate a [`Command`] after it's been added as a subcommand.
278
+ ///
279
+ /// This can be useful for modifying auto-generated arguments of nested subcommands with
280
+ /// [`Command::mut_arg`].
281
+ ///
282
+ /// # Examples
283
+ ///
284
+ /// ```rust
285
+ /// # use clap::Command;
286
+ ///
287
+ /// let mut cmd = Command::new("foo")
288
+ /// .subcommand(Command::new("bar"))
289
+ /// .mut_subcommand("bar", |subcmd| subcmd.disable_help_flag(true));
290
+ ///
291
+ /// let res = cmd.try_get_matches_from_mut(vec!["foo", "bar", "--help"]);
292
+ ///
293
+ /// // Since we disabled the help flag on the "bar" subcommand, this should err.
294
+ ///
295
+ /// assert!(res.is_err());
296
+ ///
297
+ /// let res = cmd.try_get_matches_from_mut(vec!["foo", "bar"]);
298
+ /// assert!(res.is_ok());
299
+ /// ```
300
+ #[ must_use]
301
+ pub fn mut_subcommand < T , F > ( mut self , name : T , f : F ) -> Self
302
+ where
303
+ F : FnOnce ( App < ' help > ) -> App < ' help > ,
304
+ T : Into < & ' help str > ,
305
+ {
306
+ let subcmd_name: & str = name. into ( ) ;
307
+ let pos = self
308
+ . subcommands
309
+ . iter ( )
310
+ . position ( |s| s. aliases_to ( subcmd_name) ) ;
311
+
312
+ let subcmd = if let Some ( idx) = pos {
313
+ self . subcommands . remove ( idx)
314
+ } else {
315
+ App :: new ( subcmd_name)
316
+ } ;
317
+
318
+ self . subcommands . push ( f ( subcmd) ) ;
319
+ self
320
+ }
321
+
277
322
/// Adds an [`ArgGroup`] to the application.
278
323
///
279
324
/// [`ArgGroup`]s are a family of related arguments.
0 commit comments