|
| 1 | +import re |
| 2 | +import shlex |
| 3 | +from gettext import gettext as _ |
| 4 | + |
| 5 | +import click |
| 6 | +from click.formatting import wrap_text |
| 7 | + |
| 8 | + |
| 9 | +def find_and_replace_command_variable(arg, command, index): |
| 10 | + match = re.match(r"^--(?P<arg_name>[^=\ ]+)[\ =](?P<arg_value>.+)", arg) |
| 11 | + if match: |
| 12 | + arg_name, arg_value = match.groups() |
| 13 | + if f"${arg_name}" in command: |
| 14 | + return True, command.replace(f"${arg_name}", arg_value) |
| 15 | + elif f'${arg_name.replace("-", "_")}' in command: |
| 16 | + return True, command.replace(f'${arg_name.replace("-", "_")}', arg_value) |
| 17 | + if f"${index}" in command: |
| 18 | + return True, command.replace(f"${index}", arg) |
| 19 | + return False, command |
| 20 | + |
| 21 | + |
| 22 | +def parse_alias(alias): |
| 23 | + from developers_chamber.scripts import cli |
| 24 | + |
| 25 | + command_parts = list(shlex.split(alias)) |
| 26 | + used_command_parts = [] |
| 27 | + |
| 28 | + click_command = cli |
| 29 | + while command_parts: |
| 30 | + command_part = command_parts[0] |
| 31 | + current_click_command = click_command.get_command(None, command_part) |
| 32 | + if current_click_command is None: |
| 33 | + raise click.ClickException( |
| 34 | + 'Command to the alias "{}" cannot be found'.format(alias) |
| 35 | + ) |
| 36 | + |
| 37 | + used_command_parts.append(command_parts.pop(0)) |
| 38 | + click_command = current_click_command |
| 39 | + if not isinstance(current_click_command, click.Group): |
| 40 | + break |
| 41 | + return used_command_parts, command_parts, click_command, alias |
| 42 | + |
| 43 | + |
| 44 | +class AliasCommand(click.Command): |
| 45 | + |
| 46 | + def __init__(self, commands, *args, **kwargs): |
| 47 | + super().__init__(*args, **kwargs) |
| 48 | + self._parse_alias(commands) |
| 49 | + |
| 50 | + def _parse_alias(self, commands): |
| 51 | + if isinstance(commands, str): |
| 52 | + commands = [commands] |
| 53 | + description = "" |
| 54 | + elif isinstance(commands, list): |
| 55 | + commands = commands |
| 56 | + description = "" |
| 57 | + elif isinstance(commands, dict): |
| 58 | + description = commands["description"] |
| 59 | + commands = commands["command"] |
| 60 | + else: |
| 61 | + raise click.ClickException("Invalid alias type") |
| 62 | + |
| 63 | + commands = [commands] if isinstance(commands, str) else commands |
| 64 | + self.aliases = [(parse_alias(alias)) for alias in commands] |
| 65 | + self.short_help = description |
| 66 | + |
| 67 | + def format_help_text(self, ctx, formatter) -> None: |
| 68 | + formatter.write_paragraph() |
| 69 | + with formatter.indentation(): |
| 70 | + if self.short_help: |
| 71 | + formatter.write_text(self.short_help) |
| 72 | + formatter.write_paragraph() |
| 73 | + formatter.write_text("Alias to commands: ") |
| 74 | + with formatter.indentation(): |
| 75 | + for ( |
| 76 | + used_command_parts, |
| 77 | + remaining_command_parts, |
| 78 | + click_command, |
| 79 | + alias_str, |
| 80 | + ) in self.aliases: |
| 81 | + formatter.write_text("* " + " ".join(used_command_parts)) |
| 82 | + if remaining_command_parts: |
| 83 | + with formatter.indentation(), formatter.indentation(): |
| 84 | + formatter.write_text(" ".join(remaining_command_parts)) |
| 85 | + |
| 86 | + def format_epilog(self, ctx, formatter) -> None: |
| 87 | + with formatter.section(_("Commands in alias help")): |
| 88 | + for ( |
| 89 | + used_command_parts, |
| 90 | + remaining_command_parts, |
| 91 | + click_command, |
| 92 | + alias_str, |
| 93 | + ) in self.aliases: |
| 94 | + with formatter.indentation(): |
| 95 | + formatter.write_paragraph() |
| 96 | + formatter.write_text("* " + " ".join(used_command_parts)) |
| 97 | + with formatter.indentation(), formatter.indentation(): |
| 98 | + click_command.format_help_text(ctx, formatter) |
| 99 | + click_command.format_options(ctx, formatter) |
| 100 | + formatter.write_paragraph() |
| 101 | + |
| 102 | + def invoke(self, ctx): |
| 103 | + from developers_chamber.scripts import cli |
| 104 | + |
| 105 | + for i, ( |
| 106 | + used_command_parts, |
| 107 | + remaining_command_parts, |
| 108 | + click_command, |
| 109 | + alias_str, |
| 110 | + ) in enumerate(self.aliases): |
| 111 | + if i == 0: |
| 112 | + alias_args = [] |
| 113 | + |
| 114 | + for index, arg in enumerate(ctx.args, start=1): |
| 115 | + replaced_arg, alias_str = find_and_replace_command_variable( |
| 116 | + arg, alias_str, index |
| 117 | + ) |
| 118 | + if not replaced_arg: |
| 119 | + alias_args.append(arg) |
| 120 | + |
| 121 | + if "$@" in alias_str: |
| 122 | + alias_str = alias_str.replace("$@", " ".join(ctx.args)) |
| 123 | + alias_args = [] |
| 124 | + |
| 125 | + cli.main(args=shlex.split(alias_str) + alias_args, standalone_mode=False) |
0 commit comments