Optparse 模塊使編寫命令行工具變得容易。它允許在 python 程序中進行參數解析。
- 優化分析使處理命令行參數變得容易。
- python 是默認的。
- 它允許動態數據輸入來改變輸出
代碼:創建OptionParser對象。
Python3
import optparse
parser = optparse.OptionParser()
定義選項:
應使用add_option() 一次添加一個。每個 Option 實例代表一組同義的命令行選項字符串。
創建 Option 實例的方法是:
OptionParser.add_option(option)
OptionParser.add_option(*opt_str, attr=value, ...)
要定義僅包含短選項字符串的選項:
parser.add_option("-f", attr=value, ....)
並定義一個僅包含長選項字符串的選項:
parser.add_option("--foo", attr=value, ....)
標準選項操作:
- “store”: store this option’s argument (default).
- “store_const”: store a constant value.
- “store_true”: store True.
- “store_false”: store False.
- “append”: append this option’s argument to a list.
- “append_const”: append a constant value to a list.
標準選項屬性:
- Option.action: (default: “store”)
- Option.type: (default: “string”)
- Option.dest: (default: derived from option strings)
- Option.default: The value to use for this option’s destination if the option is not seen on the command line.
這是在簡單腳本中使用 optparse 模塊的示例:
Python3
# import OptionParser class
# from optparse module.
from optparse import OptionParser
# create a OptionParser
# class object
parser = OptionParser()
# add options
parser.add_option("-f", "--file",
dest = "filename",
help = "write report to FILE",
metavar = "FILE")
parser.add_option("-q", "--quiet",
action = "store_false",
dest = "verbose", default = True,
help = "don't print status messages to stdout")
(options, args) = parser.parse_args()
通過這幾行代碼,腳本的用戶現在可以在命令行上執行“usual thing”,例如:
<yourscript> --file=outfile -q
讓我們通過一個例子來理解:
代碼:編寫用於打印 n 表的 python 腳本。
Python3
# import optparse module
import optparse
# define a function for
# table of n
def table(n, dest_cheak):
for i in range(1,11):
tab = i*n
if dest_cheak:
print(tab)
return tab
# define a function for
# adding options
def Main():
# create OptionParser object
parser = optparse.OptionParser()
# add options
parser.add_option('-n', dest = 'num',
type = 'int',
help = 'specify the n''th table number to output')
parser.add_option('-o', dest = 'out',
type = 'string',
help = 'specify an output file (Optional)')
parser.add_option("-a", "--all",
action = "store_true",
dest = "print",
default = False,
help = "print all numbers up to N")
(options, args) = parser.parse_args()
if (options.num == None):
print (parser.usage)
exit(0)
else:
number = options.num
# function calling
result = table(number, options.print)
print ("The " + str(number)+ "th table is " + str(result))
if (options.out != None):
# open a file in append mode
f = open(options.out,"a")
# write in the file
f.write(str(result) + '\n')
# Driver code
if __name__ == '__main__':
# function calling
Main()
輸出:
python file_name.py -n 4
python file_name.py -n 4 -o
file.txt已創建
python file_name.py -n 4 -a
了解有關此模塊的更多信息click here.
相關用法
- Python OpenCV cv2.circle()用法及代碼示例
- Python OpenCV cv2.blur()用法及代碼示例
- Python OpenCV cv2.ellipse()用法及代碼示例
- Python OpenCV cv2.cvtColor()用法及代碼示例
- Python OpenCV cv2.copyMakeBorder()用法及代碼示例
- Python OpenCV cv2.imread()用法及代碼示例
- Python OpenCV cv2.imshow()用法及代碼示例
- Python OpenCV cv2.imwrite()用法及代碼示例
- Python OpenCV cv2.putText()用法及代碼示例
- Python OpenCV cv2.rectangle()用法及代碼示例
- Python Operator.countOf用法及代碼示例
- Python OpenCV cv2.arrowedLine()用法及代碼示例
- Python OpenCV cv2.erode()用法及代碼示例
- Python OpenCV cv2.line()用法及代碼示例
- Python OpenCV cv2.flip()用法及代碼示例
- Python OpenCV cv2.transpose()用法及代碼示例
- Python OpenCV cv2.rotate()用法及代碼示例
- Python OpenCV cv2.polylines()用法及代碼示例
- Python OpenCV Canny()用法及代碼示例
- Python OpenCV destroyAllWindows()用法及代碼示例
- Python OpenCV Filter2D()用法及代碼示例
- Python OpenCV getgaussiankernel()用法及代碼示例
- Python OpenCV getRotationMatrix2D()用法及代碼示例
- Python OpenCV getTrackbarPos()用法及代碼示例
- Python OpenCV haveImageReader()用法及代碼示例
注:本文由純淨天空篩選整理自kumar_satyam大神的英文原創作品 Optparse module in Python。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。