How do I return a value when @click.option is used

2019-04-28 22:22发布

I am trying to use click python package to pass a command line argument to a function. The example from official documentation works as explained. But nowhere in the documentation is it mentioned how to return a value back. None of the functions in the documentation returns a value, so I don't understand how to do this.

Given example at documentation:

import click

@click.command()
@click.option('--count', default=3, help='Number of greetings.')

def hello(count):
    """Simple program that greets NAME for a total of COUNT times."""
    for x in range(count):
        click.echo('Hello')

if __name__ == '__main__':
    hello()

What I want to do:

import click

@click.command()
@click.option('--count', default=3, help='Number of greetings.')

def hello(count):
    """Simple program that greets NAME for a total of COUNT times."""
    for x in range(count):
        click.echo('Hello')
    return "hello hi"

if __name__ == '__main__':
    print(hello())

"hello hi" is not returned as output on console. Can someone tell me how to achieve this?

3条回答
Luminary・发光体
2楼-- · 2019-04-28 23:00

You have to tell click to print to stdout using click.echo(). Here is a working example using the code in your question (without caring about exit code as others have mentioned):

import click


@click.command()
@click.option('--count', default=3, help='Number of greetings.')
def hello(count):
    """Simple program that greets NAME for a total of COUNT times."""
    for x in range(count):
        click.echo('Hello')
    return click.echo("hello hi")

if __name__ == '__main__':
    hello()
查看更多
甜甜的少女心
3楼-- · 2019-04-28 23:00

Apparently this click package makes your hello() function never return. So the print never happens. If I were you, I would not use this unintuitive package and instead use argparse which is great at command line processing in Python (one of the best in any language, in my opinion).

Here's a working version of your code using argparse. It works out-of-the-box with Python (any version 2.7 or later), and it's easy to understand.

def hello(count):
    """Simple program that greets NAME for a total of COUNT times."""
    for x in range(count):
        print('Hello')
    return "hello hi"

if __name__ == '__main__':
    import argparse
    parser = argparse.ArgumentParser(description=__doc__)
    parser.add_argument('--count', default=3, type=int, help='Number of greetings.')
    args = parser.parse_args()

    print(hello(args.count))
查看更多
爱情/是我丢掉的垃圾
4楼-- · 2019-04-28 23:20

Unfortunately, what you're trying to do doesn't make sense. Command-line programs can have an exit code, but that's just a small integer; they can't return text, or arbitrary Python objects.

There's a quasi-standard for what these integers means; the simple version is 0 for success, 1 for most errors, 2 for invalid command-line arguments. click is trying to make your function into a good command-line citizen, so when you exit your function, it calls sys.exit with the appropriate number (0 if you return, 1 if you raise, and 2 if it failed to parse your arguments).

So, whatever you return has no effect, and whatever you try to do with the return value at the top level doesn't even get run.

What programs usually do when they need to "return" text is to print it to standard output, which is exactly what click.echo is for.

查看更多
登录 后发表回答