The Argparse module in Python

Source: Internet
Author: User

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

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.