|
|
## 使用Fire最简单的方法是在任何Python程序结束时调用fire.Fire()。 这会将程序的全部内容暴露给命令行。
|
|
|
import fire
|
|
|
def hello(name):
|
|
|
return 'Hello {name}!'.format(name=name)
|
|
|
if __name__ == '__main__':
|
|
|
fire.Fire()
|
|
|
从命令行运行程序:
|
|
|
$ python example.py hello World
|
|
|
Hello World!
|
|
|
|
|
|
## 暴露多个命令最简单的方法是编写多个函数,然后调用Fire。
|
|
|
|
|
|
import fire
|
|
|
|
|
|
def add(x, y):
|
|
|
return x + y
|
|
|
|
|
|
def multiply(x, y):
|
|
|
return x * y
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
fire.Fire()
|
|
|
我们可以这样使用它:
|
|
|
$ python example.py add 10 20
|
|
|
30
|
|
|
$ python example.py multiply 10 20
|
|
|
200
|
|
|
|
|
|
## 下面是一个如何使用分组命令创建命令行界面的示例。
|
|
|
|
|
|
class IngestionStage(object):
|
|
|
|
|
|
def run(self):
|
|
|
return 'Ingesting! Nom nom nom...'
|
|
|
|
|
|
class DigestionStage(object):
|
|
|
|
|
|
def run(self, volume=1):
|
|
|
return ' '.join(['Burp!'] * volume)
|
|
|
|
|
|
def status(self):
|
|
|
return 'Satiated.'
|
|
|
|
|
|
class Pipeline(object):
|
|
|
|
|
|
def __init__(self):
|
|
|
self.ingestion = IngestionStage()
|
|
|
self.digestion = DigestionStage()
|
|
|
|
|
|
def run(self):
|
|
|
self.ingestion.run()
|
|
|
self.digestion.run()
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
fire.Fire(Pipeline)
|
|
|
以下是使用方式:
|
|
|
$ python example.py run
|
|
|
Ingesting! Nom nom nom...
|
|
|
Burp!
|
|
|
$ python example.py ingestion run
|
|
|
Ingesting! Nom nom nom...
|
|
|
$ python example.py digestion run
|
|
|
Burp!
|
|
|
$ python example.py digestion status
|
|
|
Satiated.
|
|
|
|
|
|
(1)参数的类型取决于它们的值,而不是使用它们的函数签名。 您可以从命令行传递任何Python文本:数字,字符串,元组,列表,字典(仅在某些版本的Python中支持集合)。只要它们只包含文字,您也可以任意嵌套这些集合。
|
|
|
|
|
|
为了演示这个,我们将制作一个小程序,通过这个小程序告诉我们传给它的参数类型:
|
|
|
|
|
|
import fire
|
|
|
fire.Fire(lambda obj: type(obj).__name__)
|
|
|
|
|
|
我们会像这样使用它:
|
|
|
|
|
|
$ python example.py 10
|
|
|
int
|
|
|
$ python example.py 10.0
|
|
|
float
|
|
|
$ python example.py hello
|
|
|
str
|
|
|
$ python example.py '(1,2)'
|
|
|
tuple
|
|
|
$ python example.py [1,2]
|
|
|
list
|
|
|
$ python example.py True
|
|
|
bool
|
|
|
$ python example.py {name: David}
|
|
|
dict
|
|
|
在最后一个例子中,你会注意到裸词会自动替换为字符串。
|
|
|
要注意! 如果你想传递字符串"10"而不是int 10,你需要转义或者引用你的引号。 否则,Bash将会把你的引用取消并将一个没有引号的10传递给你的Python程序,在那里Fire将把它解释为一个数字。
|
|
|
|
|
|
$ python example.py 10
|
|
|
int
|
|
|
$ python example.py "10"
|
|
|
int
|
|
|
$ python example.py '"10"'
|
|
|
str
|
|
|
$ python example.py "'10'"
|
|
|
str
|
|
|
$ python example.py \"10\"
|
|
|
str
|
|
|
要注意! 记住Bash首先处理你的参数,然后Fire解析结果。 如果你想将dict {"name":"David Bieber"}传递给你的程序,你可以试试这个:
|
|
|
$ python example.py '{"name": "David Bieber"}' # Good! Do this.
|
|
|
dict
|
|
|
$ python example.py {"name":'"David Bieber"'} # Okay.
|
|
|
dict
|
|
|
$ python example.py {"name":"David Bieber"} # Wrong. This is parsed as a string.
|
|
|
str
|
|
|
$ python example.py {"name": "David Bieber"} # Wrong. This isn't even treated as a single argument.
|
|
|
<error>
|
|
|
$ python example.py '{"name": "Justin Bieber"}' # Wrong. This is not the Bieber you're looking for. (The syntax is fine though :))
|
|
|
dict
|
|
|
(2)rue和False被解析为布尔值。
|
|
|
|
|
|
你也可以通过--flag语法--name和--noname来指定布尔值,它们分别将名称设置为True和False。
|
|
|
|
|
|
继续前面的例子,我们可以运行以下任何一个:
|
|
|
|
|
|
$ python example.py --obj=True
|
|
|
bool
|
|
|
$ python example.py --obj=False
|
|
|
bool
|
|
|
$ python example.py --obj
|
|
|
bool
|
|
|
$ python example.py --noobj
|
|
|
bool
|