Python Command Line Parameter Parsing OptionParser class usage instance, pythonoptionparser
This article describes how to use the OptionParser class for python Command Line Parameter Parsing.
The Code is as follows:
from optparse import OptionParser parser = OptionParser(usage="usage:%prog [optinos] filepath") parser.add_option("-t", "--timeout", action = "store", type = 'int', dest = "timeout", default = None, help="Specify annalysis execution time limit" ) parser.add_option("-u", "--url", action = "store_true", dest = "url", default = False, help = "Specify if the target is an URL" ) (options, args) = parser.parse_args() if options.url: print(args[0]) print options.timeout
Run the following command:
I hope this article will help you with Python programming.
How can I use python command line parameters?
For example, if you write a script named a. py, you can read all the data from a file and output the sum.
For convenience, we can specify the name of the input file in the source code. If you want to publish it to everyone, you cannot change the source code each time you use it. Therefore, the command line parameters are used in this way:
$ Python a. py myfile.txt
In this way, you do not need to modify a. py or know any details in a. py. You can simply call it.
I know nothing about the command line parameters in python and understand an OptionParser () code.
First, confirm that the subject is studying Python in linux.
Locate the option1.py directory in terminal.
Enter./option1.py-f and press Enter.