Google open source Python command-line library: Preliminary fire



Author: HelloGitHub- Prodesire

HelloGitHub of "explain open source projects" series, Project Address: https://github.com/HelloGitHub-Team/Article

I. Introduction

In front of all the articles in this series, we were introduced argparse, docoptand clickthe main features and usage. They are distinctive, can be well done command-line tasks. argparseA process-oriented, it is necessary to set the parser, and then define the parameters, and then parses the command line, the last business logic. docoptFirst-out parameters declarative syntax definition, and another program to parse the command line and implement business logic. clickIs further simplified explicit command to invoke logic with decorator way, but still not object-oriented.

Today to introduce the fire is in a generalized object-oriented approach to Fun command line, such an object can be a class, function, dictionaries, lists, etc., it is more flexible and easier.

本系列文章默认使用 Python 3 作为解释器进行讲解。
若你仍在使用 Python 2,请注意两者之间语法和库的使用差异哦~

Second, the introduction

fire can automatically generate a command line interface according to any Python object. It has the following features:

  • CLI can be generated in a simple manner
  • It is a practical tool for developing and debugging Python code
  • Existing code or can someone else's code into CLI
  • Such that the transition between the Python and Bash easier
  • By presetting a desired module and REPL variables, such utility easier REPL

By the following command to quickly install firethe library:

pip install fire

Third, Quick Start

Recall that the use of argparse, docoptand clicksteps command line program implementation:

  • 对于 argparse 来说,要先设置解析器,再定义参数,再解析命令行,最后实现业务逻辑(四步)
  • 对于 docopt 来说,要先定义定义接口描述,再解析命令行,最后实现业务逻辑(三步)
  • 对于 click 来说,就是实现业务逻辑和通过装饰器的方式定义参数(两步)

它们的实现步骤越来越简单,从四步简化到了两步。而今天的主角 fire 只需一步,现业务逻辑就够了。

这简直简单的不可思议,为什么这样做就够了?我们不妨考虑下 Python 中的函数,函数是不是可以对应一个命令行程序,而函数的参数可以对应命令行程序的参数和选项呢?再看看 Python 中的类,一个类是不是可以对应一个命令行程序,而类中的每个实例方法就可以对应子命令,实例方法中的参数就是对应子命令的参数和选项。

这么一想,理论上确实是可以实现的,我们不妨通过下面的示例来看看 fire 是如何让我们通过简单的方式实现命令行程序。

3.1 使用函数

来看这么一个例子:

import fire

def hello(name="World"):
  return 'Hello {name}!'.format(name=name)

if __name__ == '__main__':
  fire.Fire(hello)

在上述例子中定义一个 hello 函数,它接受 name 参数,并且有默认值 "World"。使用 fire.Fire(hello) 即可非常简单快速地实现命令功能,这个命令行就接受 --name 选项,不提供时使用默认值 "World",提供时就按提供的值来。

可在命令行中执行下列命令:

$ python hello.py
Hello World!
$ python hello.py --name=Prodesire
Hello Prodesire!
$ python hello.py --help
INFO: Showing help with the command 'hello.py -- --help'.

NAME
    hello.py

SYNOPSIS
    hello.py <flags>

FLAGS
    --name=NAME

3.2 使用类

使用函数是最简单的方式,如果我们想以更有组织的方式来实现,比如使用类,fire 也是支持的。

import fire

class Calculator(object):
  """A simple calculator class."""

  def double(self, number):
    return 2 * number

  def triple(self, number):
    return 3 * number

if __name__ == '__main__':
  fire.Fire(Calculator)

在上述例子中定义一个 Calculator 类,它有两个实例方法 doubletriple,并且都接受 number 参数,没有默认值。使用 fire.Fire(Calculator) 即可非常简单快速地实现命令功能,这个命令行支持两个子命令 doubletriple,位置参数 NUMBER 或选项参数 --number

可在命令行中执行下列命令:

$ python calculator.py double 10
20
$ python calculator.py triple --number=15
45
$ python calculator.py double --help
INFO: Showing help with the command 'calculator.py double -- --help'.

NAME
    calculator.py double

SYNOPSIS
    calculator.py double NUMBER

POSITIONAL ARGUMENTS
    NUMBER

NOTES
    You can also use flags syntax for POSITIONAL ARGUMENTS

四、小结

fire 的使用方式非常简单,定一个 Python 对象,剩下的就交给 fire 来处理,可谓是非常的 Pythonic,这也是它会如此受欢迎的原因。

除了上面展示的内容,fire 还支持更多种类的 Python 对象,也拥有很多强大的功能,我们将在接下来几节中逐步走近它。


『讲解开源项目系列』——让对开源项目感兴趣的人不再畏惧、让开源项目的发起者不再孤单。跟着我们的文章,你会发现编程的乐趣、使用和发现参与开源项目如此简单。欢迎留言联系我们、加入我们,让更多人爱上开源、贡献开源~

Guess you like

Origin www.cnblogs.com/xueweihan/p/12084550.html