def get_command(self, ctx: Context, name: str) -> Optional[Union[Command, 'PluginCLI']]: # Dashes aren't valid in Python identifiers, so let's just replace them here. name = name.replace('-', '_') command_map: Dict[str, str] = self.command_to_canonical_map if name in command_map: return self._get_command(command_map[name]) matches = match_prefix(command_map.keys(), name, return_unique=False) if matches is None: matches = [] if len(matches) == 1: match = command_map[matches[0]] return self._get_command(match) if ' ' not in name: cmd = self._try_suffix_match(ctx, name) if cmd: return cmd if len(matches) > 1: ctx.fail('"{name}" matches {matches}; be more specific?'.format( name=name, matches=', '.join( click.style(match, bold=True) for match in sorted(matches)))) return None
def parse_args(self: Command, ctx: Context, args: List[str], supportsLiterals: Callable[[click.Parameter], bool]): if not args and self.no_args_is_help and not ctx.resilient_parsing: click.echo(ctx.get_help(), color=ctx.color) ctx.exit() args = PrettyHelper.parse_line(args) ctx.original_params = args.copy() parser = self.make_parser(ctx) opts, args, param_order = parser.parse_args(args=args) ctx.original_args = args.copy() i = 0 for param in iter_params_for_processing(param_order, self.get_params(ctx)): if supportsLiterals(param): value, args, i = param.handle_parse_result(ctx, opts, args, i) else: value, args = param.handle_parse_result(ctx, opts, args) if args and not ctx.allow_extra_args and not ctx.resilient_parsing: ctx.fail("Got unexpected extra argument{} ({})".format( "s" if len(args) != 1 else "", " ".join(map(make_str, args)))) ctx.args = args return args
def cli(ctx: ClickContext): if 'debian' not in Sys.id_like(): ctx.fail("Sorry, sutler only supports Debian based systems") app = ctx.ensure_object(App) if app.is_root(): ctx.fail("You're not allowed to run sutler as root") if ctx.invoked_subcommand is None: click.echo(ctx.get_help())
def get_command(self, ctx: Context, cmd_name: str) -> Optional[Command]: rv = click.Group.get_command(self, ctx, cmd_name) if rv is not None: return rv matches = [ x for x in self.list_commands(ctx) if x.startswith(cmd_name) ] if not matches: return None elif len(matches) == 1: return click.Group.get_command(self, ctx, matches[0]) ctx.fail("Too many matches: %s" % ", ".join(sorted(matches)))