Python命令列click引數用法解析

2023-03-19 00:20:38 字數 2804 閱讀 4766

一、前言

在概念上, click 把命令列分為 3 個組成:引數、選項和命令。

引數 就是跟在命令後的除選項外的內容,比如 git add a.txt 中的 a.txt 就是表示檔案路徑的引數

選項 就是以 - 或 -- 開頭的引數,比如 -f、--file

命令 就是命令列的初衷了,比如 git 就是命令,而 git add 中的 add 則是 git 的子命令

二、引數

2.1 基本引數

基本引數 就是通過位置裡指定引數值。

比如,我們可以指定兩個位置引數 x 和 y ,先新增的 x 位於第一個位置,後加入的 y 位於第二個位置。那麼在命令列中輸入 1 2varno的時候,分別對應到的就是 x 和 y:

@click.command()

@click.argument('x')

@click.argument('y')

def hello(x, y):

print(x, y)

2.2 引數型別

引數型別 就是將引數值作為什麼型別去解析,預設情況下是字串型別。我們可以通過 type 入參來指定引數型別。

click 支援的引數型別多種多樣:

同 argparse 一樣,click 也支援自定義型別,需要編寫 click.paramtype 的子類,並過載 convert 方法。

2.3 檔案引數

在基本引數的基礎上,通過指定引數型別,我們就能構建出各類引數。

檔案引數 是非常常用的一類引數,通過 type=click.file 指定,它能正確處理所有 python 版本的 unicode 和 位元組,使得處理檔案十分方便。

@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) # 此時 input 為檔案物件,每次讀入 1024 位元組

if not chunk:

break

output.write(chunk) # 此時 output 為檔案物件,寫入上步讀入的內容

2.4 檔案路徑引數

檔案路徑引數 用來處理檔案路徑,可以對路徑做是否存在等檢查,通過 type=click.path 指定。不**件名是 unicode 還是程式設計客棧位元組型別,獲取到的引數型別都是 unicode 型別。

@click.command()

@click.argument('filename', type=click.path(exists=true)) # 要求給定路徑存在,否則報錯

def hello(filename):

click.echo(click.format_filename(filename))

如果檔名是以 - 開頭,會被誤認為是命令列選項,這個時候需要在引數前加上 -- 和空格,比如

$ python hello.py -- -foo.txt

-foo.txt

2.5 選擇項引數

選擇項引數 用來限定引數內容,通過 type=click.choice 指定。

比如,指定檔案讀取方式限制為 read-only 和 read-write:

@click.command()

@click.argument('mode', type=click.choice(['read-only', 'read-write']))

def hello(mode):

click.echo(mode)

2.6 可變引數

可變引數 用來定義一個引數可以有多個值,且能通過 nargs 來定義值的個數,取得的引數的變數型別為元組。

若 nargs=n,n為一個數字,則要求該引數提供 n 個值。若 n 為 -1 則接受提供無數量限制的引數,如:

@click.command()

@click.argument('foo', nargs=-1)

@click.argument('bar', nargs=1)

def hello(foo, bar):

pass

如果要實現 argparse 中要求引數數量為 1 個或多個的功能,則指定 nargs=-1 且 required=true 即可:

@click.command()

@click.argument('foo', nargs=-1, required=true)

def hello(foo, bar):

pass

2.7 從環境變數讀取引數

通過在 click.argument 中指定 envvar,則可讀取指定名稱的環境變數作為引數值,比如:

@click.command()

@click.argument('filename', envvar='filename')

def hello(filename):

print(filename)

執行如下命令檢視效果:

www.cppcns.com$ filename=hello.txt python3 hello.py

hello.txt

而在 argparse 中,則需要自己從環境變數中讀取。

三、小節

本文講解了 click 中基本引數的用法,在此基礎上介紹了各種型別的引數,最後說明了從環境變數中獲取引數值的寫法。

本文標題: python命令列click引數用法解析

本文地址:

Python 命令列引數

python 提供了getopt模組來獲取命令列引數。python test.py arg1 arg2 arg3python 中也可以所用 sys 的sys.argv來獲取命令列引數 注 sys.argv 0 表示指令碼名。例項 test.py 檔案 如下 usr bin python coding...

Python命令列引數

python 提供了getopt模組來獲取命令列引數。python test py arg1 arg2 arg3python 中也可以所用sys的sys.argv來獲取命令列引數 注 sys.argv 0 表示指令碼名。test.py 檔案 如下 usr bin python coding utf ...

Python 命令列引數

記錄背景 16年時候記錄的 python 提供了 getopt 模組來獲取命令列引數。python test.py arg1 arg2 arg3python 中也可以所用 sys 的 sys.argv 來獲取命令列引數 注 sys.argv 0 表示指令碼名。例項test.py 檔案 如下 usr ...

Python 命令列引數

python 提供了 getopt 模組來獲取命令列引數。python test.py arg1 arg2 arg3python 中也可以所用 sys 的 sys.ar 來獲取命令列引數 sys.ar 是命令列引數列表。len sys.ar 是命令列引數個數。注 sys.ar 0 表示指令碼名。例項...

python 命令列引數

from sys import ar print ar 執行結果 python test.py test.py python test.py abc test.py abc python test.py abc 12345 test.py abc 12345 語法格式 getopt.getopt a...

python 命令列引數解析

argparse是python標準庫推薦使用的命令列引數解析模組。下面是一個示例 import argparse def main parser argparse.argumentparser description progrom description parser.add argument m...

Python命令列引數大全

b 當轉換陣列為字串時提出警告。比方str bytes instance str bytearray instance b 當匯入.py co 檔案阻止寫入位元組碼檔案。c cmd c後面的引數cmd當作字串傳給指令碼程式。d 分析階段是否進行列印分析樹輸出。e 忽然環境引數變數。h 列印命令列引數...

python解析命令列引數

常常需要解析命令列引數,經常忘記,好煩,總結下來吧。1.python 中也可以所用sys的sys.ar 來獲取命令列引數 sys.ar 是命令列引數列表 引數個數 len sys.ar 指令碼名 sys.ar 0 引數1 sys.ar 1 示例 如下 1 usr bin python2 coding...

python解析命令列引數

20200816 python自帶了命令列解析的庫argparse,利用這個就能達到不錯的效果。import argparse parser argparse.argumentparser parser.add argument file help input file name args pars...

python3命令列引數 Python命令列引數

python提供了一個getopt模組,用於解析命令列選項和引數。python test.py arg1 arg2 arg3 python sys模組通過sys.ar 提供對任何命令列引數的訪問。主要有兩個引數變數 sys.ar 是命令列引數的列表。len sys.ar 是命令列引數的數量。這裡sy...