python command arguments detailed how to obtain performed by getopt module

This article is mainly to introduce the relevant information on python how to get command parameter performed by getopt module, the paper sample code described in great detail, has a certain reference value of learning for all of us to learn or work, we need friends below with the small series together to learn learn it.
Foreword

python scripts and shell scripts may be acquired as the command line parameters, in accordance with different parameters, perform different logic.

Usually we can get different commands and parameters through the implementation of getopt module. The following did not talk much to say, to take a look at the detailed introduction.
Methods as below:

Let me explain by using this module to create a new script test.py

#!/usr/bin/python
# -*- coding: utf-8 -*-
import sys
import getopt
if __name__=='__main__':
 print sys.argv
 opts, args = getopt.getopt(sys.argv[1:], "ht:q:", ["url=",'out'])
 print opts
 print args

Executive order: ./test3.py -t 20171010-20171011 -q 24 -h --url=https://www.baidu.com --out file1 file2
The results:

['D:/GitReposity/hope_crontab_repo/sla_channel/test3.py', '-t', '20171010-20171011', '-q', '24', '-h', '--url=https://www.baidu.com', '--out', 'file1', 'file2']
[('-t', '20171010-20171011'), ('-q', '24'), ('-h', ''), ('--url', 'https://www.baidu.com'), ('--out', '')]
['file1', 'file2']

We see getopt module of official documents

def getopt(args, shortopts, longopts = [])
 
Parses command line options and parameter list. args is the
argument list to be parsed, without the leading reference to the
running program. Typically, this means "sys.argv[1:]". shortopts
is the string of option letters that the script wants to
recognize, with options that require an argument followed by a
colon (i.e., the same format that Unix getopt() uses). If
specified, longopts is a list of strings with the names of the
long options which should be supported. The leading '--'
characters should not be included in the option name. Options
which require an argument should be followed by an equal sign
('=').
 
The return value consists of two elements: the first is a list of
(option, value) pairs; the second is the list of program arguments
left after the option list was stripped (this is a trailing slice
of the first argument). Each option-and-value pair returned has
the option as its first element, prefixed with a hyphen (e.g.,
'-x'), and the option argument as its second element, or an empty
string if the option has no argument. The options occur in the
list in the same order in which they were found, thus allowing
multiple occurrences. Long and short options may be mixed.

Can be found getopt method requires three parameters.

The first parameter is args is going to parse command line arguments we can obtain the relevant parameters performed by the sys.argv

['D:/GitReposity/hope_crontab_repo/sla_channel/test3.py', '-t', '20171010-20171011', '-q', '24', '-h', '--url=https://www.baidu.com']

The first value of the parameter list can be seen is the full path name of the script execution, the remaining parameters are space-separated command line parameters. In order to obtain valid parameter, the parameter value is usually taken args sys.argv [1:].

The second parameter is a short command shortopts operators, to include his argument to the command line - parameters at the beginning of the symbol, as in the example above qht thought - at the beginning, so qht command of the script is short, and short command It is how to match the parameters of it? Examples can be seen in shotopts as "ht: q:", used here after the command followed: to declare whether the command requires parameters, no arguments here h, t and q need parameters, the command line followed by t and q they parameter is the command parameters, i.e., the command parameter t is 20171010-20171011, q command parameter is 24.

The third parameter is longopts, change parameter is an array, represents a set of operators long command. This set contains the command line to start with a - sign parameters, url and out are long command, after the command to when the long end is = he needs a parameter, such as "url =", he match the next command line parameters https://www.baidu.com.

This method returns an array of two elements. The first return a value, and by shortopts longopts matching tuples and its command line arguments. Examples of the return value:

[('-t', '20171010-20171011'), ('-q', '24'), ('-h', ''), ('--url', 'https://www.baidu.com'), ('--out', '')]
['file1', 'file2']

The second return value is not matched to the command line parameters, return value of this example:

['file1', 'file2']

By returning the value we can in your own code, depending on the order to design a different logic, abundant availability of the script.

We recommend learning Python buckle qun: 913066266, look at how seniors are learning! From basic web development python script to, reptiles, django, data mining, etc. [PDF, actual source code], zero-based projects to combat data are finishing. Given to every little python partner! Every day, Daniel explain the timing Python technology, to share some of the ways to learn and need to pay attention to small details, click on Join us python learner gathering

Published 47 original articles · won praise 53 · views 50000 +

Guess you like

Origin blog.csdn.net/haoxun03/article/details/104270451