Python Learning argparse Module

Source: Internet
Author: User

First, Introduction:

Argparse is a standard module used by Python to parse command-line arguments and options in place of obsolete optparse modules. The Argparse module is used to parse command-line arguments, such as Python parsetest.py input.txt output.txt--user=name--port=8080.

Second, the use of the steps:

1:import Argparse

2:parser = Argparse. Argumentparser ()

3:parser.add_argument ()

4:parser.parse_args ()

Explanation: Import the module first, then create a parse object, and then add the command line arguments and options you want to follow to the object, each Add_argument method corresponds to a parameter or option that you want to follow, and the last call to the Parse_args () method to parse it; , the following is a brief explanation of steps 2 and 3.

Third, Method Argumentparser (Prog=none, Usage=none,description=none, Epilog=none, Parents=[],formatter_class=argparse. Helpformatter, prefix_chars= '-', Fromfile_prefix_chars=none, argument_default=none,conflict_handler= ' error ', add_ Help=true)

These parameters all have default values, which are printed when parser.print_help () is called or when the program is run because the parameter is incorrect (the Python interpreter actually calls the Pring_help () method). It is generally only necessary to pass the description parameter, as above.

Iv. Method Add_argument (Name or flags ...) [, Action] [, Nargs] [, Const] [, Default] [, type] [, Choices] [, Required] [, help] [, Metavar] [, Dest])

which

Name or Flags: command-line parameter name or option, such as address above or-p,--port. If the command line argument is not given and no defualt is set, an error occurs. However, if it is an option, it is set to none

Nargs: The number of command-line arguments, generally using wildcard notation, where, '? ' means only one, ' * ' means 0 to more, ' + ' means at least one

Default: Defaults

Type: The types of parameters, which are string by default, type float, int, and so on

Help: Similar to the parameters in the Argumentparser method, and the same situations arise

Python Learning argparse Module

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.