pub struct Group {
pub command: Command,
pub commands: HashMap<String, Arc<dyn CommandLike>>,
pub chain: bool,
pub invoke_without_command: bool,
pub result_callback: Option<ResultCallback>,
pub subcommand_required: bool,
pub subcommand_metavar: String,
/* private fields */
}Expand description
A command group that contains and dispatches to subcommands.
Groups are the primary way to organize CLI applications with multiple commands. They can be nested to create complex command hierarchies.
§Features
- Subcommand registration and dispatch
- Optional group callback (runs before subcommand)
- Chain mode for executing multiple subcommands
- Automatic help generation with command listing
§Example
use click::group::Group;
use click::command::Command;
let cli = Group::new("myapp")
.help("My application")
.invoke_without_command(true)
.callback(|_ctx| {
println!("No subcommand provided");
Ok(())
})
.command(
Command::new("hello")
.help("Say hello")
.build()
)
.build();Fields§
§command: CommandThe underlying command (for the group’s own options/arguments).
commands: HashMap<String, Arc<dyn CommandLike>>Registered subcommands (name -> Command or Group).
chain: boolWhether to execute multiple subcommands in sequence.
invoke_without_command: boolWhether to invoke the group callback even if no subcommand is provided.
result_callback: Option<ResultCallback>Optional callback to process subcommand results.
subcommand_required: boolWhether a subcommand is required (error if not provided).
Defaults to true unless invoke_without_command is true.
subcommand_metavar: StringThe metavar to show for subcommands in usage.
Implementations§
Source§impl Group
impl Group
Sourcepub fn new(name: &str) -> GroupBuilder
pub fn new(name: &str) -> GroupBuilder
Create a new group builder with the given name.
§Example
use click::group::Group;
let group = Group::new("mygroup")
.help("My command group")
.build();Sourcepub fn add_command(
&mut self,
cmd: impl CommandLike + 'static,
name: Option<&str>,
)
pub fn add_command( &mut self, cmd: impl CommandLike + 'static, name: Option<&str>, )
Add a subcommand to this group.
If name is provided, it overrides the command’s own name.
§Example
use click::group::Group;
use click::command::Command;
let mut group = Group::new("cli").build();
group.add_command(Command::new("hello").build(), None);
group.add_command(Command::new("greet").build(), Some("hi")); // registered as "hi"
assert!(group.get_command("hello").is_some());
assert!(group.get_command("hi").is_some());
assert!(group.get_command("greet").is_none()); // not found by original nameAdd a subcommand to this group using a shared command object.
This makes it possible to register the same command under multiple names (aliases) while still being able to query alias metadata.
Sourcepub fn get_command(&self, name: &str) -> Option<&dyn CommandLike>
pub fn get_command(&self, name: &str) -> Option<&dyn CommandLike>
Get a subcommand by name.
§Example
use click::group::Group;
use click::command::Command;
let group = Group::new("cli")
.command(Command::new("hello").build())
.build();
assert!(group.get_command("hello").is_some());
assert!(group.get_command("unknown").is_none());Sourcepub fn list_command_entries(&self) -> Vec<(String, &dyn CommandLike)>
pub fn list_command_entries(&self) -> Vec<(String, &dyn CommandLike)>
List all command registrations as (registered_name, command) pairs.
This is useful when you need both the registered key and the canonical name
stored inside the command itself (command.name()).
Sourcepub fn list_command_aliases(&self, name: &str) -> Vec<String>
pub fn list_command_aliases(&self, name: &str) -> Vec<String>
List other registered names (aliases) that map to the same command.
Returns an empty list if the command is not found or if it has no aliases.
Sourcepub fn list_commands(&self) -> Vec<&str>
pub fn list_commands(&self) -> Vec<&str>
List all subcommand names (sorted alphabetically).
§Example
use click::group::Group;
use click::command::Command;
let group = Group::new("cli")
.command(Command::new("build").build())
.command(Command::new("init").build())
.command(Command::new("deploy").build())
.build();
let commands = group.list_commands();
assert_eq!(commands, vec!["build", "deploy", "init"]);Sourcepub fn resolve_command<'a>(
&'a self,
ctx: &Context,
args: &[String],
) -> Result<Option<(&'a str, &'a dyn CommandLike, Vec<String>)>, ClickError>
pub fn resolve_command<'a>( &'a self, ctx: &Context, args: &[String], ) -> Result<Option<(&'a str, &'a dyn CommandLike, Vec<String>)>, ClickError>
Resolve a command from arguments.
Returns the command name, the command, and the remaining arguments. Returns an error if the command is not found (unless in resilient parsing mode).
§Arguments
ctx- The current contextargs- The arguments to parse
Sourcepub fn format_commands(&self, _ctx: &Context) -> String
pub fn format_commands(&self, _ctx: &Context) -> String
Format the commands section for help output.
Returns a formatted string listing all visible subcommands.