In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly shows you "how to use click in python", the content is easy to understand, clear, hope to help you solve your doubts, the following let the editor lead you to study and learn "how to use click in python" this article.
1. How to install
Use the command pip install click or install in PyCharm
two。 Isolated environment vitualenv
On linux or MAC
Sudo pip install virtualenv
Windows
Pip install virtualenv
43 how to activate
Now, whenever you want to work on a project, you just need to activate the appropriate environment. On OS X and Linux, do the following:
$. Venv/bin/activate
If you are a Windows user, the following command applies to you:
$venv\ scripts\ activate
Exit activation
$deactivate
Enter the following command to activate Click in virtualenv:
$pip install Click
4.click syntax
The function is decorated to become the Click command line tool click.command (). The easiest way is to decorate a function with this decorator to make it a callable script:
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% slots'% name)
If _ _ name__ = ='_ _ main__':
Hello ()
Execute according to the parameter format
$python hello.py-count=3
Your name: John
Hello John!
Hello John!
Hello John!
Automatically generate help documentation
$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.
6. Print function click.echo
Use echo () instead of the regular print () function? The answer to this question is that Click tries to support Python 2 and Python 3 in the same way.
Since Click 2.0, the echo function also has good support for ANSI colors.
7. Nested command
Use @ click.group () to achieve the nesting of commands, that is, subcommands can exist
@ click.group ()
Def cli ():
Pass
@ click.command ()
Def initdb ():
Click.echo ('Initialized the database')
@ click.command ()
Def dropdb ():
Click.echo ('Dropped the database')
Cli.add_command (initdb)
Cli.add_command (dropdb)
As you can see, the group () decorator works like the command () decorator, but creates a Group object that provides multiple subcommands Group.add_command () that can be appended to it.
For simple scripts, you can also use the Group.command () decorator to automatically attach and create commands. The above script can be written as follows:
@ click.group ()
Def cli ():
Pass
@ cli.command ()
Def initdb ():
Click.echo ('Initialized the database')
@ cli.command ()
Def dropdb (): how much is the flow of people in Wuxi http://www.bhnnk120.com/
Click.echo ('Dropped the database')
You then call Group in the setuptools entry point or other calls:
If _ _ name__ = ='_ _ main__':
Cli ()
8. Add parameters
Add parameter @ click.option to add a parameter, use the option () and argument () decorators:
@ click.command ()
@ click.option ('--count', default=1, help='number of greetings')
@ click.argument ('name')
Def hello (count, name):
For x in range (count):
Click.echo ('Hello% slots'% name)
The generated help documents are as follows
$python hello.py-help
Usage: hello.py [OPTIONS] NAME
Options:
-- count INTEGER number of greetings
-- help Show this message and exit.
The generated help documents are as follows
$python hello.py-help
Usage: hello.py [OPTIONS] NAME
Options:
-- count INTEGER number of greetings
-- help Show this message and exit.
The above is all the content of the article "how to use click in python". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.