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