版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/y472360651/article/details/82838354
Python Fire是一个可以从任何Python对象自动生成命令行接口的库:
- Python Fire是Python中创建CLI的一种简单方法
- Python Fire是开发和调试Python代码的有用工具
- Python Fire有助于探索现有代码或将其他人的代码转换为CLI
- Python Fire使得Bash和Python之间的转换更为容易
- Python Fire通过使用已经导入和创建的模块和变量来设置REPL,从而简化了Python REPL的使用
安装
pip install fire
使用
fire.Fire()
import fire
def hello(name):
return "hello {}!!!".format(name)
if __name__ == "__main__":
fire.Fire()
接下来,进入到Bash界面,并且进入到该Python文件所在路径:
$ python xxx.py hello laozhang
hello laozhang!!!
如果我们需要多指令情况,只需要定义多个函数即可,如下:
import fire
def hello(name):
return "hello {}!!!".format(name)
def world(name):
return "world {}!!!".format(name)
if __name__ == "__main__":
fire.Fire()
然后我们可以在Bash界面这样:
$ python xxx.py hello laozhang
hello laozhang!!!
$ python xxx.py world laowang
world laowang!!!
fire.Fire()
import fire
def hello(name):
return "hello {}!!!".format(name)
if __name__ == "__main__":
fire.Fire(hello)
然后,我们可以在Bash界面中这样:
$ python xxx.py zhangsan
hello zhangsan!!!
fire.Fire()
import fire
def hello(name):
return "hello {}!!!".format(name)
def world(name):
return "world {}!!!".format(name)
if __name__ == "__main__":
fire.Fire({
"hello": hello,
"world": world
})
然后,我们可以在Bash界面中这样:
$ python xxx.py hello zhangsan
hello zhangsan!!!
$ python xxx.py world laowang
world laowang!!!
fire.Fire()
import fire
class Tom(object):
def hello(self, name):
return "hello {}!!!".format(name)
def world(self, name):
return "world {}!!!".format(name)
if __name__ == "__main__":
tom = Tom()
fire.Fire(tom)
然后,我们可以在Bash界面中这样:
$ python xxx.py hello zhangsan
hello zhangsan!!!
$ python xxx.py world laowang
world laowang!!!
fire.Fire()
import fire
class Tom(object):
def hello(self, name):
return "hello {}!!!".format(name)
def world(self, name):
return "world {}!!!".format(name)
if __name__ == "__main__":
fire.Fire(Tom)
然后,我们可以在Bash界面中这样:
$ python xxx.py hello zhangsan
hello zhangsan!!!
$ python xxx.py world laowang
world laowang!!!
另外我们还可以在初始化方法中设置参数:
import fire
class Tom(object):
def __init__(self, separator="-"):
self.separator = separator
def hello(self, name):
return "hello{}{}!!!".format(self.separator, name)
def world(self, name):
return "world{}{}!!!".format(self.separator, name)
if __name__ == "__main__":
fire.Fire(Tom)
然后,我们可以在Bash界面中这样:
$ python xxx.py hello zhangsan
hello-zhangsan!!!
$ python xxx.py world laowang
world-laowang!!!
同样,我们可以对参数进行传值:
$ python xxx.py hello zhangsan --separator='*'
hello*zhangsan
$ python xxx.py world laowang --separator='*'
world*laowang
附加
最简单的fire
使用:
import fire
hello = 'hello world'
fire.Fire()
接下来,进入到Bash界面,并且进入到该Python文件所在路径:
$ python xxx.py hello
hello world
类型解析
我们可以在命令行中传递任何Python类型数据,从而获取它相应的类型,如下:
import fire
fire.Fire(lambda x: type(x).__name__)
然后我们可以这样使用它:
$ python xxx.py 10
int
$ python xxx.py '10'
int
$ python xxx.py "'10'"
str
$ python xxx.py '(10, )'
tuple
$ python xxx.py '[10]'
list
$ python xxx.py '{"name": 10}'
dict
带*args或**kwargs参数的函数
Fire支持使用*args
或**kwargs
参数的函数,如下:
import fire
def sorted_by_length(*items):
sroted_items = sorted(items, key=lambda x: (len(x), str(x)))
return ' '.join(sroted_items)
if __name__ == '__main__':
fire.Fire(sorted_by_length)
我们在Bash中运行:
$ python xxx.py cat snake dog
cat dog snake
另外,你可以使用分隔符来表示你已经完成了为函数提供参数的工作,分隔符之后所有参数将用于处理函数的结果,而不是传递给函数本身。默认的分隔符是-
。还是上面那个栗子,使用如下:
$ python xxx.py cat snake dog - upper
CAT DOG SNAKE
如果没有使用分隔符,将会被视为传递的一个参数,如下:
$ python xxx.py cat snake dog upper
cat dog snake upper
当然,我们还可以使用--separator
来更改分隔符,只不过还需要使用--
符号进行隔开,如下:
$ python xxx.py cat snake dog X upper -- --separator=X
CAT DOG SNAKE
这里,我们将默认的分隔符替换成为了X。
至此,Over~~~