python fire 是開發和調試很是用的工具,只需將想要在命令行顯示的功能寫爲函數/模塊/類,就能夠在命令行模式下調用
sudo pip install fire
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'
#!/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'
參考官方例子python