1. 程式人生 > 其它 >【pytest單元測試框架】(4)click模組介紹

【pytest單元測試框架】(4)click模組介紹

Click模組

  click模組是Flask的作者開發的一個第三方模組,用於快速建立命令列。它的作用與Python標準庫的argparse相同,但是,使用起來更簡單。

  click是一個第三方庫,因此使用起來需要先行安裝

安裝click模組

使用pip命令即可完成模組的安裝:

pip install click

基本使用

Click對argparse的主要改在在於易用性,使用click模組主要分為兩個步驟:

  1. 使用@click.command() 裝飾一個函式,使之成為命令列介面
  2. 使用@click.option() 等裝飾函式,為其新增命令列選項

編寫例項:

# -*- coding:utf-8 -*-
# filename:test_click.py # author: click_team # date: 2021/11/19 import click @click.command() @click.option("--a", default=1, help="number1") @click.option("--b", prompt="input b", help="number2") def add(a, b): a = int(a) b = int(b) click.echo("{0}和{1}相乘等於{2}".format(a, b, a*b)) if __name__
== "__main__": add()

--help執行結果:

D:\00test\base_practice\clickTest>python test_click.py --help
Usage: test_click.py [OPTIONS]

Options:
--a INTEGER number1
--b TEXT number2
--help Show this message and exit.

--default結果:

D:\00test\base_practice\clickTest>python test_click.py
input b: 9
1和9相乘等於9

加入引數--a和--b執行結果:

D:\00test\base_practice\clickTest>python test_click.py --a 8 --b 9
8和9相乘等於72

在上面的例子中,函式hello接受兩個引數,分別是a和b,他們的取值從命令列中獲取,這裡我們使用了click模組中的command、option、echo,他們的作用如下:

  • command:使函式hello成為命令列介面
  • option:增加命令列選項
  • echo:輸出結果,使用echo進行輸出是為了更好的相容性,因為python 2中的print是個語句,python 3中的print 是一個函式

其他引數

option最基本的用法就是通過指定命令列選項的名稱,從命令列讀取引數值,再將其傳遞給函式。option常用的引數含義:

  • default: 設定命令列引數的預設值
  • help:引數說明
  • type:引數型別,可以是string、int、float等
  • prompt:當在命令列中沒有輸入相應的引數時,會更具prompt提示使用者輸入
  • nargs:指定命令列引數接受的值的個數
  • required:是否為必填引數
# -*- coding:utf-8 -*-
# filename:test_click.py
# author: click_team
# date: 2021/11/19

import click


@click.command()
@click.option("--a", default=1, help="number1", type=int)
@click.option("--b", prompt="input b", help="number2", type=int, required=True)
@click.option("--c", prompt="input c", help="number3", type=int, required=True)
@click.option("--d", prompt="input d", help="str1", type=str)
@click.option("--e", nargs=2, type=float)
def mul(a, b, c, d, e):
    click.echo("乘數字:{0}和{1}相乘等於{2}".format(a, b, a*b))
    click.echo("乘字串:{0}和{1}相乘等於{2}".format(c, d, c*d))
    click.echo("對一個引數輸入兩個值:{}".format(e))


if __name__ == "__main__":
    mul()

--help執行結果:

D:\00test\base_practice\clickTest>python test_click.py --help
Usage: test_click.py [OPTIONS]

Options:
  --a INTEGER   number1
  --b INTEGER   number2  [required]
  --c INTEGER   number3  [required]
  --d TEXT      str1
  --e FLOAT...
  --help        Show this message and exit.

加引數執行結果:

D:\00test\base_practice\clickTest>python test_click.py --a 8 --b 9 --c 5  --d 9 --e 10.00 11.00
乘數字:8和9相乘等於72
乘字串:5和9相乘等於99999
對一個引數輸入兩個值:(10.0, 11.0)

注意:option中定義的引數名稱,那麼就需要用同名的變數進行接受。

擴充套件用法

場景一:我們限定使用者輸入的值,那麼就需要使用Click模組中的Choice函式,Choice的引數是一個列表,該列表中列出所有可能的值。

import click


@click.command()
@click.option("--c", required=True, type=click.Choice(["START", "STOP"]), help="請輸入START或STOP")
def get_command(c):
    click.echo("確認值為{}".format(c))


if __name__ == '__main__':
    get_command()

--help執行結果:

D:\00test\base_practice\clickTest>python test_click2.py --help
Usage: test_click2.py [OPTIONS]

Options:
  --c [START|STOP]  請輸入START或STOP  [required]
  --help            Show this message and exit.

--輸入錯誤引數結果:

D:\00test\base_practice\clickTest>python test_click2.py --c ST
Usage: test_click2.py [OPTIONS]
Try 'test_click2.py --help' for help.

Error: Invalid value for '--c': 'ST' is not one of 'START', 'STOP'.

--輸入正確值

D:\00test\base_practice\clickTest>python test_click2.py --c START
確認值為START

場景二:應用程式從命令列讀取密碼:

使用標準庫中的argparse模組只能像輸入普通引數一樣輸入密碼。這種方式存在一定安全隱患,例如輸入的密碼會儲存在history中,檢視命令歷史列表就能獲取密碼

在Click中,這個問題就能完美的解決,只需要是這prompt為True,那麼我們就能互動式輸入密碼,設定hide_input為True,就能隱藏密碼,設定confirmation_prompt為True,就可以進行密碼的兩次驗證,使用起來非常便捷。

import click


@click.command()
@click.option("--p", prompt="your password", hide_input=True)
def test_passwd(p):
    click.echo("您的密碼是{}".format(p))


if __name__ == '__main__':
    test_passwd()

執行結果:

D:\00test\base_practice\clickTest>python test_click2.py
your password:
您的密碼是122212

本部落格所有文章僅用於學習、研究和交流目的,歡迎非商業性質轉載。

本文來自部落格園,作者:hello_殷,轉載請註明原文連結:https://www.cnblogs.com/yinzuopu/p/15578660.html

本文版權歸作者和部落格園共有,歡迎轉載,但必須給出原文連結,並保留此段宣告,否則保留追究法律責任的權利。