python - 如何使用 python-click 构建类似 kubectl 的 CLI?

标签 python python-3.x command-line-interface python-click

我想创建一个 CLI,以 kubectl 方式接收要工作的操作和资源参数。例如。

myctl init config
myctl create issue|pr|branch
myctl delete issue|pr|branch|config

myctl 命令应始终接收 2 个参数,以便用户不会尝试类似以下内容:myctl init config delete issues。此外,不应该能够执行不可能的组合,例如 myctl create config

我想到了一些类似的代码:

import click

@click.command()
@click.group()
@click.option(
    "init,create,delete",
    type=str,
    help="Action name.",
)
@click.option(
    "config,issue,pr,branch",
    type=str,
    help="Resource name.",
)
def main(action: str, resource: str) -> None:
    pass

if __name__ == "__main__":
    main(prog_name="myctl")

我不太确定应该使用哪些点击元素来构建它(参数、选项、组等)以及如何将它们组合在一起。

最佳答案

我过去通过创建多个嵌套组实现了这一点。

import click


@click.group('cli')
def cli():
    pass


@click.group('init')
def init():
    pass


@click.group('create')
def create():
    pass


@click.group('delete')
def delete():
    pass


@init.command('config')
def config():
    print('Configuration complete')


@create.command('issue')
@click.argument('name')
def issue(name):
    print(f'Created {name}')


@create.command('pr')
@click.argument('base_branch', required=False, default='master')
def pr(base_branch):
    print(f'Created PR against {base_branch}')


@create.command('branch')
@click.argument('name')
def branch(name):
    print(f'Create branch {name}')


@delete.command('issue')
@click.argument('issue_number')
def delete_issue(issue_number):
    print(f'Deleting issue {issue_number}')


@delete.command('pr')
@click.argument('pr_number')
def delete_pr(pr_number):
    print(f'Deleting PR {pr_number}')


@delete.command('branch')
@click.argument('branch_name')
def delete_branch(branch_name):
    print(f'Deleting branch {branch_name}')


@delete.command('config')
@click.argument('config_name')
def delete_config(config_name):
    print(f'Deleting config {config_name}')


cli.add_command(init)
cli.add_command(create)
cli.add_command(delete)


def run_cli():
    cli()


if __name__ == "__main__":
    run_cli()

然后,您可以根据需要扩展它,调用如下所示。我已调用 CLI play

❯ play
Usage: play [OPTIONS] COMMAND [ARGS]...

Options:
  --help  Show this message and exit.

Commands:
  create
  delete
  init

❯ play init
Usage: play init [OPTIONS] COMMAND [ARGS]...

Options:
  --help  Show this message and exit.

Commands:
  config

❯ play init config
Configuration complete

❯ play create
Usage: play create [OPTIONS] COMMAND [ARGS]...

Options:
  --help  Show this message and exit.

Commands:
  branch
  issue
  pr

❯ play create branch feature/the-coolest
Create branch feature/the-coolest

然后您可以继续添加简短的帮助消息并将其自定义到您的应用程序。

关于python - 如何使用 python-click 构建类似 kubectl 的 CLI?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62640555/

相关文章:

Travis CI 构建上的 Django 单元测试失败

python - 读取压缩的 JSON 文件

linux - Ubuntu 中打印 IP 地址的命令不理解

python - 在 Django 中对与特定类相关的帖子进行注释和计数

python - 列表中的 x, *y 在 python 中是什么意思

python - 如何通过 networkx 制作可点击的图表?

ruby - 将 thor 用于复杂的命令行工具

python - 在 matplotlib 中创建一个离散的颜色条

python - 熔化和删除重复数据帧的反转

bash - 带有值的Docker命令行参数,何时使用空格与何时使用等号[duplicate]