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