Coverage for /Users/OORDCOR/Documents/code/bump-my-version/bumpversion/aliases.py: 0%

27 statements  

« prev     ^ index     » next       coverage.py v7.3.2, created at 2023-12-10 08:09 -0600

1"""Utilities for handling command aliases.""" 

2from typing import List, Optional 

3 

4import rich_click as click 

5from click import Context 

6from rich_click.rich_group import RichGroup 

7 

8from bumpversion.ui import print_warning 

9 

10 

11class AliasedGroup(RichGroup): 

12 """ 

13 This following example implements a subclass of Group that accepts a prefix for a command. 

14 

15 If there were a command called ``push``, it would accept ``pus`` as an alias (so long as it was unique) 

16 """ 

17 

18 def get_command(self, ctx: Context, cmd_name: str) -> Optional[click.Command]: 

19 """Given a context and a command name, this returns a Command object if it exists or returns None.""" 

20 rv = click.Group.get_command(self, ctx, cmd_name) 

21 if rv is not None: 

22 return rv 

23 matches = [x for x in self.list_commands(ctx) if x.startswith(cmd_name)] 

24 if not matches: 

25 args = [cmd_name, *ctx.args] 

26 new_ctx = self.make_context(ctx.info_name, args, parent=ctx) 

27 return click.Group.get_command(self, new_ctx, "bump") 

28 elif len(matches) == 1: 

29 return click.Group.get_command(self, ctx, matches[0]) 

30 ctx.fail(f"Too many matches: {', '.join(sorted(matches))}") 

31 

32 def resolve_command(self, ctx: Context, args: List[str]) -> tuple: 

33 """Find the command and make sure the full command name is returned.""" 

34 # always return the full command name 

35 original_args = args[:] 

36 _, cmd, args = super().resolve_command(ctx, args) 

37 

38 if cmd.name == "bump" and args != original_args: 

39 if "bump" in original_args: 

40 original_args.remove("bump") 

41 else: 

42 print_warning( 

43 "Calling bumpversion without a subcommand is deprecated. " 

44 "Please use `bump-my-version bump` instead" 

45 ) 

46 return cmd.name, cmd, original_args 

47 return cmd.name, cmd, args