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 2024-02-24 07:45 -0600

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

2 

3from typing import List, Optional 

4 

5import rich_click as click 

6from click import Context 

7from rich_click.rich_group import RichGroup 

8 

9from bumpversion.ui import print_warning 

10 

11 

12class AliasedGroup(RichGroup): 

13 """ 

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

15 

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

17 """ 

18 

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

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

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

22 if rv is not None: 

23 return rv 

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

25 if not matches: 

26 args = [cmd_name, *ctx.args] 

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

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

29 elif len(matches) == 1: 

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

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

32 

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

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

35 # always return the full command name 

36 original_args = args[:] 

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

38 

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

40 if "bump" in original_args: 

41 original_args.remove("bump") 

42 else: 

43 print_warning( 

44 "Calling bumpversion without a subcommand is deprecated. " 

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

46 ) 

47 return cmd.name, cmd, original_args 

48 return cmd.name, cmd, args