|
1 | 1 | import os |
2 | 2 | import subprocess |
| 3 | +from collections import defaultdict |
3 | 4 |
|
4 | 5 | import click |
5 | 6 | from trogon import tui |
6 | 7 | from dotenv import load_dotenv, find_dotenv |
7 | | - |
8 | 8 | import afterpython as ap |
9 | 9 | from afterpython import __version__ |
10 | 10 | from afterpython.cli.commands.init import init |
|
26 | 26 | from afterpython.cli.commands.init_branch_rules import init_branch_rules |
27 | 27 |
|
28 | 28 |
|
| 29 | +class AliasGroup(click.Group): |
| 30 | + """Custom group that displays command aliases together.""" |
| 31 | + |
| 32 | + def __init__(self, *args, **kwargs): |
| 33 | + super().__init__(*args, **kwargs) |
| 34 | + self._aliases = defaultdict(list) # Maps primary command -> list of aliases |
| 35 | + self._alias_to_primary = {} # Maps alias -> primary command name |
| 36 | + |
| 37 | + def add_command(self, cmd, name=None): |
| 38 | + """Track aliases when the same command is added with different names.""" |
| 39 | + name = name or cmd.name |
| 40 | + |
| 41 | + # Check if this command object was already added under a different name |
| 42 | + for existing_name, existing_cmd in self.commands.items(): |
| 43 | + if existing_cmd is cmd and existing_name != name: |
| 44 | + # This is an alias |
| 45 | + self._aliases[existing_name].append(name) |
| 46 | + self._alias_to_primary[name] = existing_name |
| 47 | + break |
| 48 | + |
| 49 | + super().add_command(cmd, name) |
| 50 | + |
| 51 | + def format_commands(self, ctx, formatter): |
| 52 | + """Format commands with aliases shown together.""" |
| 53 | + commands = [] |
| 54 | + for subcommand in self.list_commands(ctx): |
| 55 | + # Skip aliases - they'll be shown with their primary command |
| 56 | + if subcommand in self._alias_to_primary: |
| 57 | + continue |
| 58 | + |
| 59 | + cmd = self.get_command(ctx, subcommand) |
| 60 | + if cmd is None: |
| 61 | + continue |
| 62 | + |
| 63 | + # Skip hidden commands |
| 64 | + if cmd.hidden: |
| 65 | + continue |
| 66 | + |
| 67 | + help_text = cmd.get_short_help_str(limit=formatter.width) |
| 68 | + |
| 69 | + # Build command name with aliases |
| 70 | + aliases = self._aliases.get(subcommand, []) |
| 71 | + if aliases: |
| 72 | + subcommand = f"{subcommand}, {', '.join(aliases)}" |
| 73 | + |
| 74 | + commands.append((subcommand, help_text)) |
| 75 | + |
| 76 | + if commands: |
| 77 | + with formatter.section("Commands"): |
| 78 | + formatter.write_dl(commands) |
| 79 | + |
| 80 | + |
29 | 81 | @tui(command="tui", help="Open terminal UI") |
30 | | -@click.group(context_settings={"help_option_names": ["-h", "--help"]}) |
| 82 | +@click.group(cls=AliasGroup, context_settings={"help_option_names": ["-h", "--help"]}) |
31 | 83 | @click.pass_context |
32 | 84 | @click.version_option(version=__version__) |
33 | 85 | def afterpython_group(ctx): |
@@ -65,7 +117,6 @@ def afterpython_group(ctx): |
65 | 117 | afterpython_group.add_command(clean) |
66 | 118 | afterpython_group.add_command(pre_commit) |
67 | 119 | afterpython_group.add_command(pre_commit, name="pc") |
68 | | -afterpython_group.add_command(pre_commit, name="precommit") |
69 | 120 | afterpython_group.add_command(commitizen) |
70 | 121 | afterpython_group.add_command(commitizen, name="cz") |
71 | 122 | afterpython_group.add_command(commit) |
|
0 commit comments