python通過getopt模塊如何獲取執(zhí)行的命令參數(shù)詳解
前言
python腳本和shell腳本一樣可以獲取命令行的參數(shù),根據(jù)不同的參數(shù),執(zhí)行不同的邏輯處理。
通常我們可以通過getopt模塊獲得不同的執(zhí)行命令和參數(shù)。下面話不多說了,來一起看看詳細(xì)的介紹吧。
方法如下:
下面我通過新建一個(gè)test.py的腳本解釋下這個(gè)模塊的的使用
#!/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方法需要三個(gè)參數(shù)。
第一個(gè)參數(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ù)列表的第一個(gè)值是腳本執(zhí)行的完全路徑名,剩余參數(shù)是以空格分割的命令行參數(shù)。為了獲得有效參數(shù),通常args參數(shù)的值取sys.argv[1:]
。
第二個(gè)參數(shù)是shortopts是短命令操作符,他的參數(shù)要包含命令行中以 -符號(hào)開頭的參數(shù),像上面的例子中qht都以為 -開頭,所以qht是該腳本的短命令,短命令又是如何匹配參數(shù)的呢?可以看到例子中shotopts為 "ht:q:" ,這里用命令后面跟著 : 來申明這個(gè)命令是否需要參數(shù),這里h不需要參數(shù),t和q需要參數(shù),而命令行中緊跟著t和q的參數(shù)即為他們的命令參數(shù),即t的命令參數(shù)為 20171010-20171011 ,q的命令參數(shù)為 24 。
第三個(gè)參數(shù)是longopts,改參數(shù)是個(gè)數(shù)組, 表示長命令操作符集合。這個(gè)集合要包含命令行中以 -- 符號(hào)開頭的參數(shù),url和out都是長命令,當(dāng)長命令后面以 = 結(jié)尾是表示他需要一個(gè)參數(shù),比如"url=" ,他匹配命令行中的下一個(gè)參數(shù)https://www.baidu.com.
該方法返回兩個(gè)數(shù)組元素。第一個(gè)返回值,是通過shortopts和longopts匹配的命令行和其參數(shù)的元祖。該例子的返回值為:
[('-t', '20171010-20171011'), ('-q', '24'), ('-h', ''), ('--url', 'https://www.baidu.com'), ('--out', '')] ['file1', 'file2']
第二個(gè)返回值是命令行中未被匹配到的參數(shù),該例子的返回值為:
['file1', 'file2']
通過返回值我們就可以在自己的代碼中,根據(jù)不同命令去設(shè)計(jì)不同的邏輯處理,相當(dāng)豐富了腳本的可用性。
總結(jié)
以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,如果有疑問大家可以留言交流,謝謝大家對(duì)腳本之家的支持。
- python使用paramiko模塊實(shí)現(xiàn)ssh遠(yuǎn)程登陸上傳文件并執(zhí)行
- Python2.x利用commands模塊執(zhí)行Linux shell命令
- python SSH模塊登錄,遠(yuǎn)程機(jī)執(zhí)行shell命令實(shí)例解析
- 使用Python paramiko模塊利用多線程實(shí)現(xiàn)ssh并發(fā)執(zhí)行操作
- python中使用paramiko模塊并實(shí)現(xiàn)遠(yuǎn)程連接服務(wù)器執(zhí)行上傳下載功能
- 執(zhí)行Python程序時(shí)模塊報(bào)錯(cuò)問題
- Python-jenkins模塊獲取jobs的執(zhí)行狀態(tài)操作
- Python代碼執(zhí)行時(shí)間測量模塊timeit用法解析
- 解決Python paramiko 模塊遠(yuǎn)程執(zhí)行ssh 命令 nohup 不生效的問題
- Python實(shí)現(xiàn)以主程序的形式執(zhí)行模塊
相關(guān)文章
一文教你Python如何創(chuàng)建屬于自己的IP池
這篇文章主要為大家詳細(xì)介紹了python如何創(chuàng)建屬于自己的IP池,文中的示例代碼講解詳細(xì),對(duì)我們學(xué)習(xí)或工作有一定參考價(jià)值,需要的可以參考一下2022-04-04Python functools模塊學(xué)習(xí)總結(jié)
這篇文章主要介紹了Python functools模塊學(xué)習(xí)總結(jié),本文講解了functools.partial、functool.update_wrapper、functool.wraps、functools.reduce、functools.cmp_to_key、functools.total_ordering等方法的使用實(shí)例,需要的朋友可以參考下2015-05-05Windows下實(shí)現(xiàn)pytorch環(huán)境搭建
這篇文章主要介紹了Windows下實(shí)現(xiàn)pytorch環(huán)境搭建,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-04-04Python3合并兩個(gè)有序數(shù)組代碼實(shí)例
這篇文章主要介紹了Python3合并兩個(gè)有序數(shù)組代碼實(shí)例,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-08-08TensorFlow實(shí)現(xiàn)簡單卷積神經(jīng)網(wǎng)絡(luò)
這篇文章主要為大家詳細(xì)介紹了TensorFlow實(shí)現(xiàn)簡單卷積神經(jīng)網(wǎng)絡(luò),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-05-05python計(jì)算導(dǎo)數(shù)并繪圖的實(shí)例
今天小編就為大家分享一篇python計(jì)算導(dǎo)數(shù)并繪圖的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-02-02淺析Python自帶性能強(qiáng)悍的標(biāo)準(zhǔn)庫itertools
itertools是python內(nèi)置的模塊,使用簡單且功能強(qiáng)大。這篇文章就主要介紹了通過itertools實(shí)現(xiàn)可迭代對(duì)象的無限迭代、有限迭代和排列組合。感興趣的同學(xué)可以關(guān)注一下2021-12-12python檢查序列seq是否含有aset中項(xiàng)的方法
這篇文章主要介紹了python檢查序列seq是否含有aset中項(xiàng)的方法,涉及Python針對(duì)序列的相關(guān)判斷技巧,需要的朋友可以參考下2015-06-06