3

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?

Stephen Rauch
  • 47,830
  • 31
  • 106
  • 135
JPG
  • 82,442
  • 19
  • 127
  • 206

1 Answers1

2

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.

Stephen Rauch
  • 47,830
  • 31
  • 106
  • 135
Adam Smith
  • 52,157
  • 12
  • 73
  • 112