python通過getopt模塊如何獲取執行的命令參數詳解

這篇文章主要給大家介紹了關於python通過getopt模塊如何獲取執行的命令參數的相關資料,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨着小編來一起學習學習吧。
前言

python腳本和shell腳本一樣可以獲取命令行的參數,根據不同的參數,執行不同的邏輯處理。

通常我們可以通過getopt模塊獲得不同的執行命令和參數。下面話不多說了,來一起看看詳細的介紹吧。
方法如下:

下面我通過新建一個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

執行命令 :./test3.py -t 20171010-20171011 -q 24 -h --url=https://www.baidu.com --out file1 file2
執行結果 :

['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.

可以發現getopt方法需要三個參數。

第一個參數是args是將要解析的命令行參數我們可以通過sys.argv獲取執行的相關參數

['D:/GitReposity/hope_crontab_repo/sla_channel/test3.py', '-t', '20171010-20171011', '-q', '24', '-h', '--url=https://www.baidu.com']

可以看出參數列表的第一個值是腳本執行的完全路徑名,剩餘參數是以空格分割的命令行參數。爲了獲得有效參數,通常args參數的值取sys.argv[1:] 。

第二個參數是shortopts是短命令操作符,他的參數要包含命令行中以 -符號開頭的參數,像上面的例子中qht都以爲 -開頭,所以qht是該腳本的短命令,短命令又是如何匹配參數的呢?可以看到例子中shotopts爲 “ht:q:” ,這裏用命令後面跟着 : 來申明這個命令是否需要參數,這裏h不需要參數,t和q需要參數,而命令行中緊跟着t和q的參數即爲他們的命令參數,即t的命令參數爲 20171010-20171011 ,q的命令參數爲 24 。

第三個參數是longopts,改參數是個數組, 表示長命令操作符集合。這個集合要包含命令行中以 – 符號開頭的參數,url和out都是長命令,當長命令後面以 = 結尾是表示他需要一個參數,比如"url=" ,他匹配命令行中的下一個參數https://www.baidu.com.

該方法返回兩個數組元素。第一個返回值,是通過shortopts和longopts匹配的命令行和其參數的元祖。該例子的返回值爲:

[('-t', '20171010-20171011'), ('-q', '24'), ('-h', ''), ('--url', 'https://www.baidu.com'), ('--out', '')]
['file1', 'file2']

第二個返回值是命令行中未被匹配到的參數,該例子的返回值爲:

['file1', 'file2']

通過返回值我們就可以在自己的代碼中,根據不同命令去設計不同的邏輯處理,相當豐富了腳本的可用性。

推薦我們的Python學習扣qun:913066266 ,看看前輩們是如何學習的!從基礎的python腳本到web開發、爬蟲、django、數據挖掘等【PDF,實戰源碼】,零基礎到項目實戰的資料都有整理。送給每一位python的小夥伴!每天都有大牛定時講解Python技術,分享一些學習的方法和需要注意的小細節,點擊加入我們的 python學習者聚集地

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章