click-contrib / click-default-group

Extends click.Group to invoke a command without explicit subcommand name.
BSD 3-Clause "New" or "Revised" License
73 stars 17 forks source link

How to provide `--version` option? #7

Closed ghost closed 6 years ago

ghost commented 6 years ago

Hi I'm looking for a way to use click-default-group while providing a cli --version option to get the cli version. Is there a way to get cli --version to print cli 0.0.1?

import click
from click_default_group import DefaultGroup

VERSION = 'cli 0.0.1'

@click.group(cls=DefaultGroup, default='foo', default_if_no_args=True)
@click.option('--version', is_flag=True, default=False)
def cli(version):
    click.echo(VERSION)

@cli.command()
def foo():
    click.echo('foo')

@cli.command()
def bar():
    click.echo('bar')

@cli.command('--version')
def cli_version():
    click.echo(VERSION)

cli()
$ python cli.py --version
Usage: cli.py [OPTIONS] COMMAND [ARGS]...

Error: Missing command.
ghost commented 6 years ago

invoke_without_command=True in

@click.group(cls=DefaultGroup, default='foo', default_if_no_args=True, invoke_without_command=True)

does the trick.