A library for automatically generating command line interfaces from any Python object.
—
The main Fire function provides the core functionality for automatically generating command-line interfaces from any Python object. It handles argument parsing, component traversal, function calling, and output formatting.
The primary entry point that transforms Python objects into CLIs through recursive component traversal and argument consumption.
def Fire(component=None, command=None, name=None, serialize=None):
"""
Create a CLI from any Python object.
Executes a command either from the command argument or from sys.argv by
recursively traversing the target object's members, consuming arguments,
evaluating functions, and instantiating classes.
Parameters:
- component: The initial target component (function, class, module, object, dict, list, etc.)
- command: Optional command to execute (string or list of strings)
- name: Optional name of the command for interactive mode and completion
- serialize: Optional callable to serialize all objects to text
Returns:
Result of executing the Fire command
Raises:
- ValueError: If command argument is not a string or list
- FireError: For Fire-specific errors during execution
- FireExit: For controlled exit conditions
"""Basic Function CLI:
import fire
def greet(name, greeting="Hello"):
return f"{greeting}, {name}!"
if __name__ == '__main__':
fire.Fire(greet)
# Command line: python script.py John --greeting=Hi
# Output: Hi, John!Class-based CLI:
import fire
class Calculator:
def add(self, x, y):
return x + y
def multiply(self, x, y):
return x * y
if __name__ == '__main__':
fire.Fire(Calculator)
# Command line: python script.py add 5 3
# Output: 8Module CLI:
import fire
import math
if __name__ == '__main__':
fire.Fire(math)
# Command line: python script.py sqrt 16
# Output: 4.0Dictionary CLI:
import fire
def task1():
return "Task 1 completed"
def task2():
return "Task 2 completed"
commands = {
'task1': task1,
'task2': task2
}
if __name__ == '__main__':
fire.Fire(commands)
# Command line: python script.py task1
# Output: Task 1 completedHelper functions used internally by Fire for output display and completion script generation.
def Display(lines, out):
"""
Display lines to output stream.
Parameters:
- lines: Iterable of strings to display
- out: Output stream (file-like object)
"""
def CompletionScript(name, component, shell):
"""
Generate completion script for a Fire CLI (wrapper function).
Parameters:
- name: Name of the CLI command
- component: Component to generate completion for
- shell: Shell type ('bash' or 'fish')
Returns:
String containing the completion script
"""Fire CLIs support universal flags that work with any Fire-generated CLI:
--help or -h: Show help information--interactive or -i: Drop into interactive Python REPL after running command--verbose or -v: Include private members in help and usage information--completion: Generate Bash completion script to stdout--completion fish: Generate Fish completion script to stdout--separator SEPARATOR: Use custom separator instead of default '-'--trace: Show Fire execution trace for the commandThese flags must be separated from component arguments with --:
python script.py command arg1 arg2 -- --help --verboseInstall with Tessl CLI
npx tessl i tessl/pypi-fire