Python Fire 是一个用于从任何 Python 对象自动生成命令行界面 (CLI) 的库。
要使用 pip 安装 Python Fire,请运行:pip install fire
要使用 conda 安装 Python Fire,请运行:conda install fire -c conda-forge
要从源代码安装 Python Fire,请先克隆代码库,然后运行:python setup.py install
你可以在任何 Python 对象上调用 Fire:
函数、类、模块、对象、字典、列表、元组等等。
它们都可以!
这是一个在函数上调用 Fire 的示例。
import fire
def hello(name="World"):
return "Hello %s!" % name
if __name__ == '__main__':
fire.Fire(hello)
然后,您可以从命令行运行:
python hello.py # Hello World!
python hello.py --name=David # Hello David!
python hello.py --help # Shows usage information.
这是在类上调用 Fire 的示例。
import fire
class Calculator(object):
"""A simple calculator class."""
def double(self, number):
return 2 * number
if __name__ == '__main__':
fire.Fire(Calculator)
然后,您可以从命令行运行:
python calculator.py double 10 # 20
python calculator.py double --number=15 # 30
要了解 Fire 如何处理函数、对象、字典、列表等,以及
Fire 的其他功能,请参阅 使用 Fire CLI 页面。
更多示例,请参阅 Python Fire 指南。
当您调用 Fire 时,它会触发(执行)您的命令。
请参阅 Python Fire 指南。
| 设置 | 命令 | 说明 |
|---|---|---|
| 安装 | pip install fire |
| 创建 CLI | 命令 | 说明 |
|---|---|---|
| 导入 | import fire |
|
| 调用 | fire.Fire() |
将当前模块转换为 Fire CLI。 |
| 调用 | fire.Fire(component) |
将 component 转换为 Fire CLI。 |
| 使用 CLI | 命令 | 说明 |
|---|---|---|
| 帮助 | command --help 或 command -- --help |
|
| REPL | command -- --interactive |
进入交互模式。 |
| 分隔符 | command -- --separator=X |
将分隔符设置为 X。默认分隔符为 -。 |
| 完成 | command -- --completion [shell] |
为 CLI 生成完成脚本。 |
| 跟踪 | command -- --trace |
获取命令的 Fire 跟踪。 |
| 详细 | command -- --verbose |
请注意,这些标志与 Fire 命令之间用单独的“--”分隔。
根据 Apache 2.0 许可证授权。
这不是 Google 官方产品。