1. Background
When a project has many input parameters and needs to be modified and debugged, the format and path of the parameters are often involved . If you modify the code line by line , it will be very cumbersome and the error rate is high. The argparse module is designed to solve this problem.
Two, the role
argparse is a command-line parameter parsing package that comes with python, that is, a library for passing in parameters , which can be used to easily read command-line parameters . This module uniformly calls the required parameters or file paths from the command line , and can complete the input parameter reading work at one time , and it is more convenient to change and more user-friendly.
3. Steps and usage
3.1 Import argparse library
import argparse
3.2 Create a parsing object
ape = argparse.ArgumentParser(description= '命令行中传入参数')
An ArgumentParser object contains all the information needed to parse the command line into python data types. The string in description is the custom explanation of the parsing port.
Usage: ArgumentParser object
class argparse.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, allow_abbrev=True)
- prog : the name of the program (default: sys.argv[0])
- usage : a string describing the usage of the program (default: generated from arguments added to the parser)
- description : Text to display before the parameter help documentation (default: None)
- epilog : Text to display after help documentation (default: None)
- parents : a list of ArgumentParser objects whose arguments should also be included
- formatter_class : A class used to customize the output format of the help document
- prefix_chars : set of prefix characters for optional parameter (default: '-')
- fromfile_prefix_chars : set of prefix characters used to identify filenames when other parameters need to be read from the file (default: None)
- argument_default : the global default value for the argument (default: None)
- conflict_handler : strategy for resolving conflicting options (usually unnecessary)
- add_help : add a -h/-help option to the parser (default: True)
- allow_abbrev : Allow abbreviated long options if the abbreviation is unambiguous (default: True)
3.3 Add parameters through the add_argument method
ape.add_argument("-i ", "--image", required=True, help="path to input image")
ape.add_argument("-t", "--template", required=True, help="path to template OCR-A image")
Add command-line arguments and options to this object, and each add_argument method corresponds to a parameter or option.
Usage: add_argument method
ape.add_argument(name...[, action][, nargs][, default][, type][, choices][, required][, help])
3.4 Call the parse_args() method to parse parameters
args = vars(ape.parse_args())
This statement indicates that the parameters are parsed, and the input parameters are stored in args.
4. Debugging method
Example: Input some pictures of the project through Pycharm, and complete the picture replacement during the debugging process.
import argparse
ap = argparse.ArgumentParser()#创造一个解析器对象
ap.add_argument("-i ", "--image", required=True, help="path to input image")#添加image的
ap.add_argument("-t", "--template", required=True, help="path to template OCR-A image")#添加template的
args = vars(ap.parse_args())
To run the project for the first time, you need to add it in the Parameters of Configuration according to the following format: (note the space)
-i 路径加image文件名称 -t 路径加template文件名称
As shown in the figure below,
if you want to replace other files in the same path, just modify the name directly.