Esempio n. 1
0
def add_commands(click_group: click.core.Group, commands: Iterable):
    for command in commands:
        if not isinstance(command, click.core.Command) and not isinstance(
                command, click.core.Group):
            raise TypeError(
                f"commands must be of type click.core.Command or click.core.Group, got {type(command)}"
            )

        click_group.add_command(command)
Esempio n. 2
0
def _set_commands(click_group: click.core.Group):
    """
    Set commands to click group based on the options in .fetchmerc file
    """
    config_path = _get_config_path()
    config = ConfigParser.from_files(config_path)

    option_names = config.options('fetchme')

    for i in option_names:
        func = _get_command_func(i, config)

        click_group.command(name=i)(click.pass_context(func))
Esempio n. 3
0
def readme(
    main_cli: click.core.Group,
    prog_name: str,
    context_settings: Dict[str, Any],
    output: str = 'rst',
) -> Any:
    '''Recursively output a beautiful readme on stdout, supports 3 levels of subcommands'''
    if output == 'rst':
        write_header("Commands", '-')
    elif output == 'markdown':
        click.echo('# Commands\n')

    base_ctx = click.Context(main_cli, info_name=prog_name, **context_settings)
    with base_ctx.scope():
        cli_help = main_cli.get_help(base_ctx)
        write_codeblock(cli_help, output)

        for command_name, command in sorted(main_cli.commands.items()):
            if command.hidden:
                continue

            command_header = f"{prog_name} {command_name}"
            if output == 'rst':
                write_header(command_header, '*')
            elif output == 'markdown':
                click.echo(f'\n## {command_header}\n')

            command_ctx = click.Context(command,
                                        info_name=command_name,
                                        parent=base_ctx)
            with command_ctx.scope():
                command_help = command.get_help(command_ctx)
                write_codeblock(command_help, output)

            if not isinstance(command, AdvancedGroup):
                continue

            for subcommand_name, subcommand in sorted(
                    command.commands.items()):
                if subcommand_name == 'help' or subcommand.hidden:
                    continue

                subcommand_header = f"{prog_name} {command_name} {subcommand_name}"
                if output == 'rst':
                    write_header(f"{subcommand_header}", '*')
                elif output == 'markdown':
                    click.echo(f'\n### {subcommand_header}\n')

                subcommand_ctx = click.Context(subcommand,
                                               info_name=subcommand_name,
                                               parent=command_ctx)
                with subcommand_ctx.scope():
                    subcommand_help = subcommand.get_help(subcommand_ctx)
                    write_codeblock(subcommand_help, output)
Esempio n. 4
0
def register_subcommand(
        parent_command: click.core.Group,
        config_type: Type[_ConfigType],
        stage: Type[Stage[ResultType, _ConfigType]]
) -> _WrapperType[ResultType]:
    """Register a subcommand to a main command."""
    command_name = capitalized_to_dashed(stage.__name__).lower()
    wrapper = _create_cli_option_wrapper(stage.process, config_type)
    help_text = stage.__doc__
    return parent_command.command(
            name=command_name, help=help_text)(wrapper)
Esempio n. 5
0
def bypass(group: click.core.Group, command: click.core.Command,
           command_name: str) -> Callable[..., Any]:
    """
    This function is specially important for people developing scripts in
    papis.

    Suppose you're writing a plugin that uses the ``add`` command as seen
    in the command line in papis. However you don't want exactly the ``add``
    command and you want to add some behavior before calling it, and you
    don't want to write your own ``add`` function from scratch.

    You can then use the following snippet

    .. code::python

        import click
        import papis.cli
        import papis.commands.add

        @click.group()
        def main():
            \"\"\"Your main app\"\"\"
            pass

        @papis.cli.bypass(main, papis.commands.add.cli, "add")
        def add(**kwargs):
            # do some logic here...
            # and call the original add command line function by
            papis.commands.add.cli.bypassed(**kwargs)
    """
    group.add_command(command, command_name)

    def _decorator(new_callback: Callable[..., Any]) -> None:
        setattr(command, "bypassed", command.callback)
        command.callback = new_callback

    return _decorator
Esempio n. 6
0
def register_commands(cli: click.core.Group) -> None:
    commands = (init, draft, partial, release, entry)

    for command in commands:
        cli.add_command(command)