Skip to content

Commit b47f8da

Browse files
authoredDec 4, 2023
Merge pull request #5247 from epage/group
feat: Add Command::mut_group
2 parents 2e7c9d1 + 37917be commit b47f8da

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed
 

‎clap_builder/src/builder/command.rs

+39
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,45 @@ impl Command {
298298
self
299299
}
300300

301+
/// Allows one to mutate an [`ArgGroup`] after it's been added to a [`Command`].
302+
///
303+
/// # Panics
304+
///
305+
/// If the argument is undefined
306+
///
307+
/// # Examples
308+
///
309+
/// ```rust
310+
/// # use clap_builder as clap;
311+
/// # use clap::{Command, arg, ArgGroup};
312+
///
313+
/// Command::new("foo")
314+
/// .arg(arg!(--"set-ver" <ver> "set the version manually").required(false))
315+
/// .arg(arg!(--major "auto increase major"))
316+
/// .arg(arg!(--minor "auto increase minor"))
317+
/// .arg(arg!(--patch "auto increase patch"))
318+
/// .group(ArgGroup::new("vers")
319+
/// .args(["set-ver", "major", "minor","patch"])
320+
/// .required(true))
321+
/// .mut_group("vers", |a| a.required(false));
322+
/// ```
323+
#[must_use]
324+
#[cfg_attr(debug_assertions, track_caller)]
325+
pub fn mut_group<F>(mut self, arg_id: impl AsRef<str>, f: F) -> Self
326+
where
327+
F: FnOnce(ArgGroup) -> ArgGroup,
328+
{
329+
let id = arg_id.as_ref();
330+
let index = self
331+
.groups
332+
.iter()
333+
.position(|g| g.get_id() == id)
334+
.unwrap_or_else(|| panic!("Group `{id}` is undefined"));
335+
let a = self.groups.remove(index);
336+
337+
self.groups.push(f(a));
338+
self
339+
}
301340
/// Allows one to mutate a [`Command`] after it's been added as a subcommand.
302341
///
303342
/// This can be useful for modifying auto-generated arguments of nested subcommands with

0 commit comments

Comments
 (0)