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 and advanced parameter validation.

You can find the project on PyPi.

Features

  • Command Aliases: Create multiple names for your commands
  • Group Aliases: Add aliases to command groups
  • Help Alias (-h): Automatic -h shorthand for --help with conflict detection
  • Enhanced Options & Arguments: Mutual exclusivity, requirements, and group constraints
  • Validation Rules: Group-level validation with multiple modes (all_or_none, at_least, at_most, exactly)
  • 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

Documentation

  • Command - Command decorator with aliasing support
  • Group - Group decorator for organizing commands
  • Option - Enhanced options with mutual exclusivity and requirements
  • Argument - Enhanced arguments with validation constraints
  • Rule - Group-level validation rules for complex parameter logic

Note: Both -h and --help flags are supported by default. See the Command and Group documentation for details on how -h is intelligently handled when commands use it for other purposes.

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

Enhanced Options with Validation

from click_with_aliasing import command, option

@command(name="auth")
@option("--username", requires=["password"], mutually_exclusive=["token"])
@option("--password", requires=["username"], mutually_exclusive=["token"])
@option("--token", mutually_exclusive=["username", "password"])
def auth(username, password, token):
    """Authenticate with username/password or token"""
    if token:
        print("Authenticating with token")
    elif username and password:
        print(f"Authenticating as {username}")

Usage:

my-cli auth --token abc123                          # Valid
my-cli auth --username admin --password secret      # Valid
my-cli auth --username admin                        # Error: requires password
my-cli auth --token abc123 --username admin         # Error: mutually exclusive

Validation Rules

from click_with_aliasing import command, option, rule

@command(name="deploy")
@option("--production", is_flag=True)
@option("--staging", is_flag=True)
@option("--development", is_flag=True)
@rule(["production", "staging", "development"], mode="exactly", count=1)
def deploy(production, staging, development):
    """Deploy to exactly one environment"""
    env = "production" if production else "staging" if staging else "development"
    print(f"Deploying to {env}")

Usage:

my-cli deploy --production                          # Valid
my-cli deploy --staging                             # Valid
my-cli deploy --production --staging                # Error: exactly 1 required
my-cli deploy                                       # Error: exactly 1 required

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)

Key Concepts

Command & Group Aliases

Add multiple names to commands and groups for convenience:

@command(name="deploy", aliases=["d", "dep"])
@group(name="database", aliases=["db", "d"])

Option Validation

Mutual Exclusivity: Only one option from a set can be used

@option("--json", mutually_exclusive=["xml", "yaml"])

Requirements: Options that must be used together

@option("--username", requires=["password"])

Group Constraints: Organize options and apply constraints collectively

@option("--json", group="format", group_mutually_exclusive=["output"])

Validation Rules

Apply group-level validation with different modes:

  • all_or_none: All parameters or none
  • at_least: Minimum number required
  • at_most: Maximum number allowed
  • exactly: Exact number required
@rule(["host", "port", "database"], mode="all_or_none")
@rule(["email", "sms", "slack"], mode="at_least", count=1)
@rule(["json", "xml", "yaml"], mode="at_most", count=1)
@rule(["file1", "file2", "file3"], mode="exactly", count=2)

Migration from Click

Migrating from standard Click is straightforward - just change your imports:

Before (Standard Click)

import click

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

@click.command()
@click.option("--name", default="World")
@click.argument("file")
def greet(name, file):
    pass

After (Click with Aliasing)

from click_with_aliasing import group, command, option, argument

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

@command(name="greet", aliases=["g"])
@option("--name", default="World", mutually_exclusive=["file"])
@argument("file", required=False, mutually_exclusive=["name"])
def greet(name, file):
    pass

All standard Click features work exactly the same, with optional enhancements available.

Help Text Integration

Aliases automatically appear in help text, and both -h and --help work for displaying help:

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

  Database management commands

Options:
  -h, --help  Show this message and exit.

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

The -h flag is automatically added unless a command uses it for another purpose (like --host), ensuring consistent help access while avoiding conflicts.

Contributing

Contributions are welcome! Please read our Contributing Guide for details on our development process, coding standards, and how to submit pull requests.

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.2.1.tar.gz (23.2 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.2.1-py3-none-any.whl (17.8 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for click_with_aliasing-1.2.1.tar.gz
Algorithm Hash digest
SHA256 2e07833b86da822a23e859946665541681a9c45aa67caddbb785554868296739
MD5 0133e57685d160a4c40b70e89264ef03
BLAKE2b-256 d1509db5c8bc2e6df060c764b29521480e6050ec23744ccace43e43718ef0cc2

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for click_with_aliasing-1.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 1543b1e01723eb5ec1f2aa0328970aa2b420b224e3c8fdf98b6b7400e5a843c5
MD5 0835e245542603ef393d9b16c01ef459
BLAKE2b-256 f11f39b4d731d67d721433e58160e0bea277afe1863d7b72fc70972f5589d0a9

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