python Via getopt module how to get command parameter details executed

  • 2020-06-19 11:01:10
  • OfStack

preface

The sample python script and shell script 1 can take command-line arguments and perform different logical processing according to different arguments.

In general, different execution commands and parameters can be obtained through the getopt module. Without further ado, let's take a look at the details.

The methods are as follows:

Below I explain the use of this module by creating a new script of ES13en.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 command:

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

Execution 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']

Let's look at the official documentation for the getopt module


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.

You can see that the getopt method requires three parameters.

The first argument is args which is the command line argument that we're going to parse and we can go through it sys.argv Gets the relevant parameters for execution


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

You can see that the first value in the argument list is the full pathname of the script execution, and the remaining parameters are command line parameters separated by whitespace. In order to obtain a valid parameter, the value of the args parameter is usually taken sys.argv[1:] .

The second argument is that shortopts is the short command operator, and its arguments should contain arguments starting with the -symbol on the command line, like qht in the above example, so qht is the short command of the script. How does the short command match the arguments? You can see that shotopts is "ht:q:" in the example, the command is followed by: to declare whether this command needs parameters. Here h does not need parameters, t and q need parameters, and the command line followed by the parameters of t and q are their command parameters, that is, t's command parameter is 20171010-20171011,q's command parameter is 24.

The third parameter, longopts, is an array representing a collection of long command operators. This set to begin with - symbols contains the command line parameters, url and out are long command, when the back end with = long command is said he needs a parameter, such as "url =", his match in the command line parameter under 1 https: / / www baidu. com.

This method returns two array elements. The first return value is the command line matched by shortopts and longopts and the meta ancestor of its arguments. The return value of this example is:


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

The second return value is an unmatched parameter on the command line. The return value for this example is:


['file1', 'file2']

With the return value, we can design different logic processing according to different commands in our own code, which enriched the usability of the script.

conclusion


Related articles: