Python uses getopt to parse the input parameter instance of the command line, and pythongetopt

Source: Internet
Author: User
Tags element groups

Python uses getopt to parse the input parameter instance of the command line, and pythongetopt

The example in this article describes how python uses getopt to parse the input parameters in the command line.

The specific instance code is as follows:

Import getopt import sys config = {"input": "", "output ":". ",}# getopt: the first option is sys. argv [1:]. The second parameter is short. If the parameter must be followed by a value, add:. The third parameter is long. # It is a list, opts, args = getopt. getopt (sys. argv [1:], 'Hi: o: d', ['input = ', 'output =', 'help']) # Parameter Parsing process, the long parameter is --, the short parameter is-for option, value in opts: if option in ["-h", "-- help"]: print "" usage: % s -- input = [value] -- output = [value] usage: % s-input value-o value "" elif option in ['-- input ', '-I']: config ["input"] = value elif option in ['-- output','-O']: config ["output"] = value elif option = "-d": print "usage-d" print config

Input parameters:

--input=c:\temp\aa -o c:\temp\output -d

Printed result:

usage -d{'input': 'c:\\temp\\aa', 'output': 'c:\\temp\\output'}

I hope this article will help you with Python programming.


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 ''.
 
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)

Related Article

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.