1、 Import optionparser : from optparse import OptionParser
2、 structure optionparser The object of :parser = OptionParser()
3、optionparser Add in object option :parser.add_option(...), Add the command line and related help information
Each command line parameter consists of a parameter name string and parameter attributes , Such as -f perhaps file Respectively represent the long and short parameter names :
parser.add_option("-f", "--file", ...)
4、 call optionparser Analytic function of :
(options, args) = parser.parse_args()
5、 stay options Using the resolved options, stay args Use other args.
options It's a dictionary , Its key The value is app_options() Function dest Parameter value of the property ;value The value corresponds to app_options() Function defalut Parameter value of the property , Or parameters passed in from the command line
args By positional arguments A list of components .
import sys
from optparse import OptionParser
parser = OptionParser(usage="%prog [-v]", version="%prog 1.0")
parser.add_option("-s", "--source_addr", help="source address", default="192.168.1.100")
(options, args) = parser.parse_args(sys.argv)
source_addr = options.source_addr
【1】Python Of OptionParser modular _syousetu The blog of -CSDN Blog _parser modular
【2】python Medium optionParser modular _weixin_30809333 The blog of -CSDN Blog