(i) Single independent parameter
If the parameters entered on the command line are all single and independent, just use a loop to read out all the parameters one by one. sys module directly use args = to get all the parameters (the return value args is a list), args0] is the path name of the python file to be executed, and the ones after args[1:] are the parameters entered on the command line, so use a for loop to read out all the parameters one by one. With a for loop can be read one by one args list, sample code is as follows:
import sys def usage(): print """ This is usage. This is usage. This is usage. """ def main(argv): for arg in argv[1:]: if arg == '-t': pass elif arg == '-h' or arg == '--help': usage() () elif arg == '-p': pass else: print "Error: invalid parameters" () if __name__ == '__main__': main()
(ii) Parameter combination
If you want to read a combination of arguments like: -t mytest, the above method won't work. In the shell, you have shift to move directly to the next argument, but there is no shift in python, so you'll have to do it differently.
One way is to use the getopt module (the official documentation on getopt explains:Click to open link): The getopt function in the getopt module specifies the possible types of the arguments and gets all the arguments.
Usage:
(args, options[, long_options])
The options parameter specifies short-form input, i.e., a single bar "-", such as "-h", and long_options is optional, and is used to specify long-form input, i.e., a double bar "--", such as "--help". For single-argument inputs, both options and long_options simply write the parameter name, e.g. the following code specifies two single-argument inputs: -h/--help -t/--test:
opts, args = ([1:], 'ht', ['help', 'test''])
If you want to continue to take parameters, add a colon ":" after the parameter name for options, and add an equal sign "=" after the parameter name for long_options. For example, the following code specifies three parameters: -h/--help -t/--test -r path / --root path:
opts, args = ([1:], 'htr:', ['help', 'test','root='])
getopt function has two return values, generally only the first, such as the following code, the first return value opts :" is a list of (option, value) pairs ", that is, a -t mytest such an element of the pair, of course, if -t do not need to connect the parameter, then there is no value value. Using this pairs return value, we can use a for loop to get the parameters, whether it is a single independent parameter, or a combination of parameters, can be, sample code is as follows:
import sys import getopt def usage(): print """ This is usage. This is usage. This is usage. """ def main(argv): try: opts, args = ([1:], 'm:p:h', ['miner=', 'params=', 'help']) except : usage() () for opt, arg in opts: if opt in ['-h', '--help']: pass elif opt in ['-t', '--test']: pass elif opt in ['-p', '--path']: param = arg print param () else: print "Error: invalid parameters" usage() () if __name__ == '__main__': main()
In addition, there is a point of knowledge is that, if you want to print a large section of string how to do, with three double quotes can be wrapped up, such as the above code in the usage function, and it should be noted that the contents of the three double quotes do not have to indent the contents of the direct from the very beginning of the beginning of the write, otherwise it will lead to an error.
The above example code for python to get the list of command line input parameters is all I have shared with you.