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