如何在Python3中使用Click模塊-創(chuàng)新互聯(lián)

這期內(nèi)容當(dāng)中小編將會(huì)給大家?guī)?lái)有關(guān)如何在Python3中使用Click模塊,文章內(nèi)容豐富且以專業(yè)的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。

遼中網(wǎng)站建設(shè)公司創(chuàng)新互聯(lián)公司,遼中網(wǎng)站設(shè)計(jì)制作,有大型網(wǎng)站制作公司豐富經(jīng)驗(yàn)。已為遼中上千多家提供企業(yè)網(wǎng)站建設(shè)服務(wù)。企業(yè)網(wǎng)站搭建\成都外貿(mào)網(wǎng)站建設(shè)公司要多少錢,請(qǐng)找那個(gè)售后服務(wù)好的遼中做網(wǎng)站的公司定做!
# hello.py
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()

執(zhí)行 python hello.py --count=3,不難猜到控制臺(tái)的輸出結(jié)果。除此之外,Click 還悄悄地做了其他的工作,比如幫助選項(xià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.

函數(shù)秒變 CLI

從上面的 “Hello World” 演示中可以看出,Click 是通過(guò)裝飾器來(lái)把一個(gè)函數(shù)方法裝飾成命令行接口的,這個(gè)裝飾器方法就是 `@click.command()`。

import click
@click.command()
def hello():
  click.echo('Hello World!')

`@click.command()裝飾器把hello()方法變成了Command對(duì)象,當(dāng)它被調(diào)用時(shí),就會(huì)執(zhí)行該實(shí)例內(nèi)的行為。而–help參數(shù)就是Command` 對(duì)象內(nèi)置的參數(shù)。

不同的 Command 實(shí)例可以關(guān)聯(lián)到 group 中。group 下綁定的命令就成為了它的子命令,參考下面的代碼:

@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)

`@click.group裝飾器把方法裝飾為可以擁有多個(gè)子命令的Group對(duì)象。由Group.add_command()方法把Command對(duì)象關(guān)聯(lián)到Group對(duì)象。 也可以直接用@Group.command裝飾方法,會(huì)自動(dòng)把方法關(guān)聯(lián)到該Group` 對(duì)象下。

@click.group()
def cli():
  pass
@cli.command()
def initdb():
  click.echo('Initialized the database')
@cli.command()
def dropdb():
  click.echo('Dropped the database')

命令行的參數(shù)是不可或缺的,Click 支持對(duì) command 方法添加自定義的參數(shù),由 option() 和 argument() 裝飾器實(shí)現(xiàn)。

@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)
打包跨平臺(tái)可執(zhí)行程序

通過(guò) Click 編寫了簡(jiǎn)單的命令行方法后,還需要把 .py 文件轉(zhuǎn)換成可以在控制臺(tái)里運(yùn)行的命令行程序。最簡(jiǎn)單的辦法就是在文件末尾加上如下代碼:

if __name__ == '__main__':
  command()

Click 支持使用 setuptools 來(lái)更好的實(shí)現(xiàn)命令行程序打包,把源碼文件打包成系統(tǒng)中的可執(zhí)行程序,并且不限平臺(tái)。一般我們會(huì)在源碼根目錄下創(chuàng)建 setup.py 腳本,先看一段簡(jiǎn)單的打包代碼:

from setuptools import setup
setup(
  name='hello',
  version='0.1',
  py_modules=['hello'],
  install_requires=[
    'Click',
  ],
  entry_points='''
    [console_scripts]
    hello=hello:cli
  ''',
)

留意 entry_points 字段,在 console_scripts 下,每一行都是一個(gè)控制臺(tái)腳本,等號(hào)左邊的的是腳本的名稱,右邊的是 Click 命令的導(dǎo)入路徑。

詳解命令行參數(shù)

上面提到了自定義命令行參數(shù)的兩個(gè)裝飾器:`@click.option()和@click.argument()`,兩者有些許區(qū)別,使用場(chǎng)景也有所不同。

總體而言,argument() 裝飾器比 option() 功能簡(jiǎn)單些,后者支持下面的特性:

  • 自動(dòng)提示缺失的輸入;

  • option 參數(shù)可以從環(huán)境變量中獲取,argument 參數(shù)則不行;

  • option 參數(shù)在 help 輸出中有完整的文檔,argument 則沒(méi)有;

而 argument 參數(shù)可以接受可變個(gè)數(shù)的參數(shù)值,而 option 參數(shù)只能接收固定個(gè)數(shù)的參數(shù)值(默認(rèn)是 1 個(gè))。

Click 可以設(shè)置不同的參數(shù)類型,簡(jiǎn)單類型如 click.STRING,click.INT,click.FLOAT,click.BOOL。

命令行的參數(shù)名由 “-short_name” 和 “–long_name” 聲明,如果參數(shù)名既沒(méi)有以 “-“ 開(kāi)頭,也沒(méi)有以 “–” 開(kāi)頭,那么這邊變量名會(huì)成為被裝飾方法的內(nèi)部變量,而非方法參數(shù)。

Option 參數(shù)

option 最基礎(chǔ)的用法就是簡(jiǎn)單值變量,option 接收一個(gè)變量值,下面是一段示例代碼:

@click.command()
@click.option('--n', default=1)
def dots(n):
  click.echo('.' * n)

如果在命令行后面跟隨參數(shù) --n=2 就會(huì)輸出兩個(gè)點(diǎn),如果傳參數(shù),默認(rèn)輸出一個(gè)點(diǎn)。上面的代碼中,參數(shù)類型沒(méi)有顯示給出,但解釋器會(huì)認(rèn)為是 INT 型,因?yàn)槟J(rèn)值 1 是 int 值。

有些時(shí)候需要傳入多個(gè)值,可以理解為一個(gè) list,option 只支持固定長(zhǎng)度的參數(shù)值,即設(shè)置后必須傳入,個(gè)數(shù)由 nargs 確定。

@click.command()
@click.option('--pos', nargs=2, type=float)
def findme(pos):
  click.echo('%s / %s' % pos)
findme --pos 2.0 3.0 輸出結(jié)果就是 2.0 / 3.0

既然可以傳入 list,那么 tuple 呢?Click 也是支持的:

@click.command()
@click.option('--item', type=(unicode, int))
def putitem(item):
  click.echo('name=%s id=%d' % item)

這樣就傳入了一個(gè) tuple 變量,putitem --item peter 1338 得到的輸出就是 name=peter id=1338

上面沒(méi)有設(shè)置 nargs,因?yàn)?nargs 會(huì)自動(dòng)取 tuple 的長(zhǎng)度值。因此上面的代碼實(shí)際上等同于:

@click.command()
@click.option('--item', nargs=2, type=click.Tuple([unicode, int]))
def putitem(item):
  click.echo('name=%s id=%d' % item)

option 還支持同一個(gè)參數(shù)多次使用,類似 git commit -m aa -m bb 中 -m 參數(shù)就傳入了 2 次。option 通過(guò) multiple 標(biāo)識(shí)位來(lái)支持這一特性:

@click.command()
@click.option('--message', '-m', multiple=True)
def commit(message):
  click.echo('\n'.join(message))

有時(shí)候,命令行參數(shù)是固定的幾個(gè)值,這時(shí)就可以用到 Click.choice 類型來(lái)限定傳參的潛在值:

# choice
@click.command()
@click.option('--hash-type', type=click.Choice(['md5', 'sha1']))
def digest(hash_type):
  click.echo(hash_type)

當(dāng)上面的命令行程序參數(shù) --hash-type 不是 md5 或 sha1,就會(huì)輸出錯(cuò)誤提示,并且在 --help 提示中也會(huì)對(duì) choice 選項(xiàng)有顯示。

如果希望命令行程序能在我們錯(cuò)誤輸入或漏掉輸入的情況下,友好的提示用戶,就需要用到 Click 的 prompt 功能,看代碼:

# prompt
@click.command()
@click.option('--name', prompt=True)
def hello(name):
  click.echo('Hello %s!' % name)

如果在執(zhí)行 hello 時(shí)沒(méi)有提供 –name 參數(shù),控制臺(tái)會(huì)提示用戶輸入該參數(shù)。也可以自定義控制臺(tái)的提示輸出,把 prompt 改為自定義內(nèi)容即可。

對(duì)于類似賬戶密碼等參數(shù)的輸入,就要進(jìn)行隱藏顯示。option 的 hide_input 和 confirmation_promt 標(biāo)識(shí)就是用來(lái)控制密碼參數(shù)的輸入:

# password
@click.command()
@click.option('--password', prompt=True, hide_input=True,
       confirmation_prompt=True)
def encrypt(password):
  click.echo('Encrypting password to %s' % password.encode('rot13'))

Click 把上面的操作進(jìn)一步封裝成裝飾器 click.password_option(),因此上面的代碼也可以簡(jiǎn)化成:

# password
@click.command()
@click.password_option()
def encrypt(password):
  click.echo('Encrypting password to %s' % password.encode('rot13'))

有的參數(shù)會(huì)改變命令行程序的執(zhí)行,比如 node 是進(jìn)入 Node 控制臺(tái),而 node --verion 是輸出 node 的版本號(hào)。Click 提供 eager 標(biāo)識(shí)對(duì)參數(shù)名進(jìn)行標(biāo)記,攔截既定的命令行執(zhí)行流程,而是調(diào)用一個(gè)回調(diào)方法,執(zhí)行后直接退出。下面模擬 click.version_option() 的功能,實(shí)現(xiàn) --version 參數(shù)名輸出版本號(hào):

# eager
def print_version(ctx, param, value):
  if not value or ctx.resilient_parsing:
    return
  click.echo('Version 1.0')
  ctx.exit()
@click.command()
@click.option('--version', is_flag=True, callback=print_version,
       expose_value=False, is_eager=True)
def hello():
  click.echo('Hello World!')

對(duì)于類似刪除數(shù)據(jù)庫(kù)表這樣的危險(xiǎn)操作,Click 支持彈出確認(rèn)提示,--yes 標(biāo)識(shí)位置為 True 時(shí)會(huì)讓用戶再次確認(rèn):

# yes parameters
def abort_if_false(ctx, param, value):
  if not value:
    ctx.abort()
@click.command()
@click.option('--yes', is_flag=True, callback=abort_if_false,
       expose_value=False,
       prompt='Are you sure you want to drop the db?')
def dropdb():
  click.echo('Dropped all tables!')

測(cè)試運(yùn)行下:

$ dropdb
Are you sure you want to drop the db? [y/N]: n
Aborted!
$ dropdb --yes
Dropped all tables!

同樣的,Click 對(duì)次進(jìn)行了封裝,click.confirmation_option() 裝飾器實(shí)現(xiàn)了上述功能:

@click.command()
@click.confirmation_option(prompt='Are you sure you want to drop the db?')
def dropdb():
  click.echo('Dropped all tables!')

前面只講了默認(rèn)的參數(shù)前綴 -- 和 -,Click 允許開(kāi)發(fā)者自定義參數(shù)前綴(雖然嚴(yán)重不推薦)。

# other prefix
@click.command()
@click.option('+w/-w')
def chmod(w):
  click.echo('writable=%s' % w)
if __name__ == '__main__':
  chmod()

如果想要用 / 作為前綴,而且要像上面一樣采用布爾標(biāo)識(shí),會(huì)產(chǎn)生沖突,因?yàn)椴紶枠?biāo)識(shí)也是用 /,這種情況下可以用 ; 代替布爾標(biāo)識(shí)的 /:

@click.command()
@click.option('/debug;/no-debug')
def log(debug):
  click.echo('debug=%s' % debug)
if __name__ == '__main__':
  log()

既然支持 Choice,不難聯(lián)想到 Range,先看代碼:

# range
@click.command()
@click.option('--count', type=click.IntRange(0, 20, clamp=True))
@click.option('--digit', type=click.IntRange(0, 10))
def repeat(count, digit):
  click.echo(str(digit) * count)
if __name__ == '__main__':
  repeat()
Argument 參數(shù)

Argument 的作用類似 Option,但沒(méi)有 Option 那么全面的功能。

和 Option 一樣,Argument 最基礎(chǔ)的應(yīng)用就是傳遞一個(gè)簡(jiǎn)單變量值:

@click.command()
@click.argument('filename')
def touch(filename):
  click.echo(filename)

命令行后跟的參數(shù)值被賦值給參數(shù)名 filename。

另一個(gè)用的比較廣泛的是可變參數(shù),也是由 nargs 來(lái)確定參數(shù)個(gè)數(shù),變量值會(huì)以 tuple 的形式傳入函數(shù):

@click.command()
@click.argument('src', nargs=-1)
@click.argument('dst', nargs=1)
def copy(src, dst):
  for fn in src:
    click.echo('move %s to folder %s' % (fn, dst))

運(yùn)行程序:

$ copy foo.txt bar.txt my_folder
move foo.txt to folder my_folder
move bar.txt to folder my_folder

Click 支持通過(guò)文件名參數(shù)對(duì)文件進(jìn)行操作,click.File() 裝飾器就是處理這種操作的,尤其是在類 Unix 系統(tǒng)下,它支持以 - 符號(hào)作為標(biāo)準(zhǔn)輸入/輸出。

# File

@click.command()
@click.argument('input', type=click.File('rb'))
@click.argument('output', type=click.File('wb'))
def inout(input, output):
  while True:
    chunk = input.read(1024)
    if not chunk:
      break
    output.write(chunk)

運(yùn)行程序,先將文本寫進(jìn)文件,再讀取

$ inout - hello.txt
hello
^D
$ inout hello.txt -
hello

如果參數(shù)值只是想做為文件名而已呢,很簡(jiǎn)單,將 type 指定為 click.Path():

@click.command()
@click.argument('f', type=click.Path(exists=True))
def touch(f):
  click.echo(click.format_filename(f))
$ touch hello.txt
hello.txt
$ touch missing.txt
Usage: touch [OPTIONS] F
Error: Invalid value for "f": Path "missing.txt" does not exist.

上述就是小編為大家分享的如何在Python3中使用Click模塊了,如果剛好有類似的疑惑,不妨參照上述分析進(jìn)行理解。如果想知道更多相關(guān)知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。

當(dāng)前文章:如何在Python3中使用Click模塊-創(chuàng)新互聯(lián)
本文URL:http://bm7419.com/article14/dsejde.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供外貿(mào)網(wǎng)站建設(shè)電子商務(wù)定制開(kāi)發(fā)軟件開(kāi)發(fā)網(wǎng)站制作網(wǎng)站設(shè)計(jì)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

外貿(mào)網(wǎng)站制作