CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/pypi-fire

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

Pending
Overview
Eval results
Files

argument-parsing.mddocs/

Argument Parsing Customization

Fire provides decorators and utilities for customizing how command-line arguments are parsed and converted, allowing fine-grained control over argument processing for specific parameters or functions.

Capabilities

Custom Parse Function Decorator

Decorator to set a custom parsing function for specific arguments or as the default parser for a function.

def SetParseFn(fn, *arguments):
    """
    Set custom parsing function for specific arguments.
    
    Parameters:
    - fn: Function to use for parsing arguments  
    - *arguments: Argument names to apply custom parser to (if none, sets default parser)
    
    Returns:
    Decorated function with Fire metadata for custom parsing
    """

Multiple Parse Functions Decorator

Decorator to set multiple custom parsing functions for different arguments in a single declaration.

def SetParseFns(*positional, **named):
    """
    Set multiple custom parsing functions.
    
    Parameters:
    - *positional: Functions for positional arguments  
    - **named: Mapping of argument names to parsing functions
    
    Returns:
    Decorated function with Fire metadata for custom parsing
    """

Metadata Access

Utilities for accessing Fire metadata attached to functions by the parsing decorators.

def GetMetadata(fn):
    """
    Get Fire metadata from a function.
    
    Parameters:
    - fn: Function to get metadata from
    
    Returns:
    Dictionary containing Fire metadata
    """

def GetParseFns(fn):
    """
    Get custom parse functions from a function.
    
    Parameters:
    - fn: Function to get parse functions from
    
    Returns:
    Dictionary containing parse functions configuration
    """

Usage Examples

Custom argument parser:

import fire
from fire.decorators import SetParseFn

def json_parser(value):
    import json
    return json.loads(value)

@SetParseFn(json_parser, 'config')
def process_data(data, config):
    # config will be parsed as JSON
    print(f"Processing {data} with config: {config}")
    return "Done"

if __name__ == '__main__':
    fire.Fire(process_data)
    # Command line: python script.py mydata '{"option": "value"}'

Multiple custom parsers:

import fire
from fire.decorators import SetParseFns

def parse_list(value):
    return value.split(',')

def parse_float(value):
    return float(value)

@SetParseFns(items=parse_list, threshold=parse_float)
def analyze(items, threshold):
    filtered = [item for item in items if len(item) > threshold]
    return filtered

if __name__ == '__main__':
    fire.Fire(analyze)
    # Command line: python script.py --items=apple,banana,cherry --threshold=5.5

Default parser for all arguments:

import fire
from fire.decorators import SetParseFn

def always_upper(value):
    return str(value).upper()

@SetParseFn(always_upper)  # No argument names = default for all
def shout(message, prefix="ALERT"):
    return f"{prefix}: {message}"

if __name__ == '__main__':
    fire.Fire(shout)
    # Command line: python script.py hello --prefix=warning
    # Output: WARNING: HELLO

Parser Function Requirements

Custom parser functions should:

  1. Accept a single string argument - the raw command-line value
  2. Return the parsed value - the converted Python object
  3. Handle errors gracefully - raise appropriate exceptions for invalid input
  4. Be deterministic - same input should produce same output

Example parser structure:

def custom_parser(value):
    try:
        # Parse and convert the string value
        result = some_conversion(value)
        return result
    except (ValueError, TypeError) as e:
        raise ValueError(f"Invalid value '{value}': {e}")

Metadata Constants

FIRE_METADATA = 'FIRE_METADATA'
FIRE_PARSE_FNS = 'FIRE_PARSE_FNS'
ACCEPTS_POSITIONAL_ARGS = 'ACCEPTS_POSITIONAL_ARGS'

These constants are used internally by Fire to store and retrieve metadata from decorated functions.

Install with Tessl CLI

npx tessl i tessl/pypi-fire

docs

argument-parsing.md

core-cli.md

help-documentation.md

index.md

interactive-mode.md

shell-completion.md

tile.json