A library for automatically generating command line interfaces from any Python object.
—
Fire provides functionality for generating shell completion scripts that enable tab completion for Fire-generated CLIs in Bash and Fish shells.
Generate shell completion scripts that provide tab completion for commands, subcommands, and options.
def Script(name, component, default_options=None, shell='bash'):
"""
Generate shell completion script for a Fire CLI.
Parameters:
- name: Name of the CLI command for completion registration
- component: Component to analyze for completion possibilities
- default_options: Dictionary of default options available for any command
- shell: Shell type, either 'bash' or 'fish'
Returns:
String containing the shell completion script to be sourced
"""Utilities for determining which members of a component should be visible in completion suggestions.
def MemberVisible(component, name, member, class_attrs=None, verbose=False):
"""
Check if a member should be visible in completion.
Parameters:
- component: The component containing the member
- name: Name of the member
- member: The member object
- class_attrs: Dictionary of class attributes if applicable
- verbose: Whether to include private/hidden members
Returns:
Boolean indicating if member should be visible
"""
def VisibleMembers(component, class_attrs=None, verbose=False):
"""
Get all visible members of a component for completion.
Parameters:
- component: Component to get members from
- class_attrs: Dictionary of class attributes if applicable
- verbose: Whether to include private/hidden members
Returns:
Dictionary of visible member names to member objects
"""Generate completion suggestions for a given component context.
def Completions(component, verbose=False):
"""
Get completion suggestions for a component.
Parameters:
- component: Component to get completions for
- verbose: Whether to include private/hidden members
Returns:
List of completion suggestions (strings)
"""Generate Bash completion script:
import fire
from fire import completion
class MyTool:
def process(self, file, output=None):
return f"Processing {file}"
def analyze(self, data, verbose=False):
return f"Analyzing {data}"
# Generate completion script
script = completion.Script('mytool', MyTool, shell='bash')
print(script)Generate Fish completion script:
import fire
from fire import completion
def deploy(environment, version, force=False):
return f"Deploying {version} to {environment}"
def status(service):
return f"Status of {service}"
commands = {
'deploy': deploy,
'status': status
}
# Generate Fish completion
script = completion.Script('deploy-tool', commands, shell='fish')
print(script)Using Fire's built-in completion flag:
# Any Fire CLI automatically supports completion generation
import fire
class Calculator:
def add(self, x, y):
return x + y
if __name__ == '__main__':
fire.Fire(Calculator)
# Command line: python calc.py -- --completion
# Outputs Bash completion script to stdout
# Command line: python calc.py -- --completion fish
# Outputs Fish completion script to stdoutFor Bash:
# Generate and save completion script
python your_cli.py -- --completion > /etc/bash_completion.d/your_cli
# Or for user-specific completion
python your_cli.py -- --completion > ~/.bash_completion.d/your_cli
source ~/.bash_completion.d/your_cliFor Fish:
# Generate and save completion script
python your_cli.py -- --completion fish > ~/.config/fish/completions/your_cli.fishProgrammatic setup:
import fire
from fire import completion
class MyApp:
def install_completion(self, shell='bash'):
"""Install shell completion for this CLI."""
script = completion.Script('myapp', MyApp, shell=shell)
if shell == 'bash':
with open('/etc/bash_completion.d/myapp', 'w') as f:
f.write(script)
elif shell == 'fish':
import os
os.makedirs(os.path.expanduser('~/.config/fish/completions'), exist_ok=True)
with open(os.path.expanduser('~/.config/fish/completions/myapp.fish'), 'w') as f:
f.write(script)
return f"Completion installed for {shell}"
if __name__ == '__main__':
fire.Fire(MyApp)The completion system analyzes the component structure to provide intelligent suggestions for:
--help, --verbose, etc.)Install with Tessl CLI
npx tessl i tessl/pypi-fire