gpt4 book ai didi

python - 使用click创建复杂的命令行界面

转载 作者:行者123 更新时间:2023-12-01 00:03:43 25 4
gpt4 key购买 nike

我正在尝试做一个接受多个参数的 CLI,我猜你会说这些参数是嵌套的并且是预定义的。例如,假设我正在尝试创建一个管理关系数据库的实用程序。我想要如下命令:

dbmgr.py create table --name="mytab"
dbmgr.py create view --name="myview" --other-opt=...
dbmgr.py drop table --name=...
dbmgr.py drop user --username=...

在这种情况下,有一组预定义的操作(“创建”、“删除”等),并且每个操作都有一组特定的预定义对象,每个操作都可以对其进行操作。在这种情况下,“创建”操作只能接受“表”或“ View ”对象。

在“删除”操作的情况下,用户只能指定“表”和“用户”对象。在单击的情况下,“创建”、“表”、“ View ”和“删除”只是参数吗?如果是这样,如何将可以指定的内容限制为特定值?我不确定这是否是使用组、命令等的情况,如果是,如何使用?

最佳答案

您想要做的正是 click groups 的确切用例。在您的示例中,createdrop 是组,tableview 等...是命令。 (在我看来)这种类型的结构使得 Click 比其他 Python 命令行解析库更好。它可以很好地描述这些结构。

示例代码:

import click

@click.group()
def cli():
"""DB Manager CLI"""

@cli.group()
def create():
"""create objects"""

@create.command()
def table():
click.echo('create table command')

@create.command()
def view():
click.echo('create view command')

@cli.group()
def drop():
"""create objects"""

@drop.command()
def table():
click.echo('drop table command')

@drop.command()
@click.option('--username')
def user(username):
click.echo('drop user command: {}'.format(username))

if __name__ == "__main__":
cli()

测试代码:

if __name__ == "__main__":
commands = (
'create table',
'create view',
'drop table',
'drop user --username a-user',
'--help',
'drop --help',
'drop user --help',
)

import sys, time
time.sleep(1)
print('Click Version: {}'.format(click.__version__))
print('Python Version: {}'.format(sys.version))
for cmd in commands:
try:
time.sleep(0.1)
print('-----------')
print('> ' + cmd)
time.sleep(0.1)
cli(cmd.split())

except BaseException as exc:
if str(exc) != '0' and \
not isinstance(exc, (click.ClickException, SystemExit)):
raise

结果:

Click Version: 7.0
Python Version: 3.8.1 (tags/v3.8.1:1b293b6, Dec 18 2019, 22:39:24) [MSC v.1916 32 bit (Intel)]
-----------
> create table
create table command
-----------
> create view
create view command
-----------
> drop table
drop table command
-----------
> drop user --username a-user
drop user command: a-user
-----------
> --help
Usage: test.py [OPTIONS] COMMAND [ARGS]...

DB Manager CLI

Options:
--help Show this message and exit.

Commands:
create create objects
drop create objects
-----------
> drop --help
Usage: test.py drop [OPTIONS] COMMAND [ARGS]...

create objects

Options:
--help Show this message and exit.

Commands:
table
user
-----------
> drop user --help
Usage: test.py drop user [OPTIONS]

Options:
--username TEXT
--help Show this message and exit.

关于python - 使用click创建复杂的命令行界面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60131553/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com