我想创建一个 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/