How Python obtains command-line arguments

Source: Internet
Author: User
Tags function prototype python script

How Python obtains command-line arguments

This article describes the SYS in Python and the getopt module handles command-line arguments

If you want to pass parameters to the Python script, what is the corresponding argc in Python, argv (command-line arguments for the C language)?
Module Required: SYS
Number of parameters: Len (SYS.ARGV)
脚本名:    sys.argv[0]
参数1:     sys.argv[1]
参数2:     sys.argv[2]


test.py

Import sysprint "script name:", sys.argv[0]for I in range (1, Len (SYS.ARGV)):    print "parameter", I, Sys.argv[i]

>>>python test.py Hello World

Script Name: test.py
Parameter 1 Hello
Parameter 2 World

Use command-line options in Python:
For example we need a convert.py script. It works by processing one file and outputting the processed results to another file.
The script is required to meet the following criteria:
1. Use the-I-O option to distinguish whether the parameter is an input file or an output file.
>>> python convert.py-i inputfile-o outputfile
2. If you do not know what parameters convert.py need, print out the help information with-H
>>> python convert.py-h

getopt function Prototype:
Getopt.getopt (args, options[, long_options])

convert.py

Import sys, getoptopts, args = Getopt.getopt (sys.argv[1:], "Hi:o:") input_file= "" Output_file= "" for op, value in opts:    if op = = "-I":        input_file = value    elif op = = "-O":        output_file = value    elif op = = "-H":        usage () 
   sys.exit ()

代码解释:
a) sys.argv[1:]为要处理的参数列表,sys.argv[0]为脚本名,所以用sys.argv[1:]过滤掉脚本名。
b) "hi:o:": 当一个选项只是表示开关状态时,即后面不带附加参数时,在分析串中写入选项字符。当选项后面是带一个附加参数时,在分析串中写入选项字符同时后面加一个":"号。所以"hi:o:"就表示"h"是一个开关选项;"i:"和"o:"则表示后面应该带一个参数。
c) 调用getopt函数。函数返回两个列表:opts和args。opts为分析出的格式信息。args为不属于格式信息的剩余的命令行参数。opts是一个两元组的列表。每个元素为:(选项串,附加参数)。如果没有附加参数则为空串‘‘。

getopt函数的第三个参数[, long_options]为可选的长选项参数,上面例子中的都为短选项(如-i -o)
长选项格式举例:
--version
--file=error.txt
让一个脚本同时支持短选项和长选项
getopt.getopt(sys.argv[1:], "hi:o:", ["version", "file="])

How Python obtains command-line arguments

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.