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

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

This example is in the 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 hello" does not return as console output. Can someone tell me how to do this?

+5
source share
3 answers

Unfortunately, what you are trying to do does not make sense. Command line programs may have an exit code, but this is just a small integer; they cannot return text or arbitrary Python objects.

There is a quasistandard for which these integers mean; the simple version is 0 for success, 1 for most errors, 2 for invalid command line arguments. click tries to turn your function into a good command line user, so when you exit your function, it calls sys.exit with the corresponding number (0 if you return , 1 if you raise and 2 if it could not parse your arguments) .

So, regardless of the fact that you return has no effect, and everything you try to do with the return value at the top level does not even start.

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

+4
source

You should say click to print to stdout using click.echo (). Here is a working example using the code in your question (not caring about exit codes, 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() 
0
source

Apparently this click package causes your hello() function to never return. Therefore, printing is never. If I were you, I would not use this non-intuitive package and instead use argparse , which is great for handling command line in Python (one of the best in any language, in my opinion).

Here is the working version of your code using argparse . It works out of the box with Python (any version 2.7 or later), and this is 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)) 
-3
source

Source: https://habr.com/ru/post/1204222/


All Articles