Cleo allows you to create beautiful and testable command-line interfaces.
—
Quality
Pending
Does it follow best practices?
Impact
Pending
No eval scenarios have been run
The Application class serves as the main container and coordinator for CLI commands. It handles command registration, routing, argument parsing, execution flow, and provides the foundation for building complete CLI applications.
Create and configure the main application container that will manage your CLI commands.
class Application:
def __init__(self, name: str = "console", version: str = "") -> None:
"""
Create a new CLI application.
Args:
name (str): Application name, defaults to "console"
version (str): Application version string, defaults to empty
"""Register commands with the application and manage the command registry.
def add(self, command: Command) -> Command | None:
"""
Add a command to the application.
Args:
command (Command): The command instance to add
Returns:
Command | None: The added command or None if already exists
"""
def get(self, name: str) -> Command:
"""
Get a registered command by name.
Args:
name (str): Command name to retrieve
Returns:
Command: The command instance
Raises:
CleoCommandNotFoundError: If command not found
"""
def has(self, name: str) -> bool:
"""
Check if a command is registered.
Args:
name (str): Command name to check
Returns:
bool: True if command exists
"""
def find(self, name: str) -> Command:
"""
Find a command by name or abbreviation.
Args:
name (str): Command name or abbreviation
Returns:
Command: The matching command
Raises:
CleoCommandNotFoundError: If no matching command found
"""
def all(self, namespace: str | None = None) -> dict[str, Command]:
"""
Get all registered commands, optionally filtered by namespace.
Args:
namespace (str | None): Optional namespace to filter by
Returns:
dict[str, Command]: Dictionary of command name to Command instances
"""Execute the application with input arguments and manage the execution lifecycle.
def run(self, input: Input | None = None, output: Output | None = None, error_output: Output | None = None) -> int:
"""
Run the application with provided input and output streams.
Args:
input (Input | None): Input stream, defaults to ArgvInput
output (Output | None): Output stream, defaults to StreamOutput
error_output (Output | None): Error output stream, defaults to StreamOutput
Returns:
int: Exit code (0 for success, non-zero for errors)
"""
def create_io(self, input: Input | None = None, output: Output | None = None, error_output: Output | None = None) -> IO:
"""
Create an IO instance for the application.
Args:
input (Input | None): Input stream
output (Output | None): Output stream
error_output (Output | None): Error output stream
Returns:
IO: Configured IO instance
"""Access and configure application properties and metadata.
@property
def name(self) -> str:
"""Get the application name."""
def set_name(self, name: str) -> None:
"""Set the application name."""
@property
def version(self) -> str:
"""Get the application version."""
def set_version(self, version: str) -> None:
"""Set the application version."""
@property
def display_name(self) -> str:
"""Get the application display name (name + version)."""
@property
def long_version(self) -> str:
"""Get the long version string with name and version."""
@property
def help(self) -> str:
"""Get the application help text."""
def set_help(self, help: str) -> None:
"""Set the application help text."""
@property
def definition(self) -> Definition:
"""Get the global input definition."""
@property
def default_commands(self) -> list[Command]:
"""Get the default commands (help, list, completions)."""
@property
def ui(self) -> UI:
"""Get the UI component manager."""
@property
def event_dispatcher(self) -> EventDispatcher | None:
"""Get the event dispatcher if configured."""
def set_event_dispatcher(self, dispatcher: EventDispatcher) -> None:
"""Set the event dispatcher for extensibility."""from cleo.application import Application
from cleo.commands.command import Command
class MyCommand(Command):
name = "hello"
description = "Say hello"
def handle(self):
self.line("Hello, World!")
# Create and configure application
app = Application("MyApp", "1.0.0")
app.add(MyCommand())
# Run the application
if __name__ == "__main__":
app.run()app = Application("toolkit", "2.1.0")
# Add multiple commands
app.add(ProcessCommand())
app.add(AnalyzeCommand())
app.add(ReportCommand())
# Check if command exists
if app.has("process"):
command = app.get("process")
# Get all commands
all_commands = app.all()
data_commands = app.all("data") # Commands in 'data' namespaceapp = Application()
app.set_name("custom-tool")
app.set_version("3.2.1")
app.set_help("Custom CLI tool for data processing")
# Set up event handling for extensibility
from cleo.events.event_dispatcher import EventDispatcher
dispatcher = EventDispatcher()
app.set_event_dispatcher(dispatcher)Install with Tessl CLI
npx tessl i tessl/pypi-cleo