日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學(xué)無先后,達(dá)者為師

網(wǎng)站首頁 編程語言 正文

Python?optparse模塊及簡單使用_python

作者:Python熱愛者 ? 更新時(shí)間: 2023-01-05 編程語言

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=‘a(chǎn)aa’, help=‘a(chǎn)aa,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

原文鏈接:https://blog.csdn.net/qdPython/article/details/128116943

欄目分類
最近更新