Search code examples
pythoncommand-line-interfacepython-click

Click module ignoring subgroup commands


Trying to implement the example from the palletsprojects website: https://click.palletsprojects.com/en/7.x/commands/

imoport click

@click.group()
@click.option('--debug/--no-debug', default=False)
def cli(debug):
    click.echo('Debug mode is %s' % ('on' if debug else 'off'))

@cli.command()  # @cli, not @click!
def sync():
    click.echo('Syncing')

The following lines produce no output in my terminal:

python cli_test.py cli
python cli_test.py sync
python cli_test.py

When I'd expect 'Syncing' to be printed for the second line.


Solution

  • You're (presumably from how I understand the library) supposed to invoke a master command after setting up the commands and groups. Append this to your code:

    if __name__ == '__main__':
        cli()
    

    And python cli_test.py sync should in turn call the sync command.