fire.Fire()---自动生成命令行
来源:互联网 发布:柬埔寨翻译中文软件 编辑:程序博客网 时间:2024/06/12 13:00
Python 中用于生成命令行接口(Command Line Interfaces, CLIs)的工具已经有一些了,例如已经成为 Python 标准库的 argparse 和第三方的 click ,这些都是很不错的工具。但是这些工具为 Python 程序生成 CLIs 的时候稍显麻烦,需要增加的地方比较多,过程稍显繁琐。今天介绍的这个工具,几乎可以不改变原始代码就可以生成 CLIs,是 Google 于 今年(2017 年)3 月 2 日宣布在 GitHub 上开源的。
文中的三个程序文件可以在 这里 打包下载。
简介
大致的简介我在上面简单说了下,下面我引用下官方简介:
Python Fire is a library for creating command line interfaces (CLIs) from absolutely any Python object.
- Python Fire is a simple way to create a CLI in Python.
- Python Fire is a helpful tool for developing and debugging Python code.
- Python Fire helps with exploring existing code or turning other people’s code into a CLI.
- Python Fire makes transitioning between Bash and Python easier.
- Python Fire makes using a Python REPL easier by setting up the REPL with the modules and variables you’ll need already imported and created.
可以看出来,Fire 不仅仅是一个生成 CLIs 的工具,而且还可以调试 Python 程序,交互式的使用 Fire 。
安装
在命令行中运行 pip install fire
即可安装。
用法
使用 fire
生成 CLIs 的基本用法就是使用 fire.Fire()
,可以传入任意参数,用官方的说法就是 any Python object 。下面我举三个例子简单的说明一下用法,更多的用法大家可以参考 The Python Fire Guide 和 Using a Fire CLI 。
下面的例子均是以计算时间差(两个日期之间相差的天数)的程序为例。
单个函数
文件 test-fire.py
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
这个程序中只有一个函数 cal_days
,最后使用 fire.Fire(cal_days)
来生成 CLIs。
我们可以先用下面的语句查看帮助信息:
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
我们可以看到传给 fire.Fire()
的参数类型(function
)、文件路径、文档字符串、参数用法等信息。
现在我们试着计算一下 2017 年 4 月 21 号和 2017 年 4 月 1 号差了多少天:
- 1
- 2
当然,你也可以使用 python test-fire.py --date-str1 20170421 --date-str2 20170401
。
此外,对于这种单个函数的情况,程序的最后一行 fire.Fire(cal_days)
可以改为 fire.Fire()
,结果完全一样,fire.Fire()
会默认使用 cal_days()
函数。
多个函数
文件 test-fire-2func.py
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
这个程序比 test-fire.py
多了一个函数,但是结尾处仍然使用 fire.Fire()
来生成 CLIs。我们先不输任何参数:
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
程序中有个两个函数,而我们运行 python test-fire-2func.py 20170401
的时候却没有指定使用哪个函数,所以会报错。
正确的写法应该是这样的:
- 1
- 2
- 3
- 4
也就是说在后面跟上要使用的函数名。
对象(类)
Fire
既然能够 fire 任何 Python 对象,那么给 fire.Fire()
传一个类也是完全可以的。下面是一个例子:
文件 test-fire-class.py
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
这里我定义了一个 DateStr
类,有两个类方法 cal_days
和 days2today
,那么我只需 fire.Fire(DateStr)
就可以调用这两个类方法。
- 1
- 2
- 3
- 4
还有几点
fire
默认使用-
作为参数分隔符,所以如果你要在命令行传入类似2017-04-22
的参数时,那么程序接收到的参数就肯定不是2017-04-22
了,你需要使用--separator
来改变分隔符,参考 Changing the Separatorfire
会自动区分你在命令行传入的参数的类型,例如20170422
会自动识别成int
,hello
会自动识别成str
,'(1,2)'
会自动识别成tuple
,'{"name": "Alan Lee"}'
会自动识别成dict
。但是你如果想要传入一个字符串类型的20170422
怎么办?那就需要这样写:'"20170422"'
或者"'20170422'"
或者\"20170422\"
,总之呢,就是加一个转义,因为命令行默认会吃掉你的引号。参考 Argument Parsing
转自:http://blog.csdn.net/u010099080/article/details/70332074
- fire.Fire()---自动生成命令行
- 谷歌开源 Python Fire:可自动生成命令行接口
- Python Fire开源—用于自动生成命令行界面的内容库
- Fire!
- Fire!
- Fire!
- Fire!
- Fire
- Fire!
- Fire!
- Fire
- 命令行接口神器:fire
- UVa10243 - Fire! Fire!! Fire!!!(dp)
- Fire Net
- Fire Net
- Fire Net
- Fire Net
- Fire Net
- 腾讯云下Tomcat里部署的项目访问不了!
- 不是人人都懂的学习要点
- Android 权限申请最佳适配方案——permissions4m
- 一、Spring Cloud系列:Zipkin服务端配置
- [bzoj1673][Usaco2005 Dec]Scales 天平
- fire.Fire()---自动生成命令行
- train_val.prototxt和deploy.prototxt文件解读
- Java 简介
- Java学习第一天
- Oracle行列转换
- 菜鸟学设计模式----装饰器模式
- Linux常用命令整合
- 新手入门贴:史上最全Web端即时通讯技术原理详解
- Zuul的高可用