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.
Common errors
-
Error: Missing command.
cause This error occurs when you define a Click group with `click-default-group` and set a default command using `default='command_name'`, but you also include group-level options. When only group options are provided on the command line, `click-default-group` expects a subcommand, leading to the 'Missing command' error instead of executing the default command with the group options.fixTo fix this, you need to set `default_if_no_args=True` in the `@click.group` decorator, in addition to `default='command_name'`. This ensures that the default command is invoked when no subcommand is explicitly given, even if group options are present. ```python import click from click_default_group import DefaultGroup @click.group(cls=DefaultGroup, default='foo', default_if_no_args=True) @click.option('-v', is_flag=True, help='Enable verbose mode.') def cli(v): if v: click.echo('Verbose mode enabled for group.') pass @cli.command() def foo(): click.echo('Executing default command foo') @cli.command() def bar(): click.echo('Executing command bar') if __name__ == '__main__': cli() ``` -
ModuleNotFoundError: No module named 'click_default_group'
cause This error indicates that the `click-default-group` package, or specifically the `DefaultGroup` class within it, cannot be found by Python. This typically happens if the package has not been installed, or if there's an issue with the Python environment or path. Sometimes, a related package named `click-default-group-wheel` might be mentioned in dependency conflicts, suggesting a potential confusion or incomplete installation.fixEnsure the `click-default-group` package is correctly installed in your Python environment using pip: ```bash pip install click-default-group ``` If you are using a virtual environment, ensure it is activated before installation. If the issue persists, try reinstalling or upgrading it: ```bash pip install --upgrade click-default-group ``` -
AttributeError: 'Group' object has no attribute 'option'
cause This `AttributeError` can occur when attempting to apply `@click.option` directly to a Click group object, or when trying to register options in a way that conflicts with how `click-default-group` or standard Click expects options to be attached to commands or the group's callback function. Options are typically associated with commands or the group's main callback, not the group object itself in a way that causes this specific error.fixEnsure that `@click.option` decorators for group-level options are applied to the group's main function (the one decorated with `@click.group`), and options for subcommands are applied to their respective command functions. This error often arises from incorrectly structuring the decorators. ```python import click from click_default_group import DefaultGroup @click.group(cls=DefaultGroup, default='my_default_command') @click.option('--group-setting', default='default_value', help='An option for the group.') def cli(group_setting): click.echo(f'Group initialized with setting: {group_setting}') # Any logic for the group itself before a subcommand is called pass @cli.command() @click.option('--command-option', help='An option for this specific command.') def my_default_command(command_option): click.echo(f'Default command executed. Command option: {command_option}') @cli.command() def another_command(): click.echo('Another command executed.') if __name__ == '__main__': cli() ```
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)