click-default-group
click-default-group is an extension for the Click command-line interface framework that allows you to specify a default subcommand for a Click group. This means if a user invokes the group without any arguments, the default command will be executed. The current version is 1.2.4, and as a stable Click extension, it typically has an infrequent release cadence.
Warnings
- gotcha When using `DefaultGroup`, ensure you set `default_if_no_args=True` in the `@click.group` decorator if you want the default command to run when the group is invoked without any arguments. Otherwise, the group will simply display its help message if no subcommand is given, which often defeats the purpose of a default command.
- gotcha `click-default-group` relies on Click's internal API for group behavior. While generally stable, ensure compatibility by pinning your `click` dependency to a known working version range, especially when upgrading Click to a new major version, to avoid potential unexpected behavior.
- deprecated Although `click-default-group` theoretically supports Python 2.7 according to its metadata, Python 2.7 is long End-of-Life and unsupported. It is strongly recommended to use Python 3.7+ for all new development and to ensure all dependencies are also Python 3 compatible.
Install
-
pip install click-default-group
Imports
- DefaultGroup
from click_default_group import DefaultGroup
Quickstart
import click
from click_default_group import DefaultGroup
@click.group(cls=DefaultGroup, default_if_no_args=True)
def cli():
"""A simple CLI with a default command."""
pass
@cli.command()
def foo():
click.echo("Running 'foo' command")
@cli.command(default=True)
def bar():
click.echo("Running 'bar' (default) command")
if __name__ == '__main__':
# Test cases:
# cli() should run 'bar'
# cli(['foo']) should run 'foo'
# cli(['bar']) should run 'bar'
print("\n--- Invoking CLI without arguments (should run 'bar') ---")
cli.main(args=[], standalone_mode=False)
print("\n--- Invoking CLI with 'foo' argument ---")
cli.main(args=['foo'], standalone_mode=False)
print("\n--- Invoking CLI with 'bar' argument ---")
cli.main(args=['bar'], standalone_mode=False)