跳转到主要内容

AsyncClick中缺少选项组

项目描述

click-option-group

PyPI version Build status Coverage Status Supported Python versions License

click-option-group是一个Click扩展包,它添加了Click中缺少的选项组功能。

目标和动机

Click是一个用于在Python中创建强大而美观的命令行界面(CLI)的包,但它没有创建选项组的函数。

选项组是一种方便的机制,用于逻辑组织CLI,同时也允许您设置分组选项的特定行为和关系(例如互斥选项)。此外,argparse标准库包包含此功能。

同时,许多Click用户需要此功能。您可以在以下问题中阅读有关此功能的有趣讨论

本包的目的是提供具有可扩展功能的组选项,使用规范且干净的API(尽可能使用类似Click的API)。

快速入门

安装

使用pip安装和更新

pip install -U click-option-group

简单示例

这是一个简单示例,说明如何在基于Click的CLI中使用选项组。只需使用optgroup通过装饰类似Click API的命令函数来声明选项组。

# app.py

import click
from click_option_group import optgroup, RequiredMutuallyExclusiveOptionGroup

@click.command()
@optgroup.group('Server configuration',
                help='The configuration of some server connection')
@optgroup.option('-h', '--host', default='localhost', help='Server host name')
@optgroup.option('-p', '--port', type=int, default=8888, help='Server port')
@optgroup.option('-n', '--attempts', type=int, default=3, help='The number of connection attempts')
@optgroup.option('-t', '--timeout', type=int, default=30, help='The server response timeout')
@optgroup.group('Input data sources', cls=RequiredMutuallyExclusiveOptionGroup,
                help='The sources of the input data')
@optgroup.option('--tsv-file', type=click.File(), help='CSV/TSV input data file')
@optgroup.option('--json-file', type=click.File(), help='JSON input data file')
@click.option('--debug/--no-debug', default=False, help='Debug flag')
def cli(**params):
    print(params)

if __name__ == '__main__':
    cli()
$ python app.py --help
Usage: app.py [OPTIONS]

Options:
  Server configuration:           The configuration of some server connection
    -h, --host TEXT               Server host name
    -p, --port INTEGER            Server port
    -n, --attempts INTEGER        The number of connection attempts
    -t, --timeout INTEGER         The server response timeout
  Input data sources: [mutually_exclusive, required]
                                  The sources of the input data
    --tsv-file FILENAME           CSV/TSV input data file
    --json-file FILENAME          JSON input data file
  --debug / --no-debug            Debug flag
  --help                          Show this message and exit.

文档

https://click-option-group.readthedocs.io

项目详情


下载文件

下载适合您平台的文件。如果您不确定选择哪个,请了解有关安装包的更多信息。

源代码分发

asyncclick_option_group-0.5.6.1.tar.gz (22.6 kB 查看哈希值)

上传时间 源代码

构建分发

asyncclick_option_group-0.5.6.1-py3-none-any.whl (11.5 kB 查看哈希值)

上传时间 Python 3

由以下支持