Gre*_*egH 2 python command-line-interface python-click
我正在尝试做一个接受多个参数的 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=...
Run Code Online (Sandbox Code Playgroud)
在这种情况下,存在一组预定义的操作(“创建”、“删除”等),并且每个操作都有一组特定的预定义对象,每个操作都可以对其进行操作。在这种情况下,“创建”操作只能接受“表”或“视图”对象。
在“drop”操作的情况下,用户只能指定“表”和“用户”对象。在单击的情况下,“创建”、“表”、“视图”和“删除”只是参数吗?如果是这样,如何将可以指定的内容限制为特定值?我不确定这是否是使用组、命令等的情况,如果是,如何?
您想要做的是点击组的确切用例。在您的示例中create, anddrop是组, and table、view等...是命令。(在我看来)这种类型的结构使得 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()
Run Code Online (Sandbox Code Playgroud)
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
Run Code Online (Sandbox Code Playgroud)
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.
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
487 次 |
| 最近记录: |