Python Command Line Parameter Parsing OptionParser class usage instance, pythonoptionparser

Source: Internet
Author: User

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.


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.