How do I return a value when @click.option is used to pass a command line argument to a function?

后端 未结 4 2072
慢半拍i
慢半拍i 2021-02-20 04:31

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 documentati

4条回答
  •  有刺的猬
    2021-02-20 05: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()
    

提交回复
热议问题