Skip to content

Commit 31ffbcf

Browse files
committed
Changes bump-my-version into subcommands
- 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`
1 parent 8960d24 commit 31ffbcf

File tree

2 files changed

+28
-18
lines changed

2 files changed

+28
-18
lines changed

bumpversion/aliases.py

+8-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
"""Utilities for handling command aliases."""
2-
from typing import List
2+
from typing import List, Optional
33

44
import rich_click as click
55
from click import Context
@@ -13,20 +13,25 @@ class AliasedGroup(RichGroup):
1313
If there were a command called ``push``, it would accept ``pus`` as an alias (so long as it was unique)
1414
"""
1515

16-
def get_command(self, ctx: Context, cmd_name: str) -> None:
16+
def get_command(self, ctx: Context, cmd_name: str) -> Optional[click.Command]:
1717
"""Given a context and a command name, this returns a Command object if it exists or returns None."""
1818
rv = click.Group.get_command(self, ctx, cmd_name)
1919
if rv is not None:
2020
return rv
2121
matches = [x for x in self.list_commands(ctx) if x.startswith(cmd_name)]
2222
if not matches:
23-
return None
23+
args = [cmd_name, *ctx.args]
24+
new_ctx = self.make_context(ctx.info_name, args, parent=ctx)
25+
return click.Group.get_command(self, new_ctx, "bump")
2426
elif len(matches) == 1:
2527
return click.Group.get_command(self, ctx, matches[0])
2628
ctx.fail(f"Too many matches: {', '.join(sorted(matches))}")
2729

2830
def resolve_command(self, ctx: Context, args: List[str]) -> tuple:
2931
"""Find the command and make sure the full command name is returned."""
3032
# always return the full command name
33+
original_args = args[:]
3134
_, cmd, args = super().resolve_command(ctx, args)
35+
if cmd.name == "bump" and args != original_args:
36+
return cmd.name, cmd, original_args
3237
return cmd.name, cmd, args

bumpversion/cli.py

+20-15
Original file line numberDiff line numberDiff line change
@@ -3,30 +3,35 @@
33
from typing import List, Optional
44

55
import rich_click as click
6+
from click.core import Context
67

7-
# from click.core import Context
88
from bumpversion import __version__
9-
10-
# from bumpversion.aliases import AliasedGroup
11-
from bumpversion.bump import do_bump, get_next_version
12-
from bumpversion.config import Config, find_config_file, get_configuration
9+
from bumpversion.aliases import AliasedGroup
10+
from bumpversion.bump import do_bump
11+
from bumpversion.config import find_config_file, get_configuration
1312
from bumpversion.logging import setup_logging
1413
from bumpversion.utils import get_context, get_overrides
1514

1615
logger = logging.getLogger(__name__)
1716

1817

19-
# @click.group(cls=AliasedGroup)
20-
# @click.version_option(version=__version__)
21-
# @click.pass_context
22-
# def cli(ctx: Context) -> None:
23-
# """Version bump your Python project."""
24-
# if ctx.invoked_subcommand is None:
25-
# ctx.invoke(bump)
18+
@click.group(
19+
cls=AliasedGroup,
20+
invoke_without_command=True,
21+
context_settings={
22+
"ignore_unknown_options": True,
23+
"allow_interspersed_args": True,
24+
},
25+
)
26+
@click.version_option(version=__version__)
27+
@click.pass_context
28+
def cli(ctx: Context) -> None:
29+
"""Version bump your Python project."""
30+
if ctx.invoked_subcommand is None:
31+
ctx.invoke(bump, *ctx.args)
2632

2733

28-
@click.command(context_settings={"ignore_unknown_options": True})
29-
@click.version_option(version=__version__)
34+
@cli.command(context_settings={"ignore_unknown_options": True})
3035
@click.argument("args", nargs=-1, type=str)
3136
@click.option(
3237
"--config-file",
@@ -163,7 +168,7 @@
163168
is_flag=True,
164169
help="List machine readable information",
165170
)
166-
def cli(
171+
def bump(
167172
# version_part: str,
168173
args: list,
169174
config_file: Optional[str],

0 commit comments

Comments
 (0)