欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

 更新時間:2017年12月29日 09:00:03   作者:行者無疆-ITer  
這篇文章主要給大家介紹了關于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如何創(chuàng)建屬于自己的IP池

    一文教你Python如何創(chuàng)建屬于自己的IP池

    這篇文章主要為大家詳細介紹了python如何創(chuàng)建屬于自己的IP池,文中的示例代碼講解詳細,對我們學習或工作有一定參考價值,需要的可以參考一下
    2022-04-04
  • 樹莓派實現(xiàn)移動拍照

    樹莓派實現(xiàn)移動拍照

    這篇文章主要為大家詳細介紹了樹莓派實現(xiàn)移動拍照,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2019-06-06
  • Python 線程池用法簡單示例

    Python 線程池用法簡單示例

    這篇文章主要介紹了Python 線程池用法,結合簡單實例形式分析了Python線程池相關使用技巧與操作注意事項,需要的朋友可以參考下
    2019-10-10
  • Python functools模塊學習總結

    Python functools模塊學習總結

    這篇文章主要介紹了Python functools模塊學習總結,本文講解了functools.partial、functool.update_wrapper、functool.wraps、functools.reduce、functools.cmp_to_key、functools.total_ordering等方法的使用實例,需要的朋友可以參考下
    2015-05-05
  • Windows下實現(xiàn)pytorch環(huán)境搭建

    Windows下實現(xiàn)pytorch環(huán)境搭建

    這篇文章主要介紹了Windows下實現(xiàn)pytorch環(huán)境搭建,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2021-04-04
  • Python3合并兩個有序數(shù)組代碼實例

    Python3合并兩個有序數(shù)組代碼實例

    這篇文章主要介紹了Python3合并兩個有序數(shù)組代碼實例,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2020-08-08
  • TensorFlow實現(xiàn)簡單卷積神經(jīng)網(wǎng)絡

    TensorFlow實現(xiàn)簡單卷積神經(jīng)網(wǎng)絡

    這篇文章主要為大家詳細介紹了TensorFlow實現(xiàn)簡單卷積神經(jīng)網(wǎng)絡,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-05-05
  • python計算導數(shù)并繪圖的實例

    python計算導數(shù)并繪圖的實例

    今天小編就為大家分享一篇python計算導數(shù)并繪圖的實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-02-02
  • 淺析Python自帶性能強悍的標準庫itertools

    淺析Python自帶性能強悍的標準庫itertools

    itertools是python內置的模塊,使用簡單且功能強大。這篇文章就主要介紹了通過itertools實現(xiàn)可迭代對象的無限迭代、有限迭代和排列組合。感興趣的同學可以關注一下
    2021-12-12
  • python檢查序列seq是否含有aset中項的方法

    python檢查序列seq是否含有aset中項的方法

    這篇文章主要介紹了python檢查序列seq是否含有aset中項的方法,涉及Python針對序列的相關判斷技巧,需要的朋友可以參考下
    2015-06-06

最新評論