Skip to main content

Slashed

PyPI License Package status Monthly downloads Distribution format Wheel availability Python version Implementation Releases Github Contributors Github Forks Github Issues Github Issues Github Watchers Github Stars Github Repository size Github last commit Github release date Github language count Github commits this month Package status PyUp

Read the documentation!

A Python library for implementing slash commands with rich autocompletion support.

Features

  • Simple command registration system
  • Rich autocompletion support with multiple providers
  • Type-safe command and context handling:
    • Generic typing for context data
    • Type-checked command parameters
    • Safe data access patterns
  • Built-in completers for:
    • File paths
    • Choice lists
    • Keyword arguments
    • Multi-value inputs
    • Callback based lists
    • Environment variables
  • Extensible completion provider system
  • Shell-style chaining of commands (&&, ||, ;, pipes)
  • Modern Python features (requires Python 3.13+)
  • UI framework integration:
    • Textual support
    • prompt_toolkit support
  • Built-in help system

Slashed could be compared to cmd2, both providing interactive command systems with completion and history support, but Slashed offers a modern async-first design with rich (generic) type hints, improved autocompletion, and flexible UI framework integration for both terminal (prompt-toolkit) and TUI (Textual) applications. Unlike cmd2's tight coupling to its own REPL, Slashed is framework-agnostic and provides multiple ways to define commands, making it more adaptable to different application needs while maintaining a clean, type-safe API.

Installation

pip install slashed
# optional integrations
pip install "slashed[textual]"         # Textual widgets and SlashedApp
pip install "slashed[prompt-toolkit]"  # prompt_toolkit completer
pip install "slashed[system_commands]" # /exec, /run, /processes, ... (psutil)

Quick Example

import asyncio
from dataclasses import dataclass

from slashed import SlashedCommand, CommandStore, CommandContext
from slashed.completers import ChoiceCompleter


# Define app state that will be available to commands
@dataclass
class AppState:
    greeting_count: int = 0


# Define a command with explicit parameters and typed context
class GreetCommand(SlashedCommand):
    """Greet someone with a custom greeting."""

    name = "greet"
    category = "demo"

    async def execute_command(
        self,
        ctx: CommandContext[AppState],
        name: str = "World",
        greeting: str = "Hello",
    ):
        """Greet someone.

        Args:
            ctx: Command context
            name: Who to greet
            greeting: Custom greeting to use
        """
        state = ctx.get_data()  # Type-safe access to app state
        state.greeting_count += 1
        await ctx.print(f"{greeting}, {name}! (Greeted {state.greeting_count} times)")

    def get_completer(self) -> ChoiceCompleter:
        """Provide name suggestions."""
        return ChoiceCompleter({
            "World": "Default greeting target",
            "Everyone": "Greet all users",
            "Team": "Greet the team",
        })


async def main():
    # Create store and register the command
    store = CommandStore()
    store.register_command(GreetCommand)

    # Create context with app state
    ctx = store.create_context(data=AppState())

    # Execute a command (no leading slash)
    await store.execute_command("greet Phil --greeting Hi", ctx)  # Hi, Phil! (Greeted 1 times)


asyncio.run(main())

Command Definition Styles

Slashed offers two different styles for defining commands, each with its own advantages:

Command Style (using Command class)

from slashed import Command, CommandContext


def add_worker(ctx: CommandContext, worker_id: str, host: str = "localhost", port: int = 8080):
    """Add a worker to the pool."""
    print(f"Adding worker {worker_id} at {host}:{port}")


# Usage string is auto-generated from function signature
cmd = Command(
    add_worker,
    name="add-worker",
    description="Add a worker to the pool",
    category="workers",
)
# cmd.usage will be: "<worker_id> [--host <value>] [--port <value>]"

Advantages:

  • Type-safe argument parsing with automatic validation
  • Auto-generated usage strings from function signatures
  • Works with any callable (functions, methods, lambdas)
  • Smart context injection (only passed if function expects it)
  • Better error messages for missing/invalid arguments
  • Quick to create without inheritance
  • All configuration in one place
  • Easier to create commands dynamically

Declarative Style (using SlashedCommand)

from slashed import SlashedCommand, CommandContext


class AddWorkerCommand(SlashedCommand):
    """Add a worker to the pool."""

    name = "add-worker"
    category = "workers"

    async def execute_command(
        self,
        ctx: CommandContext,
        worker_id: str,  # required parameter
        host: str = "localhost",  # optional with default
        port: int = 8080,  # optional with default
    ):
        """Add a new worker to the pool.

        Args:
            ctx: Command context
            worker_id: Unique worker identifier
            host: Worker hostname
            port: Worker port number
        """
        await ctx.print(f"Adding worker {worker_id} at {host}:{port}")

Advantages:

  • Type-safe parameter handling
  • Automatic usage generation from parameters
  • Help text generated from docstrings
  • Better IDE support with explicit parameters
  • More maintainable for complex commands
  • Validates required parameters automatically
  • Natural Python class structure
  • Parameters are self-documenting

When to Use Which?

Both styles now offer the same core capabilities (type-safe parsing, auto-generated usage, parameter validation). Choose based on your preference:

Use the Command style when:

  • Creating simple commands with functions or methods
  • Generating commands dynamically
  • Working with existing functions/methods
  • Prefer functional programming style
  • Want minimal boilerplate

Use the SlashedCommand style when:

  • Building complex commands that benefit from OOP structure
  • Want to group related functionality in classes
  • Need to override command behavior methods
  • Prefer declarative class-based definitions
  • Building a larger command hierarchy

Alternative Registration Methods

Using the Command Decorator

from importlib.util import find_spec

from slashed import CommandContext, CommandStore
from slashed.completers import PathCompleter

store = CommandStore()


@store.command(
    category="tools",
    usage="<pattern> [--type type]",
    completer=PathCompleter(files=True),
    condition=lambda: find_spec("sqlalchemy") is not None,
)
async def search(ctx: CommandContext, pattern: str, *, type: str = "any"):
    """Search for files in current directory."""
    await ctx.print(f"Searching for {pattern}")

Using add_command

# Direct function
store.add_command(
    "search",
    search_func,
    category="tools",
    completer=PathCompleter(files=True),
)

# Import path
store.add_command(
    "query",
    "myapp.commands.database.execute_query",
    category="database",
    condition=lambda: find_spec("sqlalchemy") is not None,
)

Using CommandRegistry

For cases where you need to define commands before initializing the store (e.g., in module-level code), you can use CommandRegistry to collect commands and register them later:

# commands.py
from importlib.util import find_spec

from slashed import CommandContext, CommandRegistry
from slashed.completers import PathCompleter

registry = CommandRegistry()


@registry.command(category="tools", completer=PathCompleter(files=True))
async def search(ctx: CommandContext, pattern: str):
    """Search for files in current directory."""
    await ctx.print(f"Searching for {pattern}")


@registry.command(category="tools", condition=lambda: find_spec("sqlalchemy") is not None)
async def query(ctx: CommandContext, sql: str):
    """Execute database query."""
    await ctx.print(f"Running query: {sql}")


# app.py
from slashed import CommandStore
from .commands import registry

store = CommandStore()
registry.register_to(store)  # Register all collected commands

Generic Context Example

from dataclasses import dataclass
from slashed import Command, CommandStore, CommandContext


# Define your custom context data
@dataclass
class AppContext:
    user_name: str
    is_admin: bool


# Command that uses the typed context
async def admin_cmd(ctx: CommandContext[AppContext], action: str = "status"):
    """Admin-only command."""
    state = ctx.get_data()  # Type-safe access to context data
    if not state.is_admin:
        await ctx.print("Sorry, admin access required!")
        return
    await ctx.print(f"Welcome admin {state.user_name}! Running {action}")


# Create and register the command
admin_command = Command(
    admin_cmd,
    name="admin",
    description="Admin-only command",
    category="admin",
)

# Setup the store with typed context
store = CommandStore()
store.register_command(admin_command)

# Create context with your custom data
ctx = store.create_context(data=AppContext(user_name="Alice", is_admin=True))

# Execute command with typed context (inside an async function)
await store.execute_command("admin restart", ctx)

Signal-Based Event System

Slashed uses Psygnal to provide a robust event system for monitoring command execution and output. This makes it easy to track command usage, handle errors, and integrate with UIs.

from slashed import CommandStore

store = CommandStore()


# Monitor command execution
@store.command_executed.connect
def on_command_executed(event):
    """Handle command execution results."""
    if event.success:
        print(f"Command '{event.command}' succeeded")
    else:
        print(f"Command '{event.command}' failed: {event.error}")


# Monitor command output
@store.output.connect
def on_output(message: str):
    """Handle command output."""
    print(f"Output: {message}")


# Monitor command registry changes
@store.command_events.added.connect
def on_command_added(name: str, command):
    print(f"New command registered: {name}")

Available Signals

  • command_executed: Emitted after command execution (success/failure)
  • output: Emitted for all command output
  • command_events: psygnal EventedDict signals for command registry changes (adding, added, removed, changed, ...)

The signal system provides a clean way to handle events without tight coupling, making it ideal for UI integration and logging.

UI Integration Examples

Slashed provides integrations for both prompt_toolkit and Textual:

Prompt Toolkit REPL

from prompt_toolkit import PromptSession
from slashed import CommandStore
from slashed.prompt_toolkit_completer import PromptToolkitCompleter


async def main():
    """Run a simple REPL with command completion."""
    # Initialize command store
    store = CommandStore()
    await store.initialize()

    # Create session with command completion
    completer = PromptToolkitCompleter(store=store)
    session = PromptSession(completer=completer, complete_while_typing=True)

    print("Type /help to list commands. Press Ctrl+D to exit.")

    while True:
        try:
            text = await session.prompt_async(">>> ")
            if text.startswith("/"):
                await store.execute_command_with_context(text[1:])
        except EOFError:  # Ctrl+D
            break


if __name__ == "__main__":
    import asyncio

    asyncio.run(main())

Type-Safe Context System

Slashed provides a powerful context system that automatically matches commands with their required context data based on type hints. This allows for type-safe access to application state while keeping commands decoupled from specific implementations.

Basic Usage

from dataclasses import dataclass
from slashed import SlashedCommand, CommandStore, CommandContext


# Define your contexts
@dataclass
class DatabaseContext:
    """Database connection context."""

    connection: str
    timeout: int = 30


@dataclass
class UIContext:
    """UI context."""

    theme: str = "dark"


# Commands specify their required context type
class QueryCommand(SlashedCommand):
    """Execute a database query."""

    name = "query"

    async def execute_command(
        self,
        ctx: CommandContext[DatabaseContext],  # Type hint determines required context
        query: str,
    ):
        db = ctx.get_data()  # Properly typed as DatabaseContext
        await ctx.print(f"Executing {query} with timeout {db.timeout}")


# Register contexts and commands
store = CommandStore()
store.register_context(DatabaseContext("mysql://localhost"))
store.register_context(UIContext("light"))

# Commands automatically get their matching context (inside an async function)
await store.execute_command_auto("query 'select * from users'")

Context matching reads the ctx annotation at runtime, so the context classes must be importable from the command's module (not defined inside a function).

Shell-Style Command Lines

execute_shell parses a command line with bashlex and runs the slash commands it contains, supporting &&, ||, ; and pipes:

ctx = store.create_context(data=AppState())
result = await store.execute_shell("greet Alice && greet Bob", ctx)
print(result.exit_code)

Textual App

from dataclasses import dataclass

from slashed import ChoiceCompleter, CommandContext, SlashedCommand
from slashed.textual_adapter import SlashedApp
from textual.app import ComposeResult
from textual.containers import Container, VerticalScroll
from textual.widgets import Input, Label


@dataclass
class AppState:
    """Application state available to commands."""

    user_name: str


class GreetCommand(SlashedCommand):
    """Greet someone."""

    name = "greet"
    category = "demo"

    async def execute_command(self, ctx: CommandContext[AppState], name: str = "World"):
        state = ctx.get_data()
        await ctx.print(f"Hello, {name}! (from {state.user_name})")

    def get_completer(self) -> ChoiceCompleter:
        return ChoiceCompleter({"World": "Everyone", "Team": "The Team"})


class DemoApp(SlashedApp[AppState, None]):
    """App with slash commands and completion."""

    def compose(self) -> ComposeResult:
        # Command input with completion
        suggester = self.get_suggester()
        yield Container(Input(id="command-input", suggester=suggester))
        # Output areas
        yield VerticalScroll(id="main-output")
        yield Label(id="status")

        # Connect outputs to widgets
        self.bind_output("main", "#main-output", default=True)
        self.bind_output("status", "#status")


if __name__ == "__main__":
    state = AppState(user_name="Admin")
    app = DemoApp(data=state, commands=[GreetCommand])
    app.run()

Both integrations support:

  • Command completion
  • Command history
  • Typed context data
  • Rich output formatting

Command Routing System

Slashed provides a flexible routing system that allows organizing commands into different contexts with explicit permissions:

from dataclasses import dataclass
from slashed import CommandRouter, CommandStore, DefaultOutputWriter


# Define contexts for different subsystems
@dataclass
class GlobalContext:
    """Global application context."""

    env: str = "production"


@dataclass
class DatabaseContext:
    """Database connection context."""

    connection: str
    timeout: int = 30


# Create store and router
store = CommandStore()
await store.initialize()  # registers builtin commands like help
output = DefaultOutputWriter()
db_context = DatabaseContext("mysql://localhost")
router = CommandRouter[GlobalContext, DatabaseContext](
    global_context=GlobalContext(),
    commands=store,
)

# Add route with restricted commands
router.add_route(
    "db",
    db_context,
    description="Database operations",
    allowed_commands={"query", "migrate"},  # Only allow specific commands
)

# Execute commands with proper routing
await router.execute("help", output)  # Uses global context
await router.execute("@db query 'SELECT 1'", output)  # Uses DB context

# Temporary context switching
with router.temporary_context(db_context):
    await router.execute("query 'SELECT 1'", output)  # No prefix needed

The routing system provides:

  • Route-specific command permissions
  • Automatic context switching
  • Command prefix completion (@db, @fs, etc.)
  • Type-safe context handling
  • Temporary context overrides
  • Clear separation of subsystems

This makes it easy to organize commands into logical groups while maintaining type safety and proper access control.

Documentation

For full documentation including advanced usage and API reference, visit phil65.github.io/slashed.

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

License

This project is licensed under the MIT License - see the LICENSE file for details.

Metadata

Release files for slashed 1.3.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for slashed 1.3.1
File Size Uploaded Provenance Actions
slashed-1.3.1.tar.gz 57.8 kB With provenance Details for slashed-1.3.1.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for slashed 1.3.1
File Interpreter ABI Platform Actions
slashed-1.3.1-py3-none-any.whl Python 3 none any Details for slashed-1.3.1-py3-none-any.whl

Total release size: 120.7 kB

Release files / slashed-1.3.1.tar.gz

Download URL slashed-1.3.1.tar.gz
Size 57.8 kB
Tags Source
SHA-256 checksum
How to use checksums
177f3c2afec35bfdb46f7b8c6716ea9327bb1ca6ed96e040836212c380dede47
BLAKE2b-256 checksum
How to use checksums
76cb4b955a80cd73cbfe3a8c63861b9b7fcdce750f5b1c6bdf550007cc7e7c00
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 26, 2026.

Transparency log

Release files / slashed-1.3.1-py3-none-any.whl

Download URL slashed-1.3.1-py3-none-any.whl
Size 62.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
30a91efa0b8464cfb5986cffc703ba775bb905d9c56e1c4804cf1f4590f014b4
BLAKE2b-256 checksum
How to use checksums
7d0a3469b03360e6756aeac3f4e6ef18f1c0a0f596274b868b61ca8d133bbb3a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 26, 2026.

Transparency log

Security for slashed 1.3.1

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

Full provenance

Both files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for slashed-1.3.1.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 26, 2026.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

1.3.1 This release

2 release files Full provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page