Click: "Got unexpected extra arguments" when passing string
Asked Answered
A

4

22
import click

@cli.command()
@click.argument("namespace", nargs=1)
def process(namespace):
.....

@cli.command()
def run():
    for namespace in KEYS.iterkeys():
        process(namespace)

Running run('some string') produces:

Error: Got unexpected extra arguments (o m e s t r i n g)

As if Click passes string argument by one character. Printing an argument shows correct result.

PS: KEYS dictionary defined and working as expected.

Anybody answered 20/1, 2016 at 15:41 Comment(0)
A
18

Figured this out. Instead of just calling a function, I must pass a context and call it from there:

@cli.command()
@click.pass_context
def run():
    for namespace in KEYS.iterkeys():
        ctx.invoke(process, namespace=namespace)

From the docs:

Sometimes, it might be interesting to invoke one command from another command. This is a pattern that is generally discouraged with Click, but possible nonetheless. For this, you can use the Context.invoke() or Context.forward() methods.

They work similarly, but the difference is that Context.invoke() merely invokes another command with the arguments you provide as a caller, whereas Context.forward() fills in the arguments from the current command. Both accept the command as the first argument and everything else is passed onwards as you would expect.

Example:

cli = click.Group()

@cli.command()
@click.option('--count', default=1)
def test(count):
    click.echo('Count: %d' % count)

@cli.command()
@click.option('--count', default=1)
@click.pass_context
def dist(ctx, count):
    ctx.forward(test)
    ctx.invoke(test, count=42)

And what it looks like:

$ cli dist
Count: 1
Count: 42
Anybody answered 20/1, 2016 at 16:12 Comment(0)
S
4

tl;dr Remove whitespace from your arguments.

If anyone else is going through the same and above answer does not clarify or work for you probably you are in my condition.

To me this error was being caused because I had given arguments having space in between and the function/module accepting it wasn't allowed to take space separated input, hence the rest of the input name was being taken as separate arguments causing

"Got unexpected extra arguments" error.

Strychninism answered 19/6, 2018 at 5:35 Comment(2)
Had space in the middle of json and that was enough to cause this issue from windows 10 command line calling databricks client jobs.Carrack
@MarkAndersen thanks for mentioning it, same issue with databricks cli, did you find a workaround?Protagonist
N
4

I got this error when calling a same name method with the click method:


@cli.command()
@click.argument("namespace", nargs=1)
def process(namespace):
    process("foo")  # <---- HERE

Fixed it by rename one of them, hope it helps for others!

Nonconformist answered 26/8, 2021 at 2:34 Comment(2)
Quite a consolation that this can also happen to SE-members with a 5k+ reputation.Epigrammatist
Quite a consolation that I'm not alone in making such a silly mistake!Frenzied
H
-1

Using the click module only in the main script which you run might help. In my case, removing click from modules other than the main helped solve this problem.

Howlond answered 17/2, 2020 at 5:50 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.