A library for automatically generating command line interfaces from any Python object.
—
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.
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
"""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
"""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
"""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.5Default 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: HELLOCustom parser functions should:
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}")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