我们从Python开源项目中,提取了以下50个代码示例,用于说明如何使用click.Group()。
def list_commands(self, ctx): self._load_plugin_commands() # The commands available is the list of both the application (if # available) plus the builtin commands. rv = set(click.Group.list_commands(self, ctx)) info = ctx.ensure_object(ScriptInfo) try: rv.update(info.load_app().cli.list_commands(ctx)) except Exception: # Here we intentionally swallow all exceptions as we don't # want the help page to break if the app does not exist. # If someone attempts to use the command we try to create # the app again and this will give us the error. pass return sorted(rv)
def get_command(self, ctx, command_name): # Search builtin commands (normal behaviour). command = click.Group.get_command(self, ctx, command_name) if command is not None: return command # Match against explicit aliases. if command_name in ctx.obj.get('aliases'): actual_command = ctx.obj['aliases'][command_name] return click.Group.get_command(self, ctx, actual_command) # Match commands by automatic abbreviation of the command name (e.g., # 'st' will match 'status') matches = [x for x in self.list_commands(ctx) if x.startswith(command_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)))
def get_command(self, ctx, cmd_name): """Allow aliases for commands. """ if cmd_name == 'list': cmd_name = 'ls' elif cmd_name == 'search': cmd_name = 'find' elif cmd_name == 'gen': cmd_name = 'generate' elif cmd_name == 'add': cmd_name = 'insert' elif cmd_name in ['remove', 'delete']: cmd_name = 'rm' elif cmd_name == 'rename': cmd_name = 'mv' elif cmd_name == 'copy': cmd_name = 'cp' # TODO(benedikt) Figure out how to make 'show' the default # command and pass cmd_name as the first argument. rv = click.Group.get_command(self, ctx, cmd_name) if rv is not None: return rv
def test_no_parameters(self): """Validate a `click.Group` with no parameters. This exercises the code paths for a group with *no* arguments, *no* options and *no* environment variables. """ @click.group() def cli(): """A sample command group.""" pass ctx = click.Context(cli, info_name='cli') output = list(ext._format_command(ctx, show_nested=False)) self.assertEqual(textwrap.dedent(""" .. program:: cli .. code-block:: shell cli [OPTIONS] COMMAND [ARGS]... """).lstrip(), '\n'.join(output))
def get_command(self, ctx, cmd_name): rv = click.Group.get_command(self, ctx, cmd_name) if rv is not None: return rv mapping = { 'svc': 'service', 'services': 'service', 'node': 'container-instance', 'nodes': 'container-instance', 'container-instances': 'container-instance', 'clusters': 'cluster', 'task-definitions': 'task-definition', 'taskdef': 'task-definition', 'taskdefs': 'task-definition', 'task-definition-families': 'task-definition-family', 'taskdef-family': 'task-definition-family', 'taskdef-families': 'task-definition-family', 'tasks': 'task', } if cmd_name in mapping: rv = click.Group.get_command(self, ctx, mapping[cmd_name]) if rv is not None: return rv
def custom_sort(param): """Custom Click(Command|Group).params sorter. Case insensitive sort with capitals after lowercase. --version at the end since I can't sort --help. :param click.core.Option param: Parameter to evaluate. :return: Sort weight. :rtype: int """ option = param.opts[0].lstrip('-') if param.param_type_name != 'option': return False, return True, option == 'version', option.lower(), option.swapcase()
def build_options(func): """Add "build" Click options to function. :param function func: The function to wrap. :return: The wrapped function. :rtype: function """ func = click.option('-a', '--banner-greatest-tag', is_flag=True, help='Override banner-main-ref to be the tag with the highest version number.')(func) func = click.option('-A', '--banner-recent-tag', is_flag=True, help='Override banner-main-ref to be the most recent committed tag.')(func) func = click.option('-b', '--show-banner', help='Show a warning banner.', is_flag=True)(func) func = click.option('-B', '--banner-main-ref', help="Don't show banner on this ref and point banner URLs to this ref. Default master.")(func) func = click.option('-i', '--invert', help='Invert/reverse order of versions.', is_flag=True)(func) func = click.option('-p', '--priority', type=click.Choice(('branches', 'tags')), help="Group these kinds of versions at the top (for themes that don't separate them).")(func) func = click.option('-r', '--root-ref', help='The branch/tag at the root of DESTINATION. Will also be in subdir. Default master.')(func) func = click.option('-s', '--sort', multiple=True, type=click.Choice(('semver', 'alpha', 'time')), help='Sort versions. Specify multiple times to sort equal values of one kind.')(func) func = click.option('-t', '--greatest-tag', is_flag=True, help='Override root-ref to be the tag with the highest version number.')(func) func = click.option('-T', '--recent-tag', is_flag=True, help='Override root-ref to be the most recent committed tag.')(func) func = click.option('-w', '--whitelist-branches', multiple=True, help='Whitelist branches that match the pattern. Can be specified more than once.')(func) func = click.option('-W', '--whitelist-tags', multiple=True, help='Whitelist tags that match the pattern. Can be specified more than once.')(func) return func
def command(self, *args, **kwargs): """This works exactly like the method of the same name on a regular :class:`click.Group` but it wraps callbacks in :func:`with_appcontext` unless it's disabled by passing ``with_appcontext=False``. """ wrap_for_ctx = kwargs.pop('with_appcontext', True) def decorator(f): if wrap_for_ctx: f = with_appcontext(f) return click.Group.command(self, *args, **kwargs)(f) return decorator
def group(self, *args, **kwargs): """This works exactly like the method of the same name on a regular :class:`click.Group` but it defaults the group class to :class:`AppGroup`. """ kwargs.setdefault('cls', AppGroup) return click.Group.group(self, *args, **kwargs)
def get_command(self, ctx, cmd_name): rv = click.Group.get_command(self, ctx, cmd_name) if rv is not None: return rv # base command "fs-cli" defines and then passes all global # options to children commands