Skip to content

Commit

Permalink
Changes bump-my-version into subcommands
Browse files Browse the repository at this point in the history
- Is backwards-compatible with previous versions
- `bump-my-version` forwards command to `bump-my-version bump` subcommand
- Only problem is that Click will not show help automatically, must provide `--help`
  • Loading branch information
coordt committed Jun 21, 2023
1 parent 8960d24 commit 31ffbcf
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 18 deletions.
11 changes: 8 additions & 3 deletions bumpversion/aliases.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -13,20 +13,25 @@ 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))}")

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
35 changes: 20 additions & 15 deletions bumpversion/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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],
Expand Down

0 comments on commit 31ffbcf

Please sign in to comment.