click-default-group

1.2.4 · active · verified Thu Apr 09

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

Install

Imports

Quickstart

This example demonstrates how to create a `click` group using `DefaultGroup` and specify a default command (`bar`) that runs when the group is invoked without any subcommands. It includes `cli.main` calls for easy testing.

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)

view raw JSON →