Python command-line parameter parsing learning

Source: Internet
Author: User

Argparse module parameters are many, not familiar with this, do not understand the command line, rarely used, first copy the point of explanation, slowly look at understanding.

Standard module for parsing command-line options and parameters.

Steps to use:

1:import Argparse #导入模块

2:parser = Argparse. Argumentparser () #创建解析对象

3:parser.add_argument () #向该对象中添加使用到的命令行选项和参数

4:parser.parser_args () #解析命令行

==================================================================

Create an Argumentparser instance, and the parameters for the argumentparser are the keyword parameters.

Prog: file name, default is Sys.argv[0], used to describe the name of the program in the Help information.

Usage: A string describing the purpose of the program

Information displayed prior to DESCRIPTION:HELP information

Information displayed after epilog:help the information

=====================================================================================================

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

The common parameters are explained as follows:

Name or Flags: command-line parameter name or option, such as-P,--port

Action

Store: Default action mode, store value to specified variable

Store_const: Store values specified in the const section of the parameter, commonly used to implement non-Boolean command-line flag

Store_true/store_false: boolean switch. The default value of Store_true is False, which is true if the command line has input of the boolean switch. Store_false opposite

Append: Stores the value to the list, which can be reused

Append_const: Store value to list, store value specified in const part of parameter

Count: Number of inputs for shorthand for statistical parameters

Version: Output release information, and then exit the script

Nargs: The number of command-line arguments, generally denoted by a wildcard:? Represents only one, * represents 0 to more, + represents 1 to more

Default: Defaults

Type: Types of parameters, default to string, or float, int, and BOOL

Choices: Enter a range of values

Required: The default is False, or true to indicate that the parameter must be entered

Help: Helpful Tips for using

Dest: The corresponding variable name of the parameter in the program, such as: Add_argument ("-A", dest= "Code_name"), using Parser.code_name in the script to access the value of the command-line option

Python command-line parameter parsing learning

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.