Skip to main content

A library that allows you to add aliases to your Click group and commands.

Project description

Click With Aliasing

top language code size last commit issues contributors PyPI License Downloads Monthly Downloads

A powerful extension for Click that adds command and group aliasing support with automatic async function handling.

Features

  • Command Aliases: Create multiple names for your commands
  • Group Aliases: Add aliases to command groups
  • Automatic Async Support: Seamlessly handle async functions without extra configuration
  • Drop-in Replacement: Works exactly like standard Click decorators
  • Type Safe: Full type hints support with proper IDE integration
  • Help Integration: Aliases automatically appear in help text

Installation

pip install click-with-aliasing

Requirements: Python 3.10 or newer

Quick Start

Basic Command with Aliases

from click_with_aliasing import command

@command(name="deploy", aliases=["d", "dep"])
def deploy():
    """Deploy the application"""
    print("Deploying application...")

Now you can run any of these:

my-cli deploy
my-cli d
my-cli dep

Group with Aliases

from click_with_aliasing import group, command

@group(name="database", aliases=["db"])
def database():
    """Database management commands"""
    pass

@command(name="migrate", aliases=["m"])
def migrate():
    """Run database migrations"""
    print("Running migrations...")

database.add_command(migrate)

Usage:

my-cli database migrate  # Full names
my-cli db m              # Using aliases
my-cli database m        # Mixed usage

Async Support

The library automatically detects and handles async functions, meaning no extra configuration is needed.

Async Commands

import asyncio
from click_with_aliasing import command

@command(name="fetch", aliases=["f"])
async def fetch():
    """Fetch data asynchronously"""
    await asyncio.sleep(1)
    print("Data fetched!")

Async Groups

import asyncio
from click_with_aliasing import group, command

@group(name="api", aliases=["a"])
async def api_group():
    """API management commands"""
    await asyncio.sleep(0.1)  # Simulate async setup

@command(name="start", aliases=["s"])
async def start_server():
    """Start the API server"""
    print("Starting server...")

api_group.add_command(start_server)

Complete Example

Here's a full CLI application demonstrating all features:

import asyncio
import click
from click_with_aliasing import group, command

@group(name="myapp", aliases=["app"])
def cli():
    """My Application CLI"""
    pass

@group(name="database", aliases=["db"])
async def database():
    """Database management commands"""
    await asyncio.sleep(0.1)  # Simulate async setup

@command(name="migrate", aliases=["m", "mig"])
async def migrate():
    """Run database migrations"""
    print("Running migrations...")
    await asyncio.sleep(1)
    print("Migrations completed!")

@command(name="seed", aliases=["s"])
def seed():
    """Seed the database"""
    print("Seeding database...")

@command(name="start", aliases=["run", "serve"])
def start():
    """Start the application server"""
    print("Starting server on port 8000...")

@command(name="stop", aliases=["kill"])
def stop():
    """Stop the application server"""
    print("Stopping server...")

database.add_command(migrate)
database.add_command(seed)
cli.add_command(database)
cli.add_command(start)
cli.add_command(stop)

if __name__ == "__main__":
    cli()

Usage examples:

python myapp.py database migrate
python myapp.py db m
python myapp.py database mig

python myapp.py start
python myapp.py stop

python myapp.py --help
python myapp.py db --help

API Reference

@command(name, *, aliases=None, **kwargs)

Creates a command with optional aliases.

Parameters:

  • name (str): Primary command name
  • aliases (List[str], optional): List of alternative names
  • **kwargs: Additional arguments passed to click.command()

Returns: AliasedCommand instance

@group(name=None, *, aliases=None, **kwargs)

Creates a command group with optional aliases.

Parameters:

  • name (str, optional): Group name (defaults to function name)
  • aliases (List[str], optional): List of alternative names
  • **kwargs: Additional arguments passed to click.group()

Returns: AliasedGroup instance

Migration from Click

Migrating from standard Click is straightforward:

Before (Standard Click)

import click

@click.group()
def cli():
    pass

@click.command()
def deploy():
    pass

After (Click with Aliasing)

from click_with_aliasing import group, command

@group(aliases=["c"])
def cli():
    pass

@command(name="deploy", aliases=["d", "dep"])
def deploy():
    pass

Help Text Integration

Aliases automatically appear in help text:

myapp database --help
Usage: myapp database [OPTIONS] COMMAND [ARGS]...

  Database management commands

Options:
  --help  Show this message and exit.

Commands:
  migrate (m, mig)  Run database migrations
  seed (s)          Seed the database

Troubleshooting

Common Issues

Q: My async function isn't working A: The library automatically wraps async functions. Make sure you're using Python 3.10+ and have proper async/await syntax.

Q: Aliases don't appear in help A: Ensure you're using the AliasedGroup class (automatic when using the @group decorator).

Q: Type hints are not working A: Make sure you're importing from click_with_aliasing and have the latest version installed.

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.

Acknowledgments

Built on top of the great Click library by the Pallets team.

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

click_with_aliasing-1.1.0.tar.gz (8.4 kB view details)

Uploaded Source

Built Distribution

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

click_with_aliasing-1.1.0-py3-none-any.whl (8.0 kB view details)

Uploaded Python 3

File details

Details for the file click_with_aliasing-1.1.0.tar.gz.

File metadata

  • Download URL: click_with_aliasing-1.1.0.tar.gz
  • Upload date:
  • Size: 8.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for click_with_aliasing-1.1.0.tar.gz
Algorithm Hash digest
SHA256 f963d357b5f8e6e209ccd2d0e65410e61c7931f0846f83d10c9f9993fd57104a
MD5 f73f0ef50958671d58be9984a1c8a50f
BLAKE2b-256 4d019650a5a1687d75e0d18297ace5971bf9614253aa2045adaee7c8dd5a4f3d

See more details on using hashes here.

File details

Details for the file click_with_aliasing-1.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for click_with_aliasing-1.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 ba7b1efe1b2b331ff612c632774fe3682680d77335d94d21312d1de067cb6a55
MD5 806ab699f398e77cf7d2e95c2de1a244
BLAKE2b-256 e9ccb0a0f0e1dc57dc2eb4434f79a137c943fd4d762fa87218ccb053138c22fc

See more details on using hashes here.

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