最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

python通過(guò)getopt模塊如何獲取執(zhí)行的命令參數(shù)詳解

 更新時(shí)間:2017年12月29日 09:00:03   作者:行者無(wú)疆-ITer  
這篇文章主要給大家介紹了關(guān)于python通過(guò)getopt模塊如何獲取執(zhí)行的命令參數(shù)的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧。

前言

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

通常我們可以通過(guò)getopt模塊獲得不同的執(zhí)行命令和參數(shù)。下面話不多說(shuō)了,來(lái)一起看看詳細(xì)的介紹吧。

方法如下:

下面我通過(guò)新建一個(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ù)我們可以通過(guò)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)開(kāi)頭的參數(shù),像上面的例子中qht都以為 -開(kāi)頭,所以qht是該腳本的短命令,短命令又是如何匹配參數(shù)的呢?可以看到例子中shotopts為 "ht:q:" ,這里用命令后面跟著 : 來(lái)申明這個(gè)命令是否需要參數(shù),這里h不需要參數(shù),t和q需要參數(shù),而命令行中緊跟著t和q的參數(shù)即為他們的命令參數(shù),即t的命令參數(shù)為 20171010-20171011 ,q的命令參數(shù)為 24 。

第三個(gè)參數(shù)是longopts,改參數(shù)是個(gè)數(shù)組, 表示長(zhǎng)命令操作符集合。這個(gè)集合要包含命令行中以 -- 符號(hào)開(kāi)頭的參數(shù),url和out都是長(zhǎng)命令,當(dāng)長(zhǎng)命令后面以 = 結(jié)尾是表示他需要一個(gè)參數(shù),比如"url=" ,他匹配命令行中的下一個(gè)參數(shù)https://www.baidu.com.

該方法返回兩個(gè)數(shù)組元素。第一個(gè)返回值,是通過(guò)shortopts和longopts匹配的命令行和其參數(shù)的元祖。該例子的返回值為:

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

第二個(gè)返回值是命令行中未被匹配到的參數(shù),該例子的返回值為:

['file1', 'file2']

通過(guò)返回值我們就可以在自己的代碼中,根據(jù)不同命令去設(shè)計(jì)不同的邏輯處理,相當(dāng)豐富了腳本的可用性。

總結(jié)

以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,如果有疑問(wèn)大家可以留言交流,謝謝大家對(duì)腳本之家的支持。

相關(guān)文章

最新評(píng)論

瑞金市| 辽宁省| 屯门区| 句容市| 武汉市| 来宾市| 广昌县| 汤阴县| 宁安市| 财经| 吴桥县| 大余县| 永吉县| 洛南县| 若羌县| 绵竹市| 长丰县| 梨树县| 五指山市| 曲靖市| 察隅县| 阳朔县| 城市| 绿春县| 蒲城县| 平度市| 天镇县| 郁南县| 九龙城区| 诏安县| 岳普湖县| 日土县| 洛阳市| 会宁县| 胶南市| 峨眉山市| 海盐县| 石门县| 施甸县| 通辽市| 枣阳市|