Python argparse模塊實現(xiàn)解析命令行參數(shù)方法詳解
argparse是Python的一個標(biāo)準(zhǔn)模塊,用于解析命令行參數(shù),即解析sys.argv中定義的參數(shù)。實現(xiàn)在:傳送門
argparse模塊還會自動生成幫助和使用信息,即在最后加-h或--help。當(dāng)用戶輸入的參數(shù)無效時,會觸發(fā)error,并給出出錯原因。
python test_argparse.py -h python test_argparse.py --help
使用argparse的步驟:
1.創(chuàng)建解析器:argparse.ArgumentParser(),ArgumentParser是個class,構(gòu)造它時有很多參數(shù)可以指定,若不指定這些參數(shù)都使用默認值,如description=None:此python程序功能的描述;add_help=True:添加-h/--help選項
parser = argparse.ArgumentParser(description="test argparse's use", add_help=True)
2.添加參數(shù):parser.add_argument(*args, **kwargs):*args允許函數(shù)接受任意數(shù)量的位置參數(shù)(positional arguments),**kwargs允許函數(shù)接受任意數(shù)量的關(guān)鍵字參數(shù)。Python不允許位置參數(shù)跟在關(guān)鍵字參數(shù)之后。
(1).name or flags:一個名字,位置參數(shù)(positional arguments),不帶有"-"或"--",如foo;一個可選字符串的列表,如-f, --foo。當(dāng)parse_args()被調(diào)用時,可選參數(shù)會以"-"前綴識別,剩下的參數(shù)則會被假定為位置參數(shù)。
parser.add_argument("integers", metavar="N", type=int, nargs="+", help="an integer for the accumulator") # positional argument parser.add_argument("--sum", dest="accumulate", action="store_const", const=sum, default=max, help="sum the integers(default: find the max)") # optional argument args = parser.parse_args() print(args.accumulate(args.integers)) # print either the sum or the max of the command-line integers
(2).action:ArgumentParser對象將命令行參數(shù)與action相關(guān)聯(lián)。這些action可以做與它們相關(guān)聯(lián)的命令行參數(shù)的任何事,盡管大多數(shù)action只是簡單的向parse_args()返回的對象上添加一個屬性。action關(guān)鍵字參數(shù)指定應(yīng)如何處理命令行參數(shù)。默認的action是"store",存儲參數(shù)的值。
(3).nargs:此關(guān)鍵字參數(shù)將不同數(shù)量的命令行參數(shù)與單個操作相關(guān)聯(lián)。
"?":如果不存在命令行參數(shù),則會使用default值;還有一種情況,"-"選項字符串存在,但后面沒有跟著命令行參數(shù),則會使用const值。
"*":所有命令行參數(shù)都收集到一個列表中。
"+":與"*"類似,但至少存在一個命令行參數(shù),否則會產(chǎn)生錯誤消息。
parser.add_argument('--foo', nargs='?', const='c', default='d') args = parser.parse_args() print(args.foo) # print 'c' or 'd' or command-line input
(4).const:此關(guān)鍵字參數(shù)用于保存不是從命令行中讀取但被各種ArgumentParser操作所需的常量值。對'store_const'和'append_const' action,const關(guān)鍵字參數(shù)必須給出;對其它action,默認為None。
(5).default:此關(guān)鍵字參數(shù)用于在命令行參數(shù)不存在應(yīng)使用的值,默認值為None。
(6).type:此關(guān)鍵字參數(shù)允許執(zhí)行任何必要的類型檢查和類型轉(zhuǎn)換。
(7).choices:某些命令行參數(shù)應(yīng)當(dāng)從一組受限值中選擇,這些可以通過將容器對象作為choices關(guān)鍵字參數(shù)傳遞給add_argument()來處理。當(dāng)執(zhí)行命令行解析時,參數(shù)值將被檢查,如果參數(shù)不是可接受的值之一將觸發(fā)錯誤消息。
parser.add_argument('addr', type=str, choices=['csdn', 'github']) args = parser.parse_args() print("addr:", args.addr)
(8).required:通常,argparse模塊假定"-f"和"--bar"等標(biāo)志表示可選參數(shù),在命令行中始終可以省略這些參數(shù)。要讓一個選項成為必需的,則可以將required關(guān)鍵字參數(shù)指定為True。
(9).help:包含參數(shù)簡短描述的字符串。通常在命令行中使用-h或--help時,這些help描述將與每個參數(shù)一起顯示。
(10).metavar:當(dāng)ArgumentParser生成幫助消息時,它需要某種方式來引用每個預(yù)期的參數(shù)。默認情況下,ArgumentParser對象使用dest值作為每個對象的"name"。默認情況下,對于位置參數(shù)action,直接使用dest值,對于可選參數(shù)action,dest值是大寫的??梢允褂胢etavar來指定一個替代名稱。
parser.add_argument('--foo') parser.add_argument('--bar', metavar='XXX') args = parser.parse_args()
(11).dest:大多數(shù)ArgumentParser操作都會添加一些值作為parse_args()返回的對象的一個屬性。此屬性的名稱由add_argument()的dest關(guān)鍵字參數(shù)確定。
3.解析參數(shù):parser.parse_args(),將參數(shù)字符串轉(zhuǎn)換為對象并將其設(shè)為命名空間的屬性,返回帶有成員的命名空間。
更多介紹參考:傳送門
到此這篇關(guān)于Python argparse模塊實現(xiàn)解析命令行參數(shù)方法詳解的文章就介紹到這了,更多相關(guān)Python argparse模塊 內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
python用10行代碼實現(xiàn)對黃色圖片的檢測功能
這篇文章主要介紹了python用10行代碼實現(xiàn)對黃色圖片的檢測功能,涉及Python基于圖片庫PIL對圖片的檢測技巧,具有一定參考借鑒價值,需要的朋友可以參考下2015-08-08Python for循環(huán)與getitem的關(guān)系詳解
這篇文章主要介紹了Python for循環(huán)與getitem的關(guān)系詳解,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-01-01Windows安裝Python、pip、easy_install的方法
這篇文章主要介紹了Windows安裝Python、pip、easy_install的方法,需要的朋友可以參考下2017-03-03