"Python" reads command-line arguments, sets the Python runtime parameters in Pydev

Source: Internet
Author: User

Sometimes we write a command-line program that requires batch execution, where the Python program reads the command-line arguments and then writes a command-line batch execution script. bat, which is actually a command-and-suffix text file, is useful for throwing multiple test cases into a Python program.

For example, if you have a cmdargs.py program that takes-h-i behind a python cmdargs.py, the program outputs the-H and-I parameters, followed by the contents:


If you enter other parameters, such as the more-u parameter here, the program is automatically error.

This cmdargs.py code is as simple as this:

#-*-coding:utf-8-*-import sys,getoptopts,args=getopt.getopt (sys.argv[1:], "H:I:") # "H:I:" indicates that the program only accepts-h-i parameters #sys.argv [1:] for the parameter list to be processed, sys.argv[0] is the script name, so only take sys.argv[1:] section, filter out the script name for Op,value in opts:    if op== "-i":        print "-I values is : "+value; #此处的value就是-I value    if op=="-H ":            print"-H values is: "+value;

One of the for loops takes a double-variable traversal.

An array of Opts,args, similar to a key-value logarithmic group.

By traversing the OPTs array, you can take out the arguments behind the command line.

If you want to run the above program in Eclipse's Pydev environment, you can use the same method as the one you need to set up the program:

Right-click the appropriate python file and select ... The running setting in the mode, in which the corresponding parameters are arguments.


If you write in programs arguments:-H sdsad-i sdasdas The above program runs as follows:


If other parameters are set, the same error will be


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

"Python" reads command-line arguments, sets the Python runtime parameters in Pydev

Related Article

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.