Recently, in the project of machine learning, I often encounter argparse modular , Here is a simple record and a brief introduction to the use of the module
No matter what kind of computer project is, it will not be specially developed for just one person , Even a project belonging to a certain person cannot guarantee that the user's needs will not change . Of course , We can temporarily adjust the code to meet the requirements （“ Thought of the terrible Party A ！！”）, But it's too much trouble . meanwhile , To facilitate the dissemination of modules or applications , As developers, we need to enable non professionals to correctly 、 Easily use the functions it needs , and argparse Modules are born for this .（
Add up , Personally think that argparse Modules also provide convenience for ourselves , In the process of using, we must sort out the implementation process and functions of the application .）
From the perspective of human-computer interaction , A common way is to use “read” Way to read the user's requirements , Change the execution process of the application through judgment ; This method is relatively simple , But the inevitable problem is , Used for the huge randomness caused by user input , It makes it difficult for us to ensure the normal execution of the program . So can we design a way ： It's like making a questionnaire , For ease of use , At the same time, ensure that the results meet the needs , We take the initiative to provide line selection , The user only needs to after the corresponding option “ tick ” that will do . Personally think that argparse That's my idea .
Import module ：
Definition argparse object ：
parser = argparse.ArgumentParser(description=" Add your comments on this “ questionnaire ” Description of , To act as a guide .")
add to “ Options ” And description ：
parser.add_argument("--path", # Indicate the name of the topic here type=str, # Indicates the data type help="path to the training data", # Help information , Help users understand choices=["1", "2"], # Qualifying options , You can only select... From the specified values default=os.path.dirname(__file__)) # Set the default value , Title users with default values are not required parser.add_argument("--png", # This option is special “ switch ” Options , As long as the running program is marked --png , This means that the option is on , Corresponding action Will execute ,“store_true” It means saving as true. help="if set, trains from raw KITTI png files (instead of jpgs)", action="store_true")
The user fills in ：
Be careful ： This step is not during program definition , It happens when the user calls the application
python file_name.py --path /etc/tmp/??? -- The other options ...
collect “ questionnaire ：”
options = parser.parse_args() # call .parse_args Methods complete the analysis of the questionnaire return options # Return results , The results are saved in the form of a dictionary
from __future__ import absolute_import, division, print_function import os import argparse class Options: def __init__(self): self.parser = argparse.ArgumentParser(description=" Description information ") # Add options and descriptions self.parser.add_argument("--path", type=str, help="path to the training data", default=os.path.dirname(__file__)) self.parser.add_argument("--dir", type=str, help="log directory", default=os.path.join(os.path.expanduser("~"), "tmp")) # The other options def parse(self): self.options = self.parser.parse_args() return self.options