Python Fire, which can generate command-line interfaces from any Python codeSimply call the Fire function in any Python program to automatically convert that program to a CLI.

Python Fire Github

Acquisition method:

  • Get from pypi `pip install fire`

  • Get from conda`conda install fire -c conda-forge`

  • Install from source, clone code base & `python install`


What are the advantages of Python Fire:

  • There is no need to define parameters, set help messages or write a main function that defines how the code runs. Simply call the `Fire` function from the main module.

  • Convert any Python object (class, object, dictionary, function, or even an entire module) into a command-line interface, and output annotation labels and documentation.

  • The command line interface is kept up to date in real time as the code changes.


Example 1


1# calling Fire a function
2import fire
3def hello(name="World"):
4  return "Hello %s!" % name
6if __name__ ==  __main__ :
7  fire.Fire(hello)
10# from the command line,then run:
11python  # Hello World!
12python --name=David  # Hello David!
13python --help  # Shows usage information.

Example 2


1# calling Fire on a class
2import fire
4class Calculator(object):
5  """A simple calculator class."""
7  def double(self, number):
8    return 2 * number
10if __name__ ==  __main__ :
11  fire.Fire(Calculator)
13# from the command line, then run:
15python double 10  # 20
16python double --number=15  # 30

Example 3


1#!/usr/bin/env python
2import fire
3class Example(object):
4 def hello(self, name= world ):
5 """Says hello to the specified name."""
6 return  Hello {name}! .format(name=name)
8def main():
9 fire.Fire(Example)
11if __name__ ==  __main__ :
12 main()
15 When the Fire function is run, our command is executed.
16By simply calling Fire, we can now use the sample class as a command line tool.
19$ ./ hello
20Hello world!
22$ ./ hello David
23Hello David!
25$ ./ hello --name=Google
26Hello Google!


Each Fire CLI comes with an interactive mode.

  • Use the "-interactive" flag and command line and other defined variables to log into IPython REPL when running the CLI.

  • Be sure to check out the Python Fire documentation to learn more about the useful features of Fire.

Related articles

Pytype Usage and Examples

Pytype checks and infers the type of Python code:Use lint to check pure Python code, flag common errors such as misspelled property names, incorrect function calls, and much more, and it can even span files.

Encrypting files with Python

In life, sometimes we need to encrypt some important files,Python provides easy-to-use cryptographic libraries such as hashlib, base64, etc.

Quickly test your Python code with Hypothesis

Testing is important no matter which programming language or framework you use. hypothesis is an advanced testing library for Python. It allows writing test cases with parameters, and then generating easy-to-understand test data that makes the test fail.

mplcyberpunk tutorials

Cyberpunk 2077" is a very popular single-player game ~ The game is set in the year 2077, a highly developed technology but chaotic and disorderly "cyberpunk" city. In this world, although the technology is highly developed, but the standard of living of

Heartrate: Real-time Visualization for Python Execution

Heartate - Track programs like a heart rate,Heartrate is a Python tool library that allows you to visualize the execution of Python programs in real time. Monitoring a running Python program is shown in the following figure.

Implementing reverse proxies with Django only

When you think of reverse proxies, you say nginx. nginx is the ideal reverse proxy tool. But now the conditions are tough. The server doesn't have nginx and doesn't have root privileges, which means you can't compile and install nginx, and only one port,

What does join mean in python?

Python has two functions .join() and os.path.join(), which do the following: . join(): Concatenate string arrays. Concatenates the elements of a string, a tuple, and a list with the specified characters (separator) to generate a new string os.path.join()

Add new content to the python dict

Adding new content to a dictionary is done by adding new key/value pairs, modifying or deleting existing key/value pairs as in the following examples

How to delete elements in python dict

clear() method is used to clear all the data in the dictionary, because it is an in-place operation, so it returns None (also can be interpreted as no return value)