Recently in the process of reading the code encountered the Argparse module, I remember the previous period of time has been seen, but after two weeks now and forget, it seems to write code must delve into the end to find out how to do, this article mainly refer to the Python3.6 series of official documents Argparse Module tutorial.
Note: There are also two modules similar to the Argparse function, getopt and optparse. Argparse is based on optparse, so it is very similar in terms of use, but it is not recommended to use Optparse, the official document is more recommended Argparse.
The role of 1.Argparse
To give a small example, in a Linux system, we often use the LS command, by default, LS will display all the file or folder name of the current directory, but when we want to get more information, such as file permissions, settling time, etc., we need to add-all or other permitted instructions after the LS command, so LS This command, suppose we want to change the behavior of the program, display more information for each file, instead of just displaying the file name. In this case,-all is called an optional parameter. Similarly, LS also has the-H option parameter, which means that the help text is opened. This is very useful, you can meet a program you have never used before, and you can simply read the help text to figure out how it works.
Argparse's role is to encapsulate the py file can be selected parameters, so that they more flexible and rich.
Basics of 2.Argparse
The underlying invocation of Argparse is described below. Write the following code, save as prog.py
1 Import Argparse 2 parser = Argparse. Argumentparser ()3 Parser.parse_args ()
OK, call Python3, run a try:
--Help
usage:prog.py [-h]optional arguments: -H,--help show this helpmessage and exit
--verboseusage:prog.py [---verbose
$ python3 prog.py foousage:prog.py [-h]prog.py:error:unrecognized arguments:foo
The first command runs the script directly, without any output, because the script does not run any options and the source program does not set the default, so there will be no parameters out. The second command uses the--HELP parameter without error, the system gives the result, which is the system default help information. It is clear that the last two uses were error-free because the script did not write verbose and foo.
2. Add parameters to Argparse
Similarly, write a Python code that is saved as prog.py for later invocation:
1 Import Argparse 2 parser = Argparse. Argumentparser ()3 parser.add_argument ("echo")4 args = Parser.parse_args ()5print(Args.echo)
The results of the operation are as follows:
$ python3 prog.pyusage:prog.py [-h] echo prog.py:error:the following arguments is required: echo $ python3 prog.py --helpusage : prog.py [-h] echo positional arguments: echo optional arguments: -H,--< Span style= "COLOR: #000000" >help show this help message and exit$ python3 prog.py foofoo
After adding add_argument (), this is the command-line option that we use to specify that the program can accept . At this point I have named Echo (the output command from Linux), so when called, we are asked to specify an option. the Parse_args () method actually returns some data from the specified options, and this is echo. Argparse can be executed directly (that is, you do not need to specify which stored variable). It matches the name and the specified string parameter echo.
Although the help display looks good, it is not very effective at the moment. For example, although ECHO is used as a positional parameter, we don't know what it does except by guessing or by reading the source code. The next step is to make the Help document more effective:
To be Continued--------------
What is the Argparse module for Python? (not finished)