Home > Backend Development > Python Tutorial > Introductory tutorial on click, a powerful command line library in Python

Introductory tutorial on click, a powerful command line library in Python

高洛峰
Release: 2017-02-11 13:32:01
Original
1649 people have browsed it

click is a command line tool for Python, which is extremely easy to use. Do not believe? You'll know once you try it. The following article mainly introduces you to the powerful command line library click in Python. Friends who need it can refer to it. Let’s take a look together.

Preface

Our game resource processing tool is implemented in Python. Its functions include csv parsing, UI material processing, animation resource parsing, batch processing, Androd&iOS automatic packaging and other functions. This project was inherited from other departments. Since most of the code did not meet our business needs, a major refactoring was carried out. All business code has been removed, leaving only the python code framework. The command line parameter parsing in the project was implemented by myself, which was extremely inelegant and I had to endure it for so long. I plan to find time to rewrite it using click. So I recently learned about click. The content of this article below is an introductory tutorial on click. Beginners can come and learn together.

Official website mirror address: http://click.uoota.com/6/

Supports: Any nesting of

  1. commands

  2. Automatically generate help information

  3. Support lazy loading of subcommands at runtime

Installation The method is to use pip:

pip install click
Copy after login

The following small piece of code is an example of its official homepage, 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()
Copy after login

Run:

$ python hello.py --count=3
Your name: John
Hello John!
Hello John!
Hello John!
Copy after login

View 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.
Copy after login

For more articles related to the powerful command line library click introductory tutorial in Python, please pay attention to the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template