A library for automatically generating command line interfaces from any Python object.
—
Fire provides comprehensive utilities for generating help text and usage information for CLI components, with support for docstring parsing, automatic formatting, and customizable output.
Generate formatted help text for any Fire component, including function signatures, docstrings, and usage examples.
def HelpText(component, trace=None, verbose=False):
"""
Generate help text for a component.
Analyzes the component to extract documentation, signatures, and usage
information, formatting it for display to users.
Parameters:
- component: Component to generate help for (function, class, module, etc.)
- trace: Optional FireTrace object showing execution path
- verbose: Whether to include private/hidden members and detailed info
Returns:
String containing formatted help text
"""Generate concise usage information showing how to invoke a component from the command line.
def UsageText(component, trace=None, verbose=False):
"""
Generate usage text for a component.
Creates a concise summary of how to use the component from the command line,
including argument names, types, and optional parameters.
Parameters:
- component: Component to generate usage for
- trace: Optional FireTrace object showing execution path
- verbose: Whether to include additional usage details
Returns:
String containing formatted usage text
"""Fire includes comprehensive docstring parsing capabilities for extracting structured information from Python docstrings.
class DocstringInfo:
"""
Information extracted from docstrings (namedtuple).
Attributes:
- summary: Brief description (first line)
- description: Detailed description
- args: List of ArgInfo objects for parameters
- returns: Return value description
- yields: Generator yield value description
- raises: Exception information
"""
class ArgInfo:
"""
Argument information from docstrings (namedtuple).
Attributes:
- name: Parameter name
- type: Parameter type annotation or inferred type
- description: Parameter description
"""
class KwargInfo(ArgInfo):
"""
Keyword argument information, extends ArgInfo (namedtuple).
Same attributes as ArgInfo but specifically for keyword arguments.
"""Basic help generation:
import fire
from fire import helptext
def process_file(filename, output_dir="./output", verbose=False):
"""
Process a file and save results.
Args:
filename: Path to input file to process
output_dir: Directory to save processed results
verbose: Enable verbose logging output
Returns:
Path to the processed output file
"""
# Implementation here
pass
# Generate help text
help_text = helptext.HelpText(process_file)
print(help_text)
# Generate usage text
usage_text = helptext.UsageText(process_file)
print(usage_text)Class help generation:
import fire
from fire import helptext
class DataAnalyzer:
"""Analyze datasets with various statistical methods."""
def __init__(self, data_path):
"""Initialize analyzer with data file path."""
self.data_path = data_path
def summary(self, include_plots=False):
"""
Generate summary statistics.
Args:
include_plots: Whether to generate visualization plots
Returns:
Dictionary containing summary statistics
"""
pass
def correlations(self, method='pearson'):
"""
Calculate correlations between variables.
Args:
method: Correlation method ('pearson', 'spearman', 'kendall')
Returns:
Correlation matrix as pandas DataFrame
"""
pass
# Generate help for the class
help_text = helptext.HelpText(DataAnalyzer)
print(help_text)Using Fire's built-in help:
import fire
class Calculator:
"""A simple calculator with basic arithmetic operations."""
def add(self, x, y):
"""Add two numbers together."""
return x + y
def multiply(self, x, y):
"""Multiply two numbers."""
return x * y
if __name__ == '__main__':
fire.Fire(Calculator)
# Command line: python calc.py -- --help
# Shows help for the Calculator class
# Command line: python calc.py add -- --help
# Shows help for the add methodAutomatic signature extraction: Fire automatically extracts and formats function signatures, including:
Docstring parsing: Supports multiple docstring formats:
Usage examples:
# Get help for main component
python my_cli.py -- --help
# Get help for specific subcommand
python my_cli.py subcommand -- --help
# Verbose help with private members
python my_cli.py -- --help --verbose
# Usage information only
python my_cli.py -- --usageFormatted output includes:
Fire's help system respects:
The help text automatically adapts to the component type:
Install with Tessl CLI
npx tessl i tessl/pypi-fire