Python命令行解析器argparse詳解
第1章 argparse簡介
1.1 解析
argparse 模塊是Python內(nèi)置的一個用于命令項選項與參數(shù)解析的模塊,argparse 模塊可以讓人輕松編寫用戶友好的命令行接口。
argparse能夠幫助程序員為模型定義參數(shù)、通過sys.argv 解析命令行參數(shù)。模塊還會自動生成幫助和使用手冊,并在用戶給程序傳入無效參數(shù)時報出錯誤信息。
1.2 argparse定義三步驟
(1)創(chuàng)建一個命令行解析器對象——創(chuàng)建 ArgumentParser() 對象
(2)給解析器添加命令行參數(shù) ——調(diào)用add_argument() 方法添加參數(shù)
(3)解析命令行的參數(shù) ——使用 parse_args() 解析添加的參數(shù)
1.3 代碼示例
# 導(dǎo)入庫 import argparse # 1. 定義命令行解析器對象 parser = argparse.ArgumentParser(description='test') # 2. 添加命令行參數(shù) parser.add_argument('--sparse', action='store_true', default=False, help='GAT with sparse version or not.') parser.add_argument('--seed', type=int, default=72, help='Random seed.') parser.add_argument('--epochs', type=int, default=10000, help='Number of epochs to train.') # 3. 從命令行中結(jié)構(gòu)化解析參數(shù) args = parser.parse_args() print(args.sparse) print(args.seed) print(args.epochs)
第2章 參數(shù)詳解
2.1 創(chuàng)建一個命令行解析器對象:ArgumentParser()
使用 argparse 的第一步是創(chuàng)建一個 ArgumentParser 對象:
parser = argparse.ArgumentParser(description='test')
ArgumentParser 實例化對象將包含把命令行解析成 Python 數(shù)據(jù)類型所需的全部信息,
(1)描述description
大多數(shù)對 ArgumentParser 構(gòu)造方法的調(diào)用都會使用 description= 關(guān)鍵字參數(shù)。
這個參數(shù)簡要描述這個程度做什么以及怎么做。
在幫助消息中,這個描述會顯示在命令行用法字符串和各種參數(shù)的幫助消息之間。
2.2 為命令行添加參數(shù): add_argument() 方法
(1)添加命令行參數(shù)的案例
給一個 ArgumentParser 添加程序參數(shù)信息,是通過調(diào)用 add_argument() 方法完成的。
通常,這些調(diào)用指定 ArgumentParser 如何獲取命令行字符串并將其轉(zhuǎn)換為對象。
這些信息在 parse_args() 調(diào)用時被存儲在ArgumentParser實例化對象中,以供后續(xù)使用。
例如:
parser.add_argument('--sparse', action='store_true', default=False, help='GAT with sparse version or not.') parser.add_argument('--seed', type=int, default=72, help='Random seed.') parser.add_argument('--epochs', type=int, default=10000, help='Number of epochs to train.')
add_argument() 方法定義如何解析命令行參數(shù)的呢?
(2)add_argument() 方法定義如何解析命令行參數(shù)
ArgumentParser.add_argument(name or flags...[, action][, nargs][, const][, default][, type][,
choices][, required][, help][, metavar][, dest])
每個參數(shù)解釋如下:
name or flags - 普通參數(shù)或flag參數(shù)選項參數(shù)的名稱或標(biāo)簽,例如 foo 或者 -f, --foo。Flag參數(shù)不需要指定參數(shù)值,只需要帶有參數(shù)名即可。
action - 命令行遇到flags參數(shù)時的動作。有兩個常見的動作,store_true:設(shè)定flag參數(shù)為true;store_false:設(shè)定flag參數(shù)為False。
nargs - 應(yīng)該讀取的命令行參數(shù)個數(shù),可以是具體的數(shù)字,或者是?號,當(dāng)不指定值時對于 Positional argument 使用 default,對于 Optional argument 使用 const;或者是 * 號,表示 0 或多個參數(shù);或者是 + 號表示 1 或多個參數(shù)。
default - 不指定參數(shù)時該參數(shù)的默認(rèn)值。
type - 命令行參數(shù)應(yīng)該被轉(zhuǎn)換成的數(shù)據(jù)類型。
required - 是否為必選參數(shù)或可選參數(shù)。
help - 參數(shù)的幫助信息,當(dāng)指定為 argparse.SUPPRESS 時表示不顯示該參數(shù)的幫助信息.
metavar - 在 usage 說明中的參數(shù)名稱,對于必選參數(shù),默認(rèn)就是參數(shù)名稱,對于可選參數(shù)默認(rèn)是全大寫的參數(shù)名稱。
dest - 解析后的參數(shù)名稱,默認(rèn)情況下,對于可選參數(shù)選取最長的名稱,中劃線轉(zhuǎn)換為下劃線.
choices - 參數(shù)可允許的值的一個容器。
const - action 和 nargs 所需要的常量值。
store_const,表示賦值為const;
append,將遇到的值存儲成列表,也就是如果參數(shù)重復(fù)則會保存多個值;
append_const,將參數(shù)規(guī)范中定義的一個值保存到一個列表;
count,存儲遇到的次數(shù);此外,也可以繼承 argparse.Action 自定義參數(shù)解析;
2.3解析命令行的參數(shù):parse_args()
ArgumentParser對象通過 parse_args() 方法解析命令行的參數(shù)。
它將檢查命令行中每個參數(shù),轉(zhuǎn)換為適當(dāng)?shù)臄?shù)據(jù)類型,然后調(diào)用相應(yīng)的操作,并把參數(shù)結(jié)構(gòu)化后存放在對象args中。
args = parser.parse_args()
在腳本中,通常 parse_args() 會被不帶參數(shù)調(diào)用,而 ArgumentParser 將自動從 sys.argv 中確定命令行參數(shù)。
2.4 命令行參數(shù)的輸入
xxx--sparse --seed 0--epochs 1000
其中,--seed和--epochs為普通參數(shù),需要指定具體的數(shù)據(jù)。
--sparse為flag參數(shù),不需要指定具體的值,指定該參數(shù)名后的具體數(shù)值,取決于其action的定義。
如果action = store_true時,--sparse表明參數(shù)值設(shè)定為true
如果action = store_false時,--sparse表明參數(shù)值設(shè)定為false
2.5 命令行參數(shù)的使用
print(args.sparse) print(args.seed) print(args.epochs)
總結(jié)
到此這篇關(guān)于Python命令行解析器argparse詳解的文章就介紹到這了,更多相關(guān)Python令行解析器argparse內(nèi)容請搜索本站以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持本站!
版權(quán)聲明:本站文章來源標(biāo)注為YINGSOO的內(nèi)容版權(quán)均為本站所有,歡迎引用、轉(zhuǎn)載,請保持原文完整并注明來源及原文鏈接。禁止復(fù)制或仿造本網(wǎng)站,禁止在非www.sddonglingsh.com所屬的服務(wù)器上建立鏡像,否則將依法追究法律責(zé)任。本站部分內(nèi)容來源于網(wǎng)友推薦、互聯(lián)網(wǎng)收集整理而來,僅供學(xué)習(xí)參考,不代表本站立場,如有內(nèi)容涉嫌侵權(quán),請聯(lián)系alex-e#qq.com處理。