How Python obtains command-line arguments

Source: Internet
Author: User
Tags parse string python script

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)
Script Name: sys.argv[0]
Parameter 1:sys.argv[1]
Parameter 2:sys.argv[2]

test.py

1 Import sys

2 print "Script name:", Sys.argv[0]

3 for I in range (1, Len (SYS.ARGV)):

4 print "Parameters", 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, getopt


02

opts, args = Getopt.getopt (sys.argv[1:], "Hi:o:")

Input_file= ""

Output_file= ""

06

On OP, value in opts:

If op = = "-I":

Input_file = value

Ten elif op = = "-O":

Output_file = value

Elif op = = "-H":

Usage ()

Sys.exit ()

Code Explanation:
A) Sys.argv[1:] for the parameter list to be processed, sys.argv[0] is the script name, so use sys.argv[1:] To filter out the script name.
b) "Hi:o:" When an option simply represents a switch state, that is, the option character is written in the parse string when no additional parameters are followed. When an optional item is followed by an additional parameter, the Write option character in the parse string is appended with a ":" Number. So "Hi:o:" means "h" is a switch option; "I:" and "O:" means that a parameter should be followed.
c) Call the Getopt function. The function returns two lists: OPTs and args. OPTs the format information for the analysis. Args is the remaining command-line argument that is not part of the format information. OPTs is a two-tuple list. Each element is: (option string, additional parameter). Empty string If no additional parameters are available.

The third parameter of the GETOPT function [, Long_options] is an optional long option parameter, and the example above is a short option (such as-i-o)
Examples of long option formats:
--version
--file=error.txt
Allow a script to support both short and long options
Getopt.getopt (sys.argv[1:], "Hi:o:", ["Version", "file="])

http://www.biyinjishi.com/products/a70-b7010/
http://www.biyinjishi.com/products/a70-b7015/
http://www.biyinjishi.com/products/a70-b7020/
Http://www.biyinjishi.com/products/a70-b7050/
http://www.biyinjishi.com/products/a70-b7060/

How Python obtains command-line arguments

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.