新聞中心
這篇文章主要為大家展示了“python中如何使用click”,內(nèi)容簡(jiǎn)而易懂,條理清晰,希望能夠幫助大家解決疑惑,下面讓小編帶領(lǐng)大家一起研究并學(xué)習(xí)一下“python中如何使用click”這篇文章吧。
創(chuàng)新互聯(lián)是專業(yè)的灞橋網(wǎng)站建設(shè)公司,灞橋接單;提供成都做網(wǎng)站、成都網(wǎng)站建設(shè)、成都外貿(mào)網(wǎng)站建設(shè),網(wǎng)頁設(shè)計(jì),網(wǎng)站設(shè)計(jì),建網(wǎng)站,PHP網(wǎng)站建設(shè)等專業(yè)做網(wǎng)站服務(wù);采用PHP框架,可快速的進(jìn)行灞橋網(wǎng)站開發(fā)網(wǎng)頁制作和功能擴(kuò)展;專業(yè)做搜索引擎喜愛的網(wǎng)站,專業(yè)的做網(wǎng)站團(tuán)隊(duì),希望更多企業(yè)前來合作!
1.如何安裝
使用命令pip install click或者在PyCharm中安裝
2.隔離環(huán)境vitualenv
linux或MAC上
sudo pip install virtualenv
windows
pip install virtualenv
43如何激活
現(xiàn)在,每當(dāng)您想要處理項(xiàng)目時(shí),您只需激活相應(yīng)的環(huán)境。在OS X和Linux上,執(zhí)行以下操作:
$ . venv/bin/activate
如果您是Windows用戶,則以下命令適合您:
$ venv\scripts\activate
退出激活
$ deactivate
輸入以下命令以在virtualenv中激活Click:
$ pip install Click
4.click語法
函數(shù)通過裝飾來成為Click命令行工具 click.command()。最簡(jiǎn)單的方法是,使用這個(gè)裝飾器裝飾一個(gè)函數(shù)會(huì)使它成為一個(gè)可調(diào)用的腳本:
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()
根據(jù)參數(shù)格式執(zhí)行
$ python hello.py --count=3
Your name: John
Hello John!
Hello John!
Hello John!
自動(dòng)生成幫助文檔
$ 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.打印函數(shù)click.echo
使用echo()而不是常規(guī) print()函數(shù)?這個(gè)問題的答案是Click嘗試以相同的方式支持Python 2和Python 3
從Click 2.0開始,echo函數(shù)也對(duì)ANSI顏色有很好的支持
7.嵌套命令
使用@click.group()實(shí)現(xiàn)命令的嵌套,即可以存在子命令
@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)
正如您所看到的,group()裝飾器的工作方式與command() 裝飾器類似,但創(chuàng)建了一個(gè)Group對(duì)象,可以為其提供多個(gè)可以附加的子命令Group.add_command()。
對(duì)于簡(jiǎn)單腳本,也可以使用Group.command()裝飾器自動(dòng)附加和創(chuàng)建命令。上面的腳本可以這樣編寫:
@click.group()
def cli():
pass
@cli.command()
def initdb():
click.echo('Initialized the database')
@cli.command()
def dropdb():無錫人流多少錢 http://www.bhnnk120.com/
click.echo('Dropped the database')
然后,您將Group在setuptools入口點(diǎn)或其他調(diào)用中調(diào)用:
if __name__ == '__main__':
cli()
8.增加參數(shù)
添加參數(shù)@click.option要添加參數(shù),請(qǐng)使用option()和argument()裝飾器:
@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 %s!' % name)
生成的幫助文檔如下
$ python hello.py --help
Usage: hello.py [OPTIONS] NAME
Options:
--count INTEGER number of greetings
--help Show this message and exit.
生成的幫助文檔如下
$ python hello.py --help
Usage: hello.py [OPTIONS] NAME
Options:
--count INTEGER number of greetings
--help Show this message and exit.
以上是“python中如何使用click”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對(duì)大家有所幫助,如果還想學(xué)習(xí)更多知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道!
網(wǎng)站名稱:python中如何使用click
標(biāo)題網(wǎng)址:http://fisionsoft.com.cn/article/iggohs.html