Error: Got unexpected extra arguments when using Click library

Question:

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
Asked By: Shinomoto Asakura

||

Answers:

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.

Answered By: larsks
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.