Python script parameter passing

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.