Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 45 additions & 0 deletions clap_builder/src/builder/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -386,6 +386,51 @@ impl Command {
self
}

/// Allows one to mutate all [`Command`]s after they've been added as subcommands.
///
/// This does not affect the built-in `--help` or `--version` arguments.
///
/// # Examples
///
#[cfg_attr(feature = "string", doc = "```")]
#[cfg_attr(not(feature = "string"), doc = "```ignore")]
/// # use clap_builder as clap;
/// # use clap::{Command, Arg, ArgAction};
///
/// let mut cmd = Command::new("foo")
/// .subcommands([
/// Command::new("fetch"),
/// Command::new("push"),
/// ])
/// // Allow title-case subcommands
/// .mut_subcommands(|sub| {
/// let name = sub.get_name();
/// let alias = name.chars().enumerate().map(|(i, c)| {
/// if i == 0 {
/// c.to_ascii_uppercase()
/// } else {
/// c
/// }
/// }).collect::<String>();
/// sub.alias(alias)
/// });
///
/// let res = cmd.try_get_matches_from_mut(vec!["foo", "fetch"]);
/// assert!(res.is_ok());
///
/// let res = cmd.try_get_matches_from_mut(vec!["foo", "Fetch"]);
/// assert!(res.is_ok());
/// ```
#[must_use]
#[cfg_attr(debug_assertions, track_caller)]
pub fn mut_subcommands<F>(mut self, f: F) -> Self
where
F: FnMut(Command) -> Command,
{
self.subcommands = self.subcommands.into_iter().map(f).collect();
self
}

/// Adds an [`ArgGroup`] to the application.
///
/// [`ArgGroup`]s are a family of related arguments.
Expand Down
Loading