예제 #1
0
def _collect_params(cmd: click.Command, ctx: click.Context) -> Dict[str, Optional[str]]:
    params = ctx.params.copy()
    for param in cmd.get_params(ctx):
        if param.name not in params:
            continue
        if params[param.name] == param.get_default(ctx):
            # drop default param
            del params[param.name]
            continue
        if param.param_type_name != "option":
            # save name only
            params[param.name] = None
        else:
            if getattr(param, "secure", True):
                params[param.name] = None
            else:
                params[param.name] = str(params[param.name])
    return params
    def get_params(self, ctx):
        print("???")
        if not config_exists():
            click.echo("WARNING NO CONFIG FILE FOUND!!!", err=True)
            result = click.prompt("WOULD YOU LIKE TO SETUP CONFIG NOW(y/n)?",
                                  type=click.Choice(['y', 'n']))
            if result == "n":
                click.echo(
                    "GOODBYE! you cannot continue without configuring the system",
                    err=True)
                sys.exit(1)
            subprocess.Popen([sys.executable, sys.argv[0], 'runsetup'],
                             stderr=sys.stderr,
                             stdout=sys.stdout).communicate()

        if self.require:
            cfg = get_config()
            for item in self.require:
                print("CHK:", item, cfg.has_section(item))
                if not cfg.has_section(item):
                    cmd = globals().get('setup_' + item, None)
                    if cmd:
                        click.echo(
                            "It appears your {0} is not setup".format(item))
                        result = click.prompt(
                            "WOULD YOU LIKE TO SETUP {0} NOW(y/n)?".format(
                                item),
                            type=click.Choice(['y', 'n']))
                        if result == "n":
                            click.echo(
                                "GOODBYE! you cannot continue without configuring the system",
                                err=True)
                            sys.exit(1)
                        print("SETUP {0} NOW".format(item))
                        subprocess.Popen(
                            [sys.executable, sys.argv[0], cmd.name],
                            stderr=sys.stderr,
                            stdout=sys.stdout).communicate()
                    else:
                        raise Exception(
                            "Cannot find setup_{0} method".format(item))

        return Command.get_params(self, ctx)
예제 #3
0
파일: main.py 프로젝트: zdog234/typer-cli
def get_docs_for_click(
    *,
    obj: Command,
    ctx: typer.Context,
    indent: int = 0,
    name: str = "",
    call_prefix: str = "",
) -> str:
    docs = "#" * (1 + indent)
    command_name = name or obj.name
    if call_prefix:
        command_name = f"{call_prefix} {command_name}"
    title = f"`{command_name}`" if command_name else "CLI"
    docs += f" {title}\n\n"
    if obj.help:
        docs += f"{obj.help}\n\n"
    usage_pieces = obj.collect_usage_pieces(ctx)
    if usage_pieces:
        docs += "**Usage**:\n\n"
        docs += "```console\n"
        docs += "$ "
        if command_name:
            docs += f"{command_name} "
        docs += f"{' '.join(usage_pieces)}\n"
        docs += "```\n\n"
    args = []
    opts = []
    for param in obj.get_params(ctx):
        rv = param.get_help_record(ctx)
        if rv is not None:
            if param.param_type_name == "argument":
                args.append(rv)
            elif param.param_type_name == "option":
                opts.append(rv)
    if args:
        docs += f"**Arguments**:\n\n"
        for arg_name, arg_help in args:
            docs += f"* `{arg_name}`"
            if arg_help:
                docs += f": {arg_help}"
            docs += "\n"
        docs += "\n"
    if opts:
        docs += f"**Options**:\n\n"
        for opt_name, opt_help in opts:
            docs += f"* `{opt_name}`"
            if opt_help:
                docs += f": {opt_help}"
            docs += "\n"
        docs += "\n"
    if obj.epilog:
        docs += f"{obj.epilog}\n\n"
    if isinstance(obj, Group):
        group: Group = cast(Group, obj)
        commands = group.list_commands(ctx)
        if commands:
            docs += f"**Commands**:\n\n"
            for command in commands:
                command_obj = group.get_command(ctx, command)
                assert command_obj
                docs += f"* `{command_obj.name}`"
                command_help = command_obj.get_short_help_str()
                if command_help:
                    docs += f": {command_help}"
                docs += "\n"
            docs += "\n"
        for command in commands:
            command_obj = group.get_command(ctx, command)
            assert command_obj
            use_prefix = ""
            if command_name:
                use_prefix += f"{command_name}"
            docs += get_docs_for_click(
                obj=command_obj, ctx=ctx, indent=indent + 1, call_prefix=use_prefix
            )
    return docs