python通過getopt模塊如何獲取執(zhí)行的命令參數(shù)詳解
前言
python腳本和shell腳本一樣可以獲取命令行的參數(shù),根據(jù)不同的參數(shù),執(zhí)行不同的邏輯處理。
通常我們可以通過getopt模塊獲得不同的執(zhí)行命令和參數(shù)。下面話不多說了,來一起看看詳細的介紹吧。
方法如下:
下面我通過新建一個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í)行結果 :
['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í)行的相關參數(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都是長命令,當長命令后面以 = 結尾是表示他需要一個參數(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ù)不同命令去設計不同的邏輯處理,相當豐富了腳本的可用性。
總結
以上就是這篇文章的全部內容了,希望本文的內容對大家的學習或者工作具有一定的參考學習價值,如果有疑問大家可以留言交流,謝謝大家對腳本之家的支持。
- 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)遠程連接服務器執(zhí)行上傳下載功能
- 執(zhí)行Python程序時模塊報錯問題
- Python-jenkins模塊獲取jobs的執(zhí)行狀態(tài)操作
- Python代碼執(zhí)行時間測量模塊timeit用法解析
- 解決Python paramiko 模塊遠程執(zhí)行ssh 命令 nohup 不生效的問題
- Python實現(xiàn)以主程序的形式執(zhí)行模塊
相關文章
一文教你Python如何創(chuàng)建屬于自己的IP池
這篇文章主要為大家詳細介紹了python如何創(chuàng)建屬于自己的IP池,文中的示例代碼講解詳細,對我們學習或工作有一定參考價值,需要的可以參考一下2022-04-04Windows下實現(xiàn)pytorch環(huán)境搭建
這篇文章主要介紹了Windows下實現(xiàn)pytorch環(huán)境搭建,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2021-04-04TensorFlow實現(xiàn)簡單卷積神經(jīng)網(wǎng)絡
這篇文章主要為大家詳細介紹了TensorFlow實現(xiàn)簡單卷積神經(jīng)網(wǎng)絡,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-05-05