def resolve_command(self, ctx, args): # Patch the `resolve_command` function to enable simple commands (eg. cld resource) # Only core commands from API and modules are registered (eg. cld admin) cmd_name = make_str(args[0]) original_cmd_name = cmd_name cmd = self.get_command(ctx, cmd_name) if cmd is None and ctx.token_normalize_func is not None: cmd_name = ctx.token_normalize_func(cmd_name) cmd = self.get_command(ctx, cmd_name) if cmd is None and not ctx.resilient_parsing: if split_opt(cmd_name)[0]: self.parse_args(ctx, ctx.args) if original_cmd_name in api.__dict__: cmd = self.get_command(ctx, "admin") return cmd_name, cmd, args elif original_cmd_name in uploader.__dict__: cmd = self.get_command(ctx, "uploader") return cmd_name, cmd, args else: ctx.fail('No such command "%s".' % original_cmd_name) return cmd_name, cmd, args[1:]
def resolve_command(self, ctx, args): cmd_name = make_str(args[0]) original_cmd_name = cmd_name cmd = self.get_command(ctx, cmd_name) if cmd is None and ctx.token_normalize_func is not None: cmd_name = ctx.token_normalize_func(cmd_name) cmd = self.get_command(ctx, cmd_name) if cmd is None and not ctx.resilient_parsing: if split_opt(cmd_name)[0]: self.parse_args(ctx, ctx.args) if original_cmd_name in api.__dict__: cmd = self.get_command(ctx, "admin") return cmd_name, cmd, args elif original_cmd_name in uploader.__dict__: cmd = self.get_command(ctx, "uploader") return cmd_name, cmd, args elif original_cmd_name in account.__dict__: cmd = self.get_command(ctx, "account") return cmd_name, cmd, args else: ctx.fail('No such command "%s".' % original_cmd_name) return cmd_name, cmd, args[1:]
def resolve_command(self, ctx, args: List[str]) -> Tuple[str, Command, List[str]]: """ Modified version of :class:`click.core.MultiCommand.resolve_command` which bumps the version to the given string if it isn't one of ``major``, ``minor`` or ``patch``. :param ctx: :param args: """ # noqa: D400 # 3rd party from click.parser import split_opt from click.utils import make_str cmd_name = make_str(args[0]) # Get the command cmd = self.get_command(ctx, cmd_name) # If we can't find the command but there is a normalization # function available, we try with that one. if cmd is None and ctx.token_normalize_func is not None: # pragma: no cover cmd_name = ctx.token_normalize_func(cmd_name) cmd = self.get_command(ctx, cmd_name) # If we don't find the command we want to show an error message # to the user that it was not provided. However, there is # something else we should do: if the first argument looks like # an option we want to kick off parsing again for arguments to # resolve things like --help which now should go to the main # place. if cmd is None and not ctx.resilient_parsing: if split_opt(cmd_name)[0]: # pragma: no cover self.parse_args(ctx, ctx.args) @release_options @click.argument( "version", type=click.STRING, ) @release_command() def version(version: str, commit: Optional[bool], message: str, force: bool): # 3rd party from packaging.version import Version # this package from repo_helper.release import Bumper bumper = Bumper(PathPlus.cwd(), force) bumper.bump(Version(version.lstrip('v')), commit, message) return "version", cast(Command, version), args return cmd_name, cmd, args[1:]
def resolve_command( self, ctx: click.Context, args: List[str], ) -> Tuple[str, click.Command, List[str]]: # noqa: D102 """ Resolve the requested command belonging to this group, and print a suggestion if it can't be found. :param ctx: :param args: :return: The name of the matching command, the :class:`click.Command` object itself, and any remaining arguments. """ cmd_name = make_str(args[0]) original_cmd_name = cmd_name # Get the command cmd = self.get_command(ctx, cmd_name) # If we can't find the command but there is a normalization # function available, we try with that one. if cmd is None and ctx.token_normalize_func is not None: cmd_name = ctx.token_normalize_func(cmd_name) cmd = self.get_command(ctx, cmd_name) # If we don't find the command we want to show an error message # to the user that it was not provided. # However, there is something else we should do: # if the first argument looks like an option we want to kick off parsing again # for arguments to resolve things like --help which now should go to the main place. if cmd is None and not ctx.resilient_parsing: if split_opt(cmd_name)[0]: self.parse_args(ctx, ctx.args) closest = difflib.get_close_matches(original_cmd_name, self.commands, n=1) message = [f"No such command '{original_cmd_name}'."] if closest: message.append(f"The most similar command is {closest[0]!r}.") ctx.fail('\n'.join(message)) # TODO: cmd here is Optional[click.Command], typeshed says it should be just click.Command # I think typeshed is wrong. # https://github.com/python/typeshed/blob/484c014665cdf071b292dd9630f207c03e111895/third_party/2and3/click/core.pyi#L171 return cmd_name, cmd, args[1:] # type: ignore