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

Python optparse模塊及簡單使用

 更新時(shí)間:2022年12月08日 11:30:35   作者:Python熱愛者  
optparse,是一個更夠讓程序設(shè)計(jì)人員輕松設(shè)計(jì)出簡單明了、易于使用、符合標(biāo)準(zhǔn)的Unix命令例程式的Python模塊,生成使用和幫助信息,這篇文章主要介紹了Python optparse模塊簡單使用,需要的朋友可以參考下

optparse,是一個更夠讓程序設(shè)計(jì)人員輕松設(shè)計(jì)出簡單明了、易于使用、符合標(biāo)準(zhǔn)的Unix命令例程式的Python模塊,生成使用和幫助信息。

參數(shù)說明

  • dest:用于保存輸入的臨時(shí)變量,其值通過options的屬性進(jìn)行訪問,存儲的內(nèi)容是dest之前輸入的參數(shù),多個參數(shù)用逗號分隔
  • type: 用于檢查命令行參數(shù)傳入的參數(shù)的數(shù)據(jù)類型是否符合要求,有 string,int,float 等類型
  • help:用于生成幫助信息
  • default: 給dest的默認(rèn)值,如果用戶沒有在命令行參數(shù)給dest分配值,則使用默認(rèn)值

函數(shù)說明

  • parse = optparse.OptionParser(usage, version=“%prog 版本解釋”)
  • parse.add_option(‘-a’, ‘–aaa’, dest=‘aaa’, help=‘aaa,aaa’)
  • group1 = optparse.OptionGroup(parse, “fff”, ‘dddd’)
  • group1.add_option(‘-s’, ‘–server’, dest=‘hhhh’, help=“dddd”)
  • parse.add_option_group(group1)
  • options, args = parse.parse_args()

簡單使用

from optparse import OptionParser
from optparse import OptionGroup

usage = 'Usage: %prog [options] arg1 arg2 ...'

parser = OptionParser(usage,version='%prog 1.0')
#通過OptionParser類創(chuàng)建parser實(shí)例,初始參數(shù)usage中的%prog等同于os.path.basename(sys.argv[0]),即
#你當(dāng)前所運(yùn)行的腳本的名字,version參數(shù)用來顯示當(dāng)前腳本的版本。

'''
添加參數(shù),-f、--file是長短options,有一即可。

action用來表示將option后面的值如何處理,比如:
XXX.py -f test.txt
經(jīng)過parser.parse_args()處理后,則將test.txt這個值存儲進(jìn)-f所代表的一個對象,即定義-f中的dest
即option.filename = 'test.txt'
action的常用選項(xiàng)還有store_true,store_false等,這兩個通常在布爾值的選項(xiàng)中使用。

metavar僅在顯示幫助中有用,如在顯示幫助時(shí)會有:
-f FILE, --filename=FILE    write output to FILE
-m MODE, --mode=MODE  interaction mode: novice, intermediate, or expert
                        [default: intermediate]
如果-f這一項(xiàng)沒有metavr參數(shù),則在上面會顯示為-f FILENAME --filename=FILENAME,即會顯示dest的值

defalut是某一選項(xiàng)的默認(rèn)值,當(dāng)調(diào)用腳本時(shí),參數(shù)沒有指定值時(shí),即采用default的默認(rèn)值。
'''

parser.add_option('-f','--file',
                  action='store',dest='filename',
                  metavar='FILE',help='write output to FILE')

parser.add_option('-m','--mode',
                  default = 'intermediate',
                  help='interaction mode:novice,intermediate,or expert [default:%default]')
parser.add_option('-v','--verbose',
                  action='store_true',dest='verbose',default=True,
                  help='make lots of noise [default]')

parser.add_option('-q','--quiet',
                  action='store_false',dest='verbose',
                  help="be vewwy quiet (I'm hunting wabbits)")

group = OptionGroup(parser,'Dangerous Options',
                    'Caution: use these options at your own risk.'
                    'It is believed that some of them bite.')
#調(diào)用OptionGroup類,定制以組顯示的option

group.add_option('-g',action='store_true',help='Group option.')
#添加option
parser.add_option_group(group)
#將剛定制的groupoption加入parser中

group = OptionGroup(parser,'Debug Options')
group.add_option('-d','--debug',action='store_true',
                 help='Print debug information.')
group.add_option('-s','--sql',action='store_true',
                 help='Print all SQL statements executed')
group.add_option('-e',action='store_true',help='Print every action done')
parser.add_option_group(group)

(options,args) = parser.parse_args()
#解析腳本輸入的參數(shù)值,options是一個包含了option值的對象
#args是一個位置參數(shù)的列表

python.exe xxx.py --help 顯示結(jié)果如下:

Usage: test_optparse.py [options] arg1 arg2 ...

Options:
  --version             show program's version number and exit
  -h, --help            show this help message and exit
  -f FILE, --file=FILE  write output to FILE
  -m MODE, --mode=MODE  interaction mode:novice,intermediate,or expert
                        [default:intermediate]
  -v, --verbose         make lots of noise [default]
  -q, --quiet           be vewwy quiet (I'm hunting wabbits)

  Dangerous Options:
    Caution: use these options at your own risk.It is believed that some
    of them bite.

    -g                  Group option.

  Debug Options:
    -d, --debug         Print debug information.
    -s, --sql           Print all SQL statements executed
    -e                  Print every action done

到此這篇關(guān)于Python optparse模塊及簡單使用的文章就介紹到這了,更多相關(guān)Python optparse模塊內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Django模板中變量的運(yùn)算實(shí)現(xiàn)

    Django模板中變量的運(yùn)算實(shí)現(xiàn)

    這篇文章主要介紹了Django模板中變量的運(yùn)算,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-04-04
  • Python素?cái)?shù)檢測的方法

    Python素?cái)?shù)檢測的方法

    這篇文章主要介紹了Python素?cái)?shù)檢測的方法,實(shí)例分析了Python素?cái)?shù)檢測的相關(guān)技巧,需要的朋友可以參考下
    2015-05-05
  • django連接mysql數(shù)據(jù)庫及建表操作實(shí)例詳解

    django連接mysql數(shù)據(jù)庫及建表操作實(shí)例詳解

    這篇文章主要介紹了django連接mysql數(shù)據(jù)庫及建表操作,結(jié)合實(shí)例形式詳細(xì)分析了Django框架連接mysql數(shù)據(jù)庫、創(chuàng)建與查詢數(shù)據(jù)表相關(guān)操作實(shí)現(xiàn)技巧,需要的朋友可以參考下
    2019-12-12
  • python爬取數(shù)據(jù)中的headers和代理IP問題分析

    python爬取數(shù)據(jù)中的headers和代理IP問題分析

    這篇文章主要為大家介紹了python爬取數(shù)據(jù)中的headers和代理IP問題分析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-06-06
  • 詳解python函數(shù)的閉包問題(內(nèi)部函數(shù)與外部函數(shù)詳述)

    詳解python函數(shù)的閉包問題(內(nèi)部函數(shù)與外部函數(shù)詳述)

    這篇文章主要介紹了python函數(shù)的閉包問題,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-05-05
  • python周期任務(wù)調(diào)度工具Schedule使用詳解

    python周期任務(wù)調(diào)度工具Schedule使用詳解

    這篇文章主要為大家介紹了python周期任務(wù)調(diào)度工具Schedule的使用及示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2021-11-11
  • Python數(shù)據(jù)分析之分析千萬級淘寶數(shù)據(jù)

    Python數(shù)據(jù)分析之分析千萬級淘寶數(shù)據(jù)

    網(wǎng)購已經(jīng)成為人們生活不可或缺的一部分,本次項(xiàng)目基于淘寶app平臺數(shù)據(jù),通過相關(guān)指標(biāo)對用戶行為進(jìn)行分析,從而探索用戶相關(guān)行為模式。感興趣的可以學(xué)習(xí)一下
    2022-03-03
  • python中顯存回收問題解決方法

    python中顯存回收問題解決方法

    這篇文章主要介紹了python中顯存回收問題解決方法,文章提供一個實(shí)例問題和處理的思路,具有一定的參考價(jià)值,需要的小伙伴可以參考一下
    2021-12-12
  • PyTorch中Torch.arange函數(shù)詳解

    PyTorch中Torch.arange函數(shù)詳解

    PyTorch是由Facebook開發(fā)的開源機(jī)器學(xué)習(xí)庫,它用于深度神經(jīng)網(wǎng)絡(luò)和自然語言處理,下面這篇文章主要給大家介紹了關(guān)于PyTorch中Torch.arange函數(shù)詳解的相關(guān)資料,需要的朋友可以參考下
    2023-02-02
  • Python3實(shí)現(xiàn)抓取javascript動態(tài)生成的html網(wǎng)頁功能示例

    Python3實(shí)現(xiàn)抓取javascript動態(tài)生成的html網(wǎng)頁功能示例

    這篇文章主要介紹了Python3實(shí)現(xiàn)抓取javascript動態(tài)生成的html網(wǎng)頁功能,結(jié)合實(shí)例形式分析了Python3使用selenium庫針對javascript動態(tài)生成的HTML網(wǎng)頁元素進(jìn)行抓取的相關(guān)操作技巧,需要的朋友可以參考下
    2017-08-08

最新評論