or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

argument-parsing.mdcore-cli.mdhelp-documentation.mdindex.mdinteractive-mode.mdshell-completion.md
tile.json

tessl/pypi-fire

A library for automatically generating command line interfaces from any Python object.

Workspace
tessl
Visibility
Public
Created
Last updated
Describes
pypipkg:pypi/fire@0.7.x

To install, run

npx @tessl/cli install tessl/pypi-fire@0.7.0

index.mddocs/

Python Fire

A 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.

Package Information

  • Package Name: fire
  • Language: Python
  • Installation: pip install fire
  • Repository: https://github.com/google/python-fire
  • License: Apache-2.0

Core Imports

import fire

For 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 tracing

Basic Usage

import 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
    # })

Architecture

Python Fire uses a component traversal system that recursively processes command-line arguments:

  • Component: The current Python object being processed (function, class, module, etc.)
  • Trace: Execution history tracking each step of component navigation
  • Parser: Argument parsing and value conversion system
  • Formatter: Output formatting and help text generation

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.

Capabilities

Core CLI Generation

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
    """

Core CLI Generation

Argument Parsing Customization

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."""

Argument Parsing

Shell Completion

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."""

Shell Completion

Interactive Mode

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."""

Interactive Mode

Help and Documentation

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."""

Help and Documentation

Types

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
    """