Skip to main content

Slash commands and autocompletions

Project description

Slashed

PyPI License Package status Daily downloads Weekly downloads Monthly downloads Distribution format Wheel availability Python version Implementation Releases Github Contributors Github Discussions 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 week Github commits this month Github commits this year Package status Code style: black 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
  • Built-in completers for:
    • File paths
    • Environment variables
    • Choice lists
    • Keyword arguments
    • Multi-value inputs
  • Extensible completion provider system
  • Type-safe with comprehensive type hints
  • Modern Python features (3.12+)
  • Built-in help system

Installation

pip install slashed

Quick Example

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

# Create a command store
store = CommandStore()

# Define a command with completion support
async def greet(ctx: CommandContext, args: list[str], kwargs: dict[str, str]) -> None:
    name = args[0] if args else "World"
    greeting = kwargs.get("greeting", "Hello")
    await ctx.output.print(f"{greeting}, {name}!")

greet_cmd = Command(
    name="greet",
    description="Greet someone",
    execute_func=greet,
    usage="[name] --greeting <greeting>",
    help_text="Greet someone with a custom greeting.\n\nExample: /greet Phil --greeting Hi",
    category="demo",
    completer=ChoiceCompleter({
        "World": "Default greeting target",
        "Everyone": "Greet all users",
        "Team": "Greet the team"
    })
)

# Register the command
store.register_command(greet_cmd)

# Register built-in commands (help, etc)
store.register_builtin_commands()

# Create context and execute a command
ctx = store.create_context(data=None)
await store.execute_command("greet Phil --greeting Hi", ctx)

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],
    args: list[str],
    kwargs: dict[str, str],
) -> None:
    if not ctx.data.is_admin:
        await ctx.output.print("Sorry, admin access required!")
        return
    await ctx.output.print(f"Welcome admin {ctx.data.user_name}!")


# Create and register the command
admin_command = Command(
    name="admin",
    description="Admin-only command",
    execute_func=admin_cmd,
    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
await store.execute_command("admin", ctx)

Documentation

For full documentation including advanced usage and API reference, visit slashed.readthedocs.io.

Contributing

Contributions are welcome! Please feel free to submit a Pull Request. Make sure to read our contributing guidelines first.

License

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

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

slashed-0.2.0.tar.gz (19.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

slashed-0.2.0-py3-none-any.whl (16.6 kB view details)

Uploaded Python 3

File details

Details for the file slashed-0.2.0.tar.gz.

File metadata

  • Download URL: slashed-0.2.0.tar.gz
  • Upload date:
  • Size: 19.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.0.1 CPython/3.12.8

File hashes

Hashes for slashed-0.2.0.tar.gz
Algorithm Hash digest
SHA256 61af03331e1f34d9e048e8dc047de0d2c7990e0d25401abb17ca5635067e28bb
MD5 816515c76d3bba373f49db44d62c4627
BLAKE2b-256 2547d7aea5df36c758f7c22f4bab70bf455b6a7e7e588faa021934ff99f44a73

See more details on using hashes here.

Provenance

The following attestation bundles were made for slashed-0.2.0.tar.gz:

Publisher: build.yml on phil65/slashed

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file slashed-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: slashed-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 16.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.0.1 CPython/3.12.8

File hashes

Hashes for slashed-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 741bbbb2d65d5fd9ef88635bb1b3ebc5b7f509fecd5aa63737cc0bd46ec2fa04
MD5 dc7dde5e1d0a7a9a51b85a64a90d8d5e
BLAKE2b-256 45d3bb112c143ccbba9f2295af5373fa988c937bca3382c5ef544a726a29036c

See more details on using hashes here.

Provenance

The following attestation bundles were made for slashed-0.2.0-py3-none-any.whl:

Publisher: build.yml on phil65/slashed

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page