Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use the command line artifact Click

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

Command line artifact Click how to use, I believe that many inexperienced people do not know what to do, so this article summarizes the causes of the problem and solutions, through this article I hope you can solve this problem.

Python writers often have to write some command-line tools, although the standard library provides a command-line parsing tool Argparse, but it is very troublesome to write, I rarely use it. The best command-line tool to use is Click, the open source project of Flask's team, pallets. With very little code, Click can elegantly create a command-line tool that aims to make the process of creating a command-line tool fast and fun.

It is better to have a try than to hear about it.

Install pip install Click for use

Create click_demo.py and write the simplest function

Import click

@ click.command ()

Def hello ():

Click.echo ('Hello Worldwide')

If _ _ name__ = ='_ _ main__':

Hello ()

Run:

Python click_demo.py

Hello World!

The decorator click.command () changes the function into a command-line tool in seconds, and the function of the echo function is equivalent to the print function.

Parameters.

The decorator click.option () can specify parameters to the command line function

Import click

@ click.command ()

@ click.option ("--count", default=1, help= "number of prints", type=int)

Def hello (count):

"

This is a simple example.

"

For i in range (count):

Click.echo ('Hello Worldwide')

If _ _ name__ = ='_ _ main__':

Hello ()

-count:

Count is the name of the parameter

Default:

Default value of the parameter

Type:

Assign a type to a parameter

Help:

Description document

When you execute the script, you can view the documentation by adding the parameter-- help.

$python click_demo.py-help

Usage: click_demo.py [OPTIONS]

This is a simple example.

Options:

-- number of count INTEGER prints

-- help Show this message and exit.

Specify parameters:

> python click_demo.py-- count 3

Hello World!

Hello World!

Hello World!prompt

Some command line tools require the user to enter information at run time, and you can specify the prompt parameter to the option decorator

Import click

@ click.command ()

@ click.option ("--count", default=1, help= "number of prints", type=int)

@ click.option ("--name", prompt= "Please enter name", help= "name")

Def hello (count, name):

"

This is a simple example.

"

For i in range (count):

Click.echo (f'Hello {name}')

If _ _ name__ = ='_ _ main__':

Hello () $python click_demo.py

Please enter the first name: lzjun

Hello lzjun!Group

One of the most important features of Click is its grouping function. When the logic of a command-line tool is already very complex, in order to decouple, we need to put different logic in different commands, which can avoid bloated functions of a single command-line tool. Let's look at an example:

# db.py

Import click

@ click.group ()

Def db ():

Pass

@ click.command ()

@ click.option ("--name", help= "user name")

Def add (name):

"

Add user

: param name:

: return:

"

Click.echo (f'add user {name}')

@ click.command ()

@ click.option ("--id", help= "user name")

Def delete (id):

"

Delete user

: param id:

: return:

"

Click.echo (f'delete user {id}')

Db.add_command (delete)

Db.add_command (add)

If _ _ name__ = ='_ _ main__':

Db ()

This is a command line tool for operating database DB, which provides other operations such as adding and deleting users. If all business logic is written in one function, maintenance becomes extremely difficult.

The @ click.group decorator decorates the function as a Group object, and many subcommands can be added through Group.

Python db.py-help

Usage: db.py [OPTIONS] COMMAND [ARGS]...

Options:

-- help Show this message and exit.

Commands:

Add add user: param name:: return:

Delete delete user: param id:: return:

We can see from the help documentation that add and delete are two subcommands.

Flask's command line tool "flask" also provides a number of subcommands.

After reading the above, have you mastered how to use the command line artifact Click? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report