How can I call click wrapped function as normal?

1.2k Views Asked by At

I have a command as:

import click

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

How can I call this test(...) as normal Python function?

I tried to call the function as test("123"), but received exception:

Error: Got unexpected extra arguments (1 2 3)

So, assuming that the test(...) function comes with a third-party package and I can't modify anything in the function, how can I call this test(...) function?

1

There are 1 best solutions below

3
On

Click calls this out as an antipattern, and recommends you do things like:

import click

@click.command()
@click.option('--count', default=1)
def test(count):
    result = _test(count)
    click.echo(result)

def _test(count):
    return f"Count: {count}"

Structuring your code in this way draws a clear separation between your API and your business logic, which is typically useful.