1 min read

Python Fire Module

Python Fire is a library for automatically generating command line interfaces (CLIs) from absolutely any Python object.

Installation

pip install fire

Basic Usage

Exposing a Function

You can turn a simple function into a CLI.

import fire

def hello(name="World"):
  return "Hello %s!" % name

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

Run it from the command line:

python script.py --name=David
# Output: Hello David!

Exposing a Class

You can expose a class and its methods.

import fire

class Calculator(object):
  def add(self, x, y):
    return x + y

  def multiply(self, x, y):
    return x * y

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

Run it:

python script.py add 10 20
# Output: 30

programming/python/python