python通過getopt模塊如何獲取執(zhí)行的命令參數(shù)詳解
前言
python腳本和shell腳本一樣可以獲取命令行的參數(shù),根據(jù)不同的參數(shù),執(zhí)行不同的邏輯處理。
通常我們可以通過getopt模塊獲得不同的執(zhí)行命令和參數(shù)。下面話不多說了,來一起看看詳細(xì)的介紹吧。
方法如下:
下面我通過新建一個test.py的腳本解釋下這個模塊的的使用
#!/usr/bin/python # -*- coding: utf-8 -*- import sys import getopt if __name__=='__main__': print sys.argv opts, args = getopt.getopt(sys.argv[1:], "ht:q:", ["url=",'out']) print opts print args
執(zhí)行命令 :
./test3.py -t 20171010-20171011 -q 24 -h --url=https://www.baidu.com --out file1 file2
執(zhí)行結(jié)果 :
['D:/GitReposity/hope_crontab_repo/sla_channel/test3.py', '-t', '20171010-20171011', '-q', '24', '-h', '--url=https://www.baidu.com', '--out', 'file1', 'file2']
[('-t', '20171010-20171011'), ('-q', '24'), ('-h', ''), ('--url', 'https://www.baidu.com'), ('--out', '')]
['file1', 'file2']
我們查看getopt模塊的官方文檔
def getopt(args, shortopts, longopts = [])
Parses command line options and parameter list. args is the
argument list to be parsed, without the leading reference to the
running program. Typically, this means "sys.argv[1:]". shortopts
is the string of option letters that the script wants to
recognize, with options that require an argument followed by a
colon (i.e., the same format that Unix getopt() uses). If
specified, longopts is a list of strings with the names of the
long options which should be supported. The leading '--'
characters should not be included in the option name. Options
which require an argument should be followed by an equal sign
('=').
The return value consists of two elements: the first is a list of
(option, value) pairs; the second is the list of program arguments
left after the option list was stripped (this is a trailing slice
of the first argument). Each option-and-value pair returned has
the option as its first element, prefixed with a hyphen (e.g.,
'-x'), and the option argument as its second element, or an empty
string if the option has no argument. The options occur in the
list in the same order in which they were found, thus allowing
multiple occurrences. Long and short options may be mixed.
可以發(fā)現(xiàn)getopt方法需要三個參數(shù)。
第一個參數(shù)是args是將要解析的命令行參數(shù)我們可以通過sys.argv獲取執(zhí)行的相關(guān)參數(shù)
['D:/GitReposity/hope_crontab_repo/sla_channel/test3.py', '-t', '20171010-20171011', '-q', '24', '-h', '--url=https://www.baidu.com']
可以看出參數(shù)列表的第一個值是腳本執(zhí)行的完全路徑名,剩余參數(shù)是以空格分割的命令行參數(shù)。為了獲得有效參數(shù),通常args參數(shù)的值取sys.argv[1:] 。
第二個參數(shù)是shortopts是短命令操作符,他的參數(shù)要包含命令行中以 -符號開頭的參數(shù),像上面的例子中qht都以為 -開頭,所以qht是該腳本的短命令,短命令又是如何匹配參數(shù)的呢?可以看到例子中shotopts為 "ht:q:" ,這里用命令后面跟著 : 來申明這個命令是否需要參數(shù),這里h不需要參數(shù),t和q需要參數(shù),而命令行中緊跟著t和q的參數(shù)即為他們的命令參數(shù),即t的命令參數(shù)為 20171010-20171011 ,q的命令參數(shù)為 24 。
第三個參數(shù)是longopts,改參數(shù)是個數(shù)組, 表示長命令操作符集合。這個集合要包含命令行中以 -- 符號開頭的參數(shù),url和out都是長命令,當(dāng)長命令后面以 = 結(jié)尾是表示他需要一個參數(shù),比如"url=" ,他匹配命令行中的下一個參數(shù)https://www.baidu.com.
該方法返回兩個數(shù)組元素。第一個返回值,是通過shortopts和longopts匹配的命令行和其參數(shù)的元祖。該例子的返回值為:
[('-t', '20171010-20171011'), ('-q', '24'), ('-h', ''), ('--url', 'https://www.baidu.com'), ('--out', '')]
['file1', 'file2']
第二個返回值是命令行中未被匹配到的參數(shù),該例子的返回值為:
['file1', 'file2']
通過返回值我們就可以在自己的代碼中,根據(jù)不同命令去設(shè)計不同的邏輯處理,相當(dāng)豐富了腳本的可用性。
總結(jié)
以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,如果有疑問大家可以留言交流,謝謝大家對腳本之家的支持。
- python使用paramiko模塊實現(xiàn)ssh遠程登陸上傳文件并執(zhí)行
- Python2.x利用commands模塊執(zhí)行Linux shell命令
- python SSH模塊登錄,遠程機執(zhí)行shell命令實例解析
- 使用Python paramiko模塊利用多線程實現(xiàn)ssh并發(fā)執(zhí)行操作
- python中使用paramiko模塊并實現(xiàn)遠程連接服務(wù)器執(zhí)行上傳下載功能
- 執(zhí)行Python程序時模塊報錯問題
- Python-jenkins模塊獲取jobs的執(zhí)行狀態(tài)操作
- Python代碼執(zhí)行時間測量模塊timeit用法解析
- 解決Python paramiko 模塊遠程執(zhí)行ssh 命令 nohup 不生效的問題
- Python實現(xiàn)以主程序的形式執(zhí)行模塊
相關(guān)文章
python實現(xiàn)判斷數(shù)組是否包含指定元素的方法
這篇文章主要介紹了python實現(xiàn)判斷數(shù)組是否包含指定元素的方法,涉及Python中in的使用技巧,具有一定參考借鑒價值,需要的朋友可以參考下2015-07-07
詳解pandas中MultiIndex和對象實際索引不一致問題
這篇文章主要介紹了詳解pandas中MultiIndex和對象實際索引不一致問題,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-07-07
使用python字典統(tǒng)計CSV數(shù)據(jù)的步驟和示例代碼
為了使用Python字典來統(tǒng)計CSV數(shù)據(jù),我們可以使用內(nèi)置的csv模塊來讀取CSV文件,并使用字典來存儲統(tǒng)計信息,以下是一個詳細(xì)的步驟和完整的代碼示例,需要的朋友可以參考下2024-12-12
Tensorflow實現(xiàn)神經(jīng)網(wǎng)絡(luò)擬合線性回歸
這篇文章主要為大家詳細(xì)介紹了Tensorflow實現(xiàn)神經(jīng)網(wǎng)絡(luò)擬合線性回歸,具有一定的參考價值,感興趣的小伙伴們可以參考一下2019-07-07
python數(shù)據(jù)處理 根據(jù)顏色對圖片進行分類的方法
今天小編就為大家分享一篇python數(shù)據(jù)處理 根據(jù)顏色對圖片進行分類的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-12-12

