diff --git a/bumpversion/aliases.py b/bumpversion/aliases.py index c3deaada..40f17874 100644 --- a/bumpversion/aliases.py +++ b/bumpversion/aliases.py @@ -1,5 +1,5 @@ """Utilities for handling command aliases.""" -from typing import List +from typing import List, Optional import rich_click as click from click import Context @@ -13,14 +13,16 @@ class AliasedGroup(RichGroup): If there were a command called ``push``, it would accept ``pus`` as an alias (so long as it was unique) """ - def get_command(self, ctx: Context, cmd_name: str) -> None: + def get_command(self, ctx: Context, cmd_name: str) -> Optional[click.Command]: """Given a context and a command name, this returns a Command object if it exists or returns None.""" rv = click.Group.get_command(self, ctx, cmd_name) if rv is not None: return rv matches = [x for x in self.list_commands(ctx) if x.startswith(cmd_name)] if not matches: - return None + args = [cmd_name, *ctx.args] + new_ctx = self.make_context(ctx.info_name, args, parent=ctx) + return click.Group.get_command(self, new_ctx, "bump") elif len(matches) == 1: return click.Group.get_command(self, ctx, matches[0]) ctx.fail(f"Too many matches: {', '.join(sorted(matches))}") @@ -28,5 +30,8 @@ def get_command(self, ctx: Context, cmd_name: str) -> None: def resolve_command(self, ctx: Context, args: List[str]) -> tuple: """Find the command and make sure the full command name is returned.""" # always return the full command name + original_args = args[:] _, cmd, args = super().resolve_command(ctx, args) + if cmd.name == "bump" and args != original_args: + return cmd.name, cmd, original_args return cmd.name, cmd, args diff --git a/bumpversion/cli.py b/bumpversion/cli.py index fd218d07..6bcec632 100644 --- a/bumpversion/cli.py +++ b/bumpversion/cli.py @@ -3,30 +3,35 @@ from typing import List, Optional import rich_click as click +from click.core import Context -# from click.core import Context from bumpversion import __version__ - -# from bumpversion.aliases import AliasedGroup -from bumpversion.bump import do_bump, get_next_version -from bumpversion.config import Config, find_config_file, get_configuration +from bumpversion.aliases import AliasedGroup +from bumpversion.bump import do_bump +from bumpversion.config import find_config_file, get_configuration from bumpversion.logging import setup_logging from bumpversion.utils import get_context, get_overrides logger = logging.getLogger(__name__) -# @click.group(cls=AliasedGroup) -# @click.version_option(version=__version__) -# @click.pass_context -# def cli(ctx: Context) -> None: -# """Version bump your Python project.""" -# if ctx.invoked_subcommand is None: -# ctx.invoke(bump) +@click.group( + cls=AliasedGroup, + invoke_without_command=True, + context_settings={ + "ignore_unknown_options": True, + "allow_interspersed_args": True, + }, +) +@click.version_option(version=__version__) +@click.pass_context +def cli(ctx: Context) -> None: + """Version bump your Python project.""" + if ctx.invoked_subcommand is None: + ctx.invoke(bump, *ctx.args) -@click.command(context_settings={"ignore_unknown_options": True}) -@click.version_option(version=__version__) +@cli.command(context_settings={"ignore_unknown_options": True}) @click.argument("args", nargs=-1, type=str) @click.option( "--config-file", @@ -163,7 +168,7 @@ is_flag=True, help="List machine readable information", ) -def cli( +def bump( # version_part: str, args: list, config_file: Optional[str],