DevC DevC - 10 days ago 9
Python Question

click no such command found

I am trying click (Command line interface package for Python), while running the following code I get error

Error: No such command "abcd"


@click.group()
@click.option('--source', required=True)
@click.pass_context
def cli(ctx, source):
ctx.obj = "pass it"

@cli.command()
@click.argument('abcd')
@click.pass_context
def hello(ctx, abcd):
click.echo("Hello, World")


if __name__ == '__main__':
cli()


I am running it as follows

python playclick.py --source this abcd

Answer

"abcd" is being treated as a separate command because of the space (this is a characteristic of your shell, not of click specifically).

If you want the value of source to be "this abcd", use quotes:

python playclick.py --source "this abcd"

To actually provide the abcd argument, you need to call the hello command – the argument is for that command:

python playclick.py --source this hello 123456

The hello command will have an argument of 123456.

Breaking down the entire line:

--source this provides the source argument to the main cli command. hello is the command to run (try python playclick.py --source this and you'll get an error because there is no command), and 123456 is the argument named abcd to that command.