A library for automatically generating command line interfaces from any Python object.
npx @tessl/cli install tessl/pypi-fire@0.7.0A comprehensive Python library for automatically generating command line interfaces (CLIs) from any Python object. Fire enables developers to quickly create CLIs from existing Python code by calling Fire() on functions, classes, modules, dictionaries, lists, tuples, or any other Python objects without additional configuration.
pip install fireimport fireFor advanced functionality, import specific modules:
from fire import decorators # Custom argument parsing
from fire import completion # Shell completion scripts
from fire import interact # Interactive REPL mode
from fire import helptext # Help text generation
from fire import trace # Execution tracingimport fire
def hello(name="World"):
return f"Hello {name}!"
def add(x, y):
return x + y
class Calculator:
def multiply(self, x, y):
return x * y
if __name__ == '__main__':
# Convert function to CLI
fire.Fire(hello)
# Convert class to CLI
# fire.Fire(Calculator)
# Convert multiple functions to CLI
# fire.Fire({
# 'hello': hello,
# 'add': add,
# 'calc': Calculator
# })Python Fire uses a component traversal system that recursively processes command-line arguments:
Fire transforms any Python object into a CLI by consuming command arguments to either access members, call functions, or instantiate classes, continuing until all arguments are processed.
The main Fire function that transforms any Python object into a command-line interface. Supports functions, classes, modules, objects, dictionaries, lists, and tuples with automatic argument parsing and help generation.
def Fire(component=None, command=None, name=None, serialize=None):
"""
Main entry point for creating CLIs from Python objects.
Parameters:
- component: The Python object to convert to CLI
- command: Optional command string/list to execute
- name: Optional name for the CLI command
- serialize: Optional serialization function for output
Returns:
Result of executing the Fire command
"""Decorators and utilities for customizing how Fire parses command-line arguments, allowing fine-grained control over argument conversion and validation.
def SetParseFn(fn, *arguments):
"""Decorator to set custom parsing function for arguments."""
def SetParseFns(*positional, **named):
"""Decorator to set multiple custom parsing functions."""Functionality for generating shell completion scripts (Bash and Fish) that provide tab completion for Fire-generated CLIs.
def Script(name, component, default_options=None, shell='bash'):
"""Generate shell completion script for a Fire CLI."""Interactive REPL functionality that allows users to drop into a Python shell with access to the CLI components and execution context.
def Embed(variables, verbose=False):
"""Drop into Python REPL with variables available."""Utilities for generating help text and usage information for Fire CLIs, with support for docstring parsing and formatting.
def HelpText(component, trace=None, verbose=False):
"""Generate help text for a component."""
def UsageText(component, trace=None, verbose=False):
"""Generate usage text for a component."""class FireError(Exception):
"""Base exception for Fire-related errors."""
class FireExit(SystemExit):
"""System exit exception used by Fire."""
class FireTrace:
"""
Represents execution trace of Fire commands.
Tracks the sequence of steps taken during Fire execution, including
component traversal, function calls, and property access.
Attributes:
- name: Optional name of the CLI command
- separator: Separator used in command parsing (default: '-')
- elements: List of FireTraceElement objects
- verbose: Whether to include verbose information
- show_help: Whether help was requested
- show_trace: Whether trace output was requested
"""
class FireTraceElement:
"""
Individual element in Fire execution trace.
Represents a single step in the Fire execution process, such as
instantiating a class, calling a function, or accessing a property.
Attributes:
- component: The component being processed
- action: Type of action performed
- target: Target of the action (if applicable)
- args: Arguments used in the action
- filename: Source file where action occurred
- lineno: Line number where action occurred
"""