From 37917be0b75d5cd667cd37db6ea5c6bac837c674 Mon Sep 17 00:00:00 2001 From: Ed Page Date: Mon, 4 Dec 2023 12:03:05 -0600 Subject: [PATCH] feat: Add Command::mut_group Fixes #5038 --- clap_builder/src/builder/command.rs | 39 +++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/clap_builder/src/builder/command.rs b/clap_builder/src/builder/command.rs index a48cb8ccfe5..6e56b183411 100644 --- a/clap_builder/src/builder/command.rs +++ b/clap_builder/src/builder/command.rs @@ -298,6 +298,45 @@ impl Command { self } + /// Allows one to mutate an [`ArgGroup`] after it's been added to a [`Command`]. + /// + /// # Panics + /// + /// If the argument is undefined + /// + /// # Examples + /// + /// ```rust + /// # use clap_builder as clap; + /// # use clap::{Command, arg, ArgGroup}; + /// + /// Command::new("foo") + /// .arg(arg!(--"set-ver" "set the version manually").required(false)) + /// .arg(arg!(--major "auto increase major")) + /// .arg(arg!(--minor "auto increase minor")) + /// .arg(arg!(--patch "auto increase patch")) + /// .group(ArgGroup::new("vers") + /// .args(["set-ver", "major", "minor","patch"]) + /// .required(true)) + /// .mut_group("vers", |a| a.required(false)); + /// ``` + #[must_use] + #[cfg_attr(debug_assertions, track_caller)] + pub fn mut_group(mut self, arg_id: impl AsRef, f: F) -> Self + where + F: FnOnce(ArgGroup) -> ArgGroup, + { + let id = arg_id.as_ref(); + let index = self + .groups + .iter() + .position(|g| g.get_id() == id) + .unwrap_or_else(|| panic!("Group `{id}` is undefined")); + let a = self.groups.remove(index); + + self.groups.push(f(a)); + self + } /// Allows one to mutate a [`Command`] after it's been added as a subcommand. /// /// This can be useful for modifying auto-generated arguments of nested subcommands with