Python command-line interpreter Argparse

Source: Internet
Author: User

One: argparse introduction

1. Argparse

Argparse, which is the recommended tool for writing command-line programs in the Python standard library

Parser = Argparse. Argumentparser () creates an instance,
#parser. Parse_args () The Parse_args method is to return some default or specified parameter information from the command-line parameters.
#print Dir (parser)
Parser.add_argument (' Square ', help= ' **2 ', type = int) Add a positional arguments (temporarily does not understand its meaning)

Note: There are currently two types of parameters: positional arguments optional arguments
#parser. Add_argument ('-t ', metavar= ' THREADS ', Type=int, default=20,
# help= ' Num of scan threads for each scan process, by default ')
You can add options to the optional parameter in the above way.

args = Parser.parse_args ()
Print Args.square **2 Note here, args.xxx,xxx must be the name of the parameter added by add_argument

2. Add_argument Method

Add_argument: Read in command-line arguments, the call has multiple parameters
Argumentparser.add_argument (name or flags...) [, Action] [, Nargs] [, Const] [, Default] [, type] [, Choices] [, Required] [, help] [, Metavar] [, Dest])

Name or flags: is a required parameter that accepts the option parameter or is a positional parameter (a string of file names

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

Chinese reference:

http://songpengfei.iteye.com/blog/1440158 describes each parameter of the Argparse module

http://blog.chinaunix.net/xmlrpc.php?r=blog/article&uid=21633169&id=4387657 a detailed introduction to the Add_argument method

Python command-line interpreter Argparse

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.