I. Introduction
Fire is a tool used in Python to generate command line Interfaces, CLIs, without any additional work, requiring only fire from the main module. Fire (), it will automatically convert your code to Cli,fire () parameters can say any Python object
two. Installation
Pip Install fire
three. UsageInstance 1 single function: Python module name parameter 1 parameter 2
Create a new test_fire.py module in the common directory
Import Kindle def Add (A, b): = A + b return countif__name__'__main__ ' : Fire . Fire (ADD)
When no parameters are taken, the Python test_fire.py will display the Help information
The fire is displayed in the Help message. Fire () parameter type (function), file path, document string, parameter usage and other information
The result of the add parameter operation is as follows:
Note: The two method calls, one is direct with the argument, one is--formal parameter argument form, for convenience, this article takes the first kind of
Instance 2 multiple functions: Python module name Function name parameter
Import Kindle def Add (A, b): = A + b return countdef Sub (A, b): = A- b return result if __name__ ' __main__ ' : Fire . Fire ()
Plus the result of the parameter operation:
Instance 3 Class (object) Multiple functions: Python module name Function name parameter
ImportKindleclassCalculator (object):defAdd (self, A, b): Count= A +breturnCountdefSub (self, A, b): Result= A-breturnresultif __name__=='__main__': Fire. Fire (Calculator) #这里用类名Calculator或者类的实例化对象Calculator () The result is the same.
Add parameter Run Result:
Note:
1. Fire is used by default -
as a parameter delimiter, so if you want to pass in a similar parameter at the command line 2017-04-22
, then the program will not receive the parameters 2017-04-22
, you need --separator
to use to change the delimiter
2. fire
Automatically distinguishes the type of parameters you pass in the command line, such as automatically recognized, automatically recognized, and automatically recognized 20170422
int
hello
str
‘(1,2)‘
tuple
‘{"name": "Alan Lee"}‘
dict
. But what if you want to pass in a string type 20170422
? Then you need to write: ‘"20170422"‘
or "‘20170422‘"
\"20170422\"
, anyway, just add an escape, because the command line will eat your quotes by default
Reference Articles
70332074
Https://mp.weixin.qq.com/s/lMOieL_RFRHsh5OW4G4axg
Use of the Python Fire Library