The Argparse module in Python

Source: Internet
Author: User

Argparse is the abbreviation for argument (parameter) and parser (parser)


The Argparse module is a parameter that is passed to a. py file. Use this module if you want to run the. py file directly while passing parameters to the file.

The steps for using this module are in the following stages:

1 Introducing Modules

Import Argparse


2 Creating a Parse Object parser

Parser = Argparse. Argumentparser ()


3 adding parameters to an object

Parser.add_argument (name or flag, nargs=, action=, type=, default=, help=)


The name parameter name, a fixed parameter, and an optional parameter, such as Add_argument ('-d '), indicate that the parameter-D is optional (can be passed in using or not passed in parameters)

The number of Nargs parameters, * represents 0 to more,? Indicates only one, + represents at least one, or can be a number directly specify the number of arguments

Type parameter, which is string by default and can be specified as Int,float,file

Default setting parameter Defaults

Help information to help you set parameters, using Python arg.py--help view, arg.py as the file name,--help default is the own

For example: Parser.add_argument (' A ', nargs= '? ' Type=int, default=10, help= "A is typeof int")


4 Call the Parse_args () method to parse, and then you can use the

arg = Parser.parse_args ()


5 Using Parameters

Arg.a


6 file saved as arg.py, run file python arg.py 12


Instance:

Import Argparse


Parser = Argparse. Argumentparser ()

Parser.add_argument ("A", nargs=2, Type=int, default=10, help= "A is typeof int")

Parser.add_argument ('-B ')


if __name__ = = ' __main__ ':

args = Parser.parse_args ()

Print args.a,args.b


Run: Python arg.py 12 13

Output [12,13] None

Here the incoming 12 and 13 are passed in to a in list form, B does not have an incoming value showing none


Run: Python arg.py 13-b AB

Output [12,13] ab

Incoming optional parameter-B shows AB


Official Document Address:

Https://docs.python.org/2/howto/argparse.html#id1

Reference:

http://blog.chinaunix.net/xmlrpc.php?r=blog/article&uid=21633169&id=4387657

Http://www.2cto.com/kf/201412/363654.html



This article is from the "Vintage Camera" blog, so be sure to keep this source http://7903389.blog.51cto.com/7893389/1789889

The Argparse module in Python

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.