Python command-line parameter parsing argparse simple analysis

Source: Internet
Author: User

After Python 2.7, it is not recommended to use Optparse, and Argparse is recommended.

In the other words, simple analysis of the problem I encountered: I want to use Argparse to parse the indefinite length of the command line parameters

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 ()

Suppose you save the above code in a test.py file and test it at the command line as follows:

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=[]), [])

As in four sets of tests, the incoming parameters are the same (the only difference is the location), but the results are different.

The reason, this is the Argparse module implementation principle. The use of regular matching method to parse parameters ( regular statement belongs to personal understanding, no attention to the source code, if wrong, hope to correct )

When adding parameter rules to the add_argument, we call the "-" option parameter, without '-' as the positional parameter. Let's take the position parameter as an example

In the above code, the POS1,POS2,POS3 three positional parameters were added through add_argument, and they constituted a regular expression of a*aa*, where A * represents a match of 0 or more values.

In the parameter list in test Example 1, first encountered positional parameter a b C, which coincides with a*aa* pattern matching, that is pos1=[a,b] pos2=c, pos3=[], because a*aa* is greedy matching pattern, that is, as much as possible to get the value, so pos1=[a,b] and pos3= []

In the parameter list in test Example 4, a*aa* matches the C D E F g h in the parameter list, in greedy match mode, can get pos1=[c,d,e,f,g], pos2=h, pos3=[]


By analyzing the above two examples, we just need to remember that the argparse parsing parameters are pattern matching based on the rules added by Add_argument . Can better understand the results of the analysis!


Finally, thanks to the blog Http://4byte.cn/question/347481/argparse-how-to-handle-variable-number-of-arguments-nargs.html's sharing

Python command-line parameter parsing argparse simple analysis

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.