Python's Get command line arguments

Source: Internet
Author: User
Tags parse string

1. Getopt module:
The Getopt module is used to extract command-line options and parameters, known as SYS.ARGV. command-line options make program parameters more flexible. Short option mode and long option mode are supported.

1) Main function: getopt (args, shortopts, longopts = [])

Parameters:
=====
-args are usually: sys.argv[1:];
-Shortopts (:): Short format analysis string, if the option string without a colon (:), then the switch state, without parameters, if there is a colon after the option string, then the option must have additional parameters;
-Longopts (=): Long format analysis string, if the option string without an equal sign (=), then the switch state, without parameters, if there is an equal sign after the option string, the option must have additional parameters.

return value:
============
-Returns opts and args.
-OPTs is a tuple of parameter options and their value ('-f ', ' Hello '), ('-t ', '), ('--format ', '), ('--directory-prefix ', '/home ')
-args is a command-line input (' A ', ' B ') that removes useful parameters
-then traverse opts to get all the command-line options and their corresponding parameters.

2) Treatment Method:
A) import getopt, sys module
b) parsing command-line arguments
c) Processing results

Cases:

# # # The first step is simple, only need import # # #
Import getopt, sys  

# # #第二步处理方法如下 (Example of a Python handbook) # # #
Try
opts, args = Getopt.getopt (sys.argv[1:], "ho:", ["Help", "output="])
Except Getopt. Getopterror:
# Print Help information and exit:
Note:
1.Using the getopt () function to handle command-line arguments
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.Short format analysis String---> "ho:". The option character is written in the parse string when an option simply represents a switch state, that is, 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 "ho:" means "h" is a switch option; "O:" indicates that a parameter should be followed.
4.Long format analysis String list----> ["Help", "output="]. Long format strings can also have a switch state, which is not followed by the "=" number. If you follow an equal sign, you should have a parameter later. This long format means "help" is a switch option; "Output=" indicates that a parameter should be followed.
5.The getopt () function returns a value---> Two lists: OPTs and args:opts for the parsed format information; 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.
6.The whole process uses exceptions to include, so that when parsing errors, you can print out usage information to inform the user how to use the program.

Example of return value opts:
[(-T OTA), (-R USB), (-e Datav3_9991.xml), (-F), (-P), (-D 1)]

# # # #第三步主要是对分析出的参数进行判断是否存在, and then further processing. The main processing mode is: # # #
For O, the A in opts:
If O in ("-H", "--help"):
Usage ()
Sys.exit ()
If O in ("-O", "--output"):
Output = a

Python's Get 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.