Python Fire
Python Fire is a library for automatically generating command-line interfaces (CLIs) from any Python object. It enables developers to turn functions, classes, objects, and dictionaries into CLIs with minimal code, simplifying the creation of powerful command-line tools. The current version is 0.7.1, with a consistent release cadence focusing on compatibility, feature enhancements, and bug fixes.
Warnings
- breaking Python Fire dropped support for Python 2 starting with version 0.7.0. If your project relies on Python 2, you must use an older version of the library.
- gotcha Arguments passed to a Fire-generated CLI that start with `--` (e.g., `--my-flag`) might not have been correctly interpreted as strings in very old versions of Fire. This could lead to unexpected parsing errors.
- gotcha While `fire.Fire()` is typically called directly within a script, you can also use `python -m fire <module_name>` or `python -m fire <path_to_file.py>` to generate a CLI for an existing module or file without modifying its source code. This is useful for exploring or interacting with third-party libraries.
Install
-
pip install fire
Imports
- Fire
from fire import Fire
Quickstart
import fire
def greet(name='World', enthusiasm=1):
"""Greets the given name with specified enthusiasm."""
return f"Hello {name}{'!' * enthusiasm}"
if __name__ == '__main__':
fire.Fire(greet)