What's with Argparse?
Answer: parameter settings, such as Python demo.py-h and so on.
Start learning This module:
- Parser = Argparse. Argumentparser () #使用这个模块首先需要创建一个解析对象
- Parser.add_argument () #向该对象添加你想要的设置的参数以及设置项.
- Parser.parse_args () #解析
Start Case:
#!usr/bin/env python#Encding:utf-8#by I3ekrImportArgparseparser=Argparse. Argumentparser () parser.add_argument ('--ver','- v', help='This is the Help ...', required=true,nargs=2,type=str,choices=['a','b','C'],dest='Test') args=Parser.parse_args ()Print(args.test)
Output Result:
[Email protected]:/home/i3ekr/desktop# python 1.py-v ' a ' B '
[' A ', ' B ']
Some of the parameters used:
Help: The input-h parameter appears as shown below (the-h parameter is the default.)
[email protected]:/home/i3ekr/desktop# cat 1.py #!usr/bin/env python#encding:utf-8#by i3ekrimport Argparseparser = Argparse. Argumentparser () parser.add_argument ('--ver ', '-V ', help= ' this are help ... ') args = Parser.parse_args ()
[email protected]:/home/i3ekr/desktop# python 1.py-husage:1.py [-h] [--ver ver]optional arguments: - H,--help show this help message and Exit--ver ver,-V ver .
Required: Must parameter, if set this parameter is true then that is to explain you this parameter is necessary. Required=true
Type: Set Value type
Choices: Set the parameter value range, such as you set choices=[' test1 ', ' test2 ', then the value of the parameter can only be test1 or test2.
Nargs: The value of the parameter has how many, only one by default. nargs=2
Dest: The value of this parameter is parsed out and placed in that attribute.
More parameters: 46693471
Argparse modules in Python