Environment: Python
Library: Optparse
Effect: Python xxx.py-parameter1 parameter 1-parameter2 parameter 2 ....
Talk less, directly on the code OK?
xxx.py
#-*-coding:utf-8-*-ImportOptparsedefMain (): Parser= Optparse. Optionparser ('%prog'+'- H ') parser.add_option ('- H', dest='Hname', type='string', help='need a host (example:www.xxx.com)') parser.add_option ('- u', dest='uname', type='string', help='need a URL (example:/xxx.html)') (options, args)=Parser.parse_args ()if(Options.hname = = None) | (Options.uname = =None):PrintParser.usage exit (0)Else: Host=options.hname URL=Options.uname##可以开始使用参数做你想做的操作if __name__=='__main__': Main ()
Example of getting hints through Python xxx.py-h is called start_test.py
# #提示信息完全按照脚本写的, you can modify it yourself.
Run time, direct incoming can, of course, the absence of an arbitrary parameter, the program will stop, of course, you can set the non-mandatory parameters, set the default value, improvise.
Type of delivery, set in type, String,int .... The passed parameters are also passed as type.
The script is relatively simple, but it's quite practical.
Python script parameter passing