Python Command Line Parameter Selection

Source: Internet
Author: User
Tags element groups

This article mainly introduces Python command line parameters. Python command line parameters encounter many difficulties in a wide range of applications, for example, sys and getopt modules in python process command line parameters, the following is a detailed introduction. If you want to pass parameters to python scripts, what is the corresponding argc and argv (command line parameters in C) in python?

Required module: sys
Number of parameters: len (sys. argv)
Script Name: sys. argv [0]
Parameter 1: sys. argv [1]
Parameter 2: sys. argv [2]

 
 
  1. Est. py
  2. View sourceprint? 1 import sys
  3. Print "Script Name:", sys. argv [0]
  4. For I in range (1, len (sys. argv )):
  5. Print "parameter", I, sys. argv [I]
  6. Python test. py hello world

Script Name: test. py
Parameter 1 hello
Parameter 2 world

Use the command line option in python:

For example, we need a convert. py script. It is used to process a file and output the processed result to another file.
The script must meet the following conditions:
1. Use the-I-o option to differentiate the Python command line parameters
Whether the parameter is an input file or an output file. python convert. py-I inputfile-o outputfile
2. If you do not know which parameters are required for convert. py, use-h to print the help information.
Python convert. py-h

Original form of the getopt function:

 
 
  1. getopt.getopt(args, options[, long_options])  
  2. convert.py  
  3. view sourceprint?01 import sys, getopt   
  4. opts, args = getopt.getopt(sys.argv[1:], "hi:o:")   
  5. input_file=""    
  6. output_file=""    
  7. for op, value in opts:   
  8. if op == "-i":  
  9. input_file = value    
  10. elif op == "-o":    
  11. output_file = value    
  12. elif op == "-h":   
  13. usage()    
  14. sys.exit()   

Code explanation:
A) sys. argv [1:] is the list of parameters to be processed, and sys. argv [0] is the script name. Therefore, use sys. argv [1:] to filter out the Script Name.
B) "hi: o:": When an option only indicates the switch status, that is, when no additional parameter is added, 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, "hi: o:" indicates that "h" is a switch option; "I:" and "o:" indicates that a parameter should be followed.
C) Call the getopt function. The function returns two lists: opts and args. Opts is the analyzed format information. Args is the remaining Python command line parameters that do not belong to the format information
Command line parameters. 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 ''.

The third parameter [, long_options] of the getopt function is an optional long option parameter. All of the preceding parameters are short options (such as-I-o)
Example of long option format:

 
 
  1. version  
  2. file=error.txt  

Make a script support both short and long options.

  1. The wonderful use of the Python programming language in website development
  2. The incomprehensible relationship between the Python programming language and Zpoe
  3. How does Python IDE adapt to the current turbulent market
  4. Basic Environment Test for Python Programming
  5. How to unpack Python Sequences

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.