From 5154cdc906d2a5deca44e1237281d91a30cc6387 Mon Sep 17 00:00:00 2001 From: pfb3lt78m <1137773494@qq.com> Date: Thu, 13 May 2021 22:49:49 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8F=98=E9=87=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 新建文本文档 (2).txt | 63 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 新建文本文档 (2).txt diff --git a/新建文本文档 (2).txt b/新建文本文档 (2).txt new file mode 100644 index 0000000..296ad5f --- /dev/null +++ b/新建文本文档 (2).txt @@ -0,0 +1,63 @@ +(1)使用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! +(2)暴露多个命令最简单的方法是编写多个函数,然后调用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 +(3)下面是一个如何使用分组命令创建命令行界面的示例。 + +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. \ No newline at end of file