Python's powerful command line library click tutorial to get started
- 2020-05-19 05:03:12
- OfStack
preface
Our game resource processing tool is implemented by Python, and its functions include csv parsing, UI material processing, animation resource parsing, batch processing, Androd&iOS automatic packaging and other functions. The project was inherited from other departments, and because most of the code did not meet our business requirements, we did a big refactoring. All business code was removed and only the python code framework was retained. The command line parameter parsing in the project was self-implemented, extremely inelegant, and endured for so long. I'm going to find time to rewrite it using click. So I recently learned click. This article is an introduction to click for beginners.
Website image address: http: / / click uoota. com / / 6
Support:
Any nesting of commands Automatically generate help messages Lazy loading of subcommands at run time is supportedThe installation method is using pip:
pip install click
The following code is an example of its official home page, posted below:
import click
@click.command()
@click.option('--count', default=1, help='Number of greetings.')
@click.option('--name', prompt='Your name',
help='The person to greet.')
def hello(count, name):
"""Simple program that greets NAME for a total of COUNT times."""
for x in range(count):
click.echo('Hello %s!' % name)
if __name__ == '__main__':
hello()
Run:
$ python hello.py --count=3
Your name: John
Hello John!
Hello John!
Hello John!
See help information:
$ python hello.py --help
Usage: hello.py [OPTIONS]
Simple program that greets NAME for a total of COUNT times.
Options:
--count INTEGER Number of greetings.
--name TEXT The person to greet.
--help Show this message and exit.
conclusion
The above is the introduction of Python command line tool click installation and use of all the content, I hope the content of this article for everyone to learn or use python can bring 1 definite help, if you have any questions you can leave a message to communicate.