Eraser, a funny advanced Internet bug.
This blog will bring you 10 parameter parsing libraries in Python. You must understand these [Module Armory Libraries] that comes with Python. After all, mastering one more library will give you one more way to solve problems.
Article Directory
-
- What is a parameter analysis library
- sys.argv
- getopt built-in parameter parsing library
- docopt parameter parsing library
- optparse parameter parsing library
- argparse parameter parsing library
- click parameter parsing library
- fire parameter parsing library
- typer parameter parsing library
- Commando parameter parsing library
- FLAGS parameter analysis library
- Write at the back
What is a parameter analysis library
In the period of time for a small Python script to run through python test.py 参数1 参数2 参数3
the form, you can pass parameters to the internal Python script library for parsing parameter is the parameter parsing library.
sys.argv
The most traditional way of writing is also the built-in content of Python, such as the following code:
import sys
print(sys.argv)
When you run the Python script by the following command, you can get to a list
type of parameter list.
python test.py 1 2 3
getopt built-in parameter parsing library
For sys.argv
purposes, and get a list, by getopt
parameter parsing library, you can obtain parameters.
getopt
The core functions in the library are the getopt.getopt()
specific parameters and usage methods. You can refer to this article in the Snowball series .
docopt parameter parsing library
An elegant command line module, you can learn and use through https://github.com/docopt/docopt .
optparse parameter parsing library
The command-line options of the optparse parser. After version 3.2, the optparse module has been deprecated and no further development; the baton is transferred to the argparse module.
To learn how to use it, you can refer to: https://docs.python.org/zh-cn/3/library/optparse.html#module-optparse
argparse parameter parsing library
By pip install argparse
the library to install, use relatively simple, it is the official definition of the command-line options, parameters and sub-command parser.
To learn how to use it, you can refer to: https://docs.python.org/zh-cn/3/library/argparse.html
click parameter parsing library
Click is an open source project of Flask's development team Pallets. It is a third-party module used to quickly create command lines.
For official address and usage, refer to github: https://github.com/pallets/click
fire parameter parsing library
fire is used to automatically generate a command-line interface from any Python object. You can turn any Python component into a command-line interface with just one call to Fire.
The tool library is open sourced by Google.
Open source address: https://github.com/google/python-fire
typer parameter parsing library
Typer is a library for building CLI applications.
Document: https://typer.tiangolo.com
Code: https://github.com/tiangolo/typer
Commando parameter parsing library
Based on argparse
the package, allowing the decorator manner defined commands and parameters.
Open source address: https://github.com/hyde/commando
FLAGS parameter analysis library
If you are doing deep learning related directions, especially often using tensorflow, then FLAGS is an essential command line tool.
The use of this module needs to be based on tensorflow, so it is the last one.
Write at the back
For these high-quality third-party libraries, the recommended order of Eraser is: fire -> click -> argparse -> **getopt **.
The super-high star of these two projects on github also proves that everyone recognizes them.
This blog does not explain all the libraries. After all, mastering a library is already based on the library to solve your problem.
Related Reading
- 100 Python crawler tutorials, great crawler tutorials, subscribe now
- Python game world (updated, the target number of articles is 50+, subscribe now, all are old fans)
- Python crawler lesson, 9 wonderful lectures
Today is the 121st /200th day of continuous writing .
If you want to build a close relationship blogger, you can focus on the public number of the same name dream eraser , close contact with a funny senior Internet worms.
Blogger ID: Dream Eraser, I hope everyone likes , comments , and favorites .