Python Command Line Parameter Parsing, python command line parameters

Source: Internet
Author: User

Python Command Line Parameter Parsing, python command line parameters

Method 1:

Python has a class that can specifically process command line parameters. first look at the Code:

#!/usr/bin/env python                                                                                                # encoding: utf-8                                                                                                                                                                                                                         from optparse import OptionParser 
                                                                                                                     parser = OptionParser(usage="%prog [options]")
parser.add_option("-m","--machine",action="store",type="string",dest="machine",help="the machine to be check")
(options,args)=parser.parse_args()   if options.machine:            print options.machine

The first line is used for initialization,

-M is short for the parameter,-machine is the complete parameter store means to store this parameter, type is the storage type. Which variable is stored in dest? The full parameter name is used by default, and the content displayed when help is used

Method 2:

Use the getopt module for parsing

import sysimport getoptdef TestGetOpt():  try:    opts, args = getopt.getopt(sys.argv[1:],'d:f:h',['days=','files=','help'])  except getopt.GetoptError:     usage()     sys.exit()  print (opts)  print (args)  for o, a in opts:     if o in ("-h", "--help"):         usage()         sys.exit()     elif o in ("-d", "--days"):         day = a     elif o in ("-f", "--files"):         files = a  print (day)  print (files)

See http://blog.csdn.net/lwnylslwnyls/article/details/8199454


How do I use Python command line parameters? I don't understand it at all.

I have already completed the tutorial for you:
[Arrangement] How to obtain and process command line parameters in Python

Before reading this, make sure that you are clear about the python development environment,
If you are not familiar with it, refer:
[Arrangement] [Multi-graph explanation] How to Develop Python in Windows: How to Run Python scripts in cmd, how to use Python Shell (command line mode and GUI mode), and how to use Python IDE

(No post address is provided here. Search the post title on google to find the post address)

In python, what are the short and long formats of command line parameters?

Python code
1. try:
2. opts, args = getopt. getopt (sys. argv [1:], "ho:", ["help ","
Output = "])
3. Counter t getopt. GetoptError: 4. # print help information and exit:

1. The function used for processing is getopt (). Because it is the getopt module imported directly using import, you must add the getopt limitation.
2. Use sys. argv [1:] to filter out the first parameter (it is the name of the execution script and should not be counted as part of the parameter ).
3. Use the short format analysis string "ho :". When an option only indicates the switch status, that is, when no additional parameter is provided, the option character is written into the analysis string. When the option is followed by an additional parameter, write the option character in the analysis string and add. Therefore, "ho:" indicates that "h" is a switch option; "o:" indicates that a parameter should be followed.
4. Use the long format to analyze the string list: ["help", "output ="]. Long format strings can also be in the switch status, that is, they are not followed by the "=" sign. If there is an equal sign, it indicates that there should be a parameter later. This long format indicates that "help" is a switch option; "output =" indicates that a parameter should be followed.
5. Call the getopt function. The function returns two lists: opts and args. Opts is the analyzed format information. Args is the remaining command line parameter that does not belong to the format information. Opts is a list of two-element groups. Each element is: (option string, additional parameter ). If no parameter is attached, it is an empty string ''.
 

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.