Python Command Line Parameter Parsing argparse simple analysis

Source: Internet
Author: User

Python Command Line Parameter Parsing argparse simple analysis

Optparse is not recommended after python 2.7, but argparse is recommended.

I don't want to talk about anything else. In a simple analysis, I encountered the problem: I want to use argparse to parse command line parameters with an indefinite length.

For example:

 

import argparseimport sysparser = argparse.ArgumentParser(description='test parsing arguments')parser.add_argument('pos1', nargs='*')parser.add_argument('pos2')parser.add_argument('-o1')parser.add_argument('-o2')parser.add_argument('pos3', nargs='*')print sys.argv# arg = parser.parse_args(sys.argv[1:])arg = parser.parse_known_args(sys.argv[1:])print arg# print parser.print_help()

Assume that the above Code is saved in the test. py file and the following test is performed in the command line:

 

Input:Python test. py a B c-o1 d e-o2 f g h

Output:

['Test. py', 'A', 'B', 'C', '-o1', 'D', 'E', '-o2', 'F ', 'G', 'H']
(Namespace (o1 = 'D', o2 = 'F', pos1 = ['A', 'B'], pos2 = 'C', pos3 = []), ['E', 'G', 'H'])

 

Input:Python test. py a-o1 B c-o2 d e f g h
Output:

['Test. py', 'A', '-o1', 'B', 'C', '-o2', 'D', 'E', 'F ', 'G', 'H']
(Namespace (o1 = 'B', o2 = 'D', pos1 = [], pos2 = 'A', pos3 = []), ['C ', 'E', 'F', 'G', 'H'])
 

Input:Python test. py-o1 a B c-o2 d e f g h

Output:

['Test. py', '-o1', 'A', 'B', 'C', '-o2', 'D', 'E', 'F ', 'G', 'H']
(Namespace (o1 = 'A', o2 = 'D', pos1 = ['B'], pos2 = 'C', pos3 = []), ['E ', 'F', 'G', 'H'])
 

Input:Python test. py-o1 a-o2 B c d e f g h

Output:

['Test. py', '-o1', 'A', '-o2', 'B', 'C', 'D', 'E', 'F ', 'G', 'H']
(Namespace (o1 = 'A', o2 = 'B', pos1 = ['C', 'D', 'E', 'F', 'G'], pos2 = 'h', pos3 = []), [])
 

For the above four groups of tests, the input parameters are the same (the only difference is the location), but different results are obtained.

The reason is that this is the implementation principle of the argparse module. The parameter is parsed using the regular match method (The regular expression is a personal understanding and does not focus on the source code. If there is any error, please correct it.)

When add_argument adds parameter rules, we call the option parameter with '-' and the location parameter. The following uses location parameters as an example.

In the code above, the pos1, pos2, and pos3 location parameters are added through add_argument. The regular expressions they constitute are A * AA *, A * Indicates matching 0 or multiple values.

In the parameter list in test example 1, the location parameter a B c is first met, which exactly matches the pattern of A * AA *, that is, pos1 = [a, B] pos2 = c, pos3 = [], because A * AA * is a greedy match mode, that is, to obtain as many values as possible, so pos1 = [A, B] And pos3 = []

In the parameter list in test example 4, A * AA * matches c d e f g h in the parameter list. In greedy match mode, pos1 = [c, d, e, f, g], pos2 = h, pos3 = []

 

By analyzing the above two examples, we only need to rememberThe argparse parsing parameter matches the pattern according to the rules added by add_argument.. The parsing result can be better understood!

 

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.