image.png
fire是python中用于生成命令行界面(Command Line Interfaces, CLIs)的工具,不需要做任何额外的工作,只需要从主模块中调用fire.Fire(),它会自动将你的代码转化为CLI,Fire()的参数可以说任何的python对象
使用pip命令:
pip install fire
image.png
安装后可以简单的来看下执行效果: 代码:
import fire
class Calculator(object):
"""A simple calculator class."""
def double(self, number):
return 2 * number
if __name__ == '__main__':
fire.Fire(Calculator)
执行命令:
$ python testfire.py
运行结果:
image.png
调用接口函数:
$ python testfire.py double 20
运行结果:
image.png