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

Missing command error when providing only group options #17

Open edobez opened 3 years ago

edobez commented 3 years ago

As title says, a missing command error is shown when only group options are passed to the prompt.

The expected behavior is that group options are handled by the group and then default command is invoked.

Minimum example

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)
def cli(v):
    print('cli group exec')
    if v:
        print('Verbose!')

@cli.command()
@click.option('--config')
def foo(config):
    print('foo command exec')
    if config:
        print(f'config={config}')

Execution

$ program foo                           
--> normal call, works

$ program
--> default cmd is called, works

$ program --config=abc 
--> option is forwarded to 'foo' cmd, works

$ program -v --config=abc
--> '-v' option is parsed by group and '--config' is parsed by 'foo' cmd, works

$ program -v
--> doesn't work!
edobez commented 3 years ago

I forked and created a branch with (refactored) tests covering this behavior. I looked a bit into it but didn't find a proper solution yet.

pyhedgehog commented 2 years ago

I've pushed fix #23. Hope this patch is in line with plans of author. @sublee