Reputation: 1542
I'm trying to use the Click library in Python to create a command line interface, but I keep getting the following error when I try to run my script:
Error: Got unexpected extra arguments (hello hello1)
Here is my code:
import click
@click.group(name='script')
def cli():
pass
@cli.command()
def hello1():
click.echo('Hello, World!')
@cli.command()
def hello2():
click.echo('Hola, Mundo!')
@cli.command()
@click.argument('function', type=click.Choice(['hello1', 'hello2']))
def hello(function):
if function == 'hello1':
hello1()
elif function == 'hello2':
hello2()
if __name__ == '__main__':
cli()
I'm trying to call the "hello" function with the argument "hello1" or "hello2", but it's not working. Can anyone help me figure out what's going wrong?
python script.py hello hello1
Upvotes: 0
Views: 824
Reputation: 311238
The way you've written your code, you shouldn't be defined hello1
and hello2
as commands. If you simply remove the decorator, your code works as written. That is, given:
import click
@click.group(name='script')
def cli():
pass
def hello1():
click.echo('Hello, World!')
def hello2():
click.echo('Hola, Mundo!')
@cli.command()
@click.argument('function', type=click.Choice(['hello1', 'hello2']))
def hello(function):
if function == 'hello1':
hello1()
elif function == 'hello2':
hello2()
if __name__ == '__main__':
cli()
We can run:
$ python script.py hello hello1
Hello, World!
$ python script.py hello hello2
Hola, Mundo!
If you want hello1
and hello2
to be subcommands of hello
, then you would need to redefine hello
as a command group, like this:
import click
@click.group(name='script')
def cli():
pass
@cli.group()
def hello():
pass
@hello.command()
def hello1():
click.echo('Hello, World!')
@hello.command()
def hello2():
click.echo('Hola, Mundo!')
if __name__ == '__main__':
cli()
This supports the same command lines, but instead of explicitly testing for an argument we rely on click
to dispatch the appropriate command.
Upvotes: 2