what is python fire
python fire 是开发和调试非常用的工具,只需将想要在命令行显示的功能写为函数/模块/类,就可以在命令行模式下调用
python fire install
sudo pip install fire
method call on class
example.py:
#!/usr/bin/env python
# coding=utf-8
import fire
class Example(object):
def hello(self,name='world'):
return 'hello {name}'.format(name=name)
def hello1(self,name='world'):
return 'hello {name}'.format(name=name)
def main():
fire.Fire(Example)
if __name__ == '__main__':
main()
调用:./example.py hello1 或者 ./example.py hello1 --name='world'
Calls to separate functions
#!/usr/bin/env python
# coding=utf-8
import fire
def identity(arg=None):
return arg, type(arg)
def identity1(arg=None):
return 'c1'
def main(_=None):
#fire.Fire(identity1, name='identity')
fire.Fire(name='identity')
if __name__ == '__main__':
main()
调用:./identity.py identity 1 或者 ./identity.py identity1 '1'
Refer to the official example