Skip to main content

基于 argparse 的微型 Python CLI 库

项目描述

PyCLI

一个基于 的 Tiny Python CLI 库argparse,让我们更容易添加子命令。

如果您使用 argparse 解析参数,并且您对添加子命令及其处理程序感到非常不安,那么 PyCLI 就是您想要的。

构建状态

演示

这是 pycli 最简单的演示。

# demo.py
from pycli import CLI

cli = CLI(prog="app", version="v1.0.0")

@cli.command
def add(x, y):
    """get sum of two numbers"""
    return x + y

print(cli.run())

然后我们运行它来获取帮助信息,添加了一个名为的子命令add

$ python demo.py -h
usage: app [-h] [-v] {add} ...

optional arguments:
  -h, --help     show this help message and exit
  -v, --version  show program's version number and exit

subcommands:
  {add}

接下来我们查找子命令的帮助信息,添加了两个位置参数:

$ python demo.py add -h
usage: app add [-h] x y

get sum of two numbers

positional arguments:
  x
  y

optional arguments:
  -h, --help  show this help message and exit

我们使用子命令得到两个数字的和,我们期望的输出是 3,但给出的是 12,所以我们需要指定参数的类型:

$ python demo.py add 1 2
12

参数类型

默认情况下,参数的类型是str,我们可以通过函数注释来改变它。

我们指定参数xyint 类型:

# demo.py
from pycli import CLI

cli = CLI(prog="app", version="v1.0.0")

@cli.command
def add(x: int, y: int):
    """get sum of two numbers"""
    return x + y

print(cli.run())

我们运行子命令来获得 1 和 2 的总和,现在给出结果 3:

# python demo.py add 1 2
3

默认参数

一些参数可能有默认值,因此我们可以将其设为可选,而无需在运行时传递它们。

# demo.py
from pycli import CLI

cli = CLI(prog="app", version="v1.0.0")

@cli.command
def add(x: int, y: int = 3):
    """get sum of two numbers"""
    return x + y

print(cli.run())

我们查找子命令的帮助信息,y成为可选参数:

$ python demo.py add -h
usage: app add [-h] [--y Y] x

get sum of two numbers

positional arguments:
  x           type: <int>

optional arguments:
  -h, --help  show this help message and exit
  --y Y       type: <int> (default: 3)

列表参数

有时我们可能需要一个列表参数,例如,指定多个配置文件:

# demo.py
from pycli import CLI

cli = CLI(prog="app", version="v1.0.0")

@cli.command
def init(conf_files: list):
    """Initialize system"""

    # do something here
    return conf_files

print(cli.run())

现在我们可以将多个值传递给同一个参数,然后将返回一个字符串列表:

$ python demo.py init --conf-files a.ini --conf-files b.ini
['a.ini', 'b.ini']

布尔参数

有时我们需要一个参数作为开关,我们可以将它的类型指定为 bool 并给它一个默认值 True/False。

如果我们在运行时传递它,它的值将被切换:

# demo.py
import queue
from pycli import CLI

cli = CLI(prog="app", version="v1.0.0")
q = queue.Queue()

@cli.command
def get(block: bool = True):
    """get a item"""

    return q.get(block)

print(cli.run())

自定义子命令

默认情况下,PyCLI 使用函数名或对象的类名作为子命令标题,并使用 docstring 作为子命令描述。

您可以根据需要自定义它们:

# demo.py
from pycli import CLI

cli = CLI(prog="app", version="v1.0.0")

@cli.command_with_args(title="sum", description="Sum of two integers")
def add(x: int, y: int):
    """get sum of two numbers"""
    return x + y

print(cli.run())

现在添加了一个名为的子命令sum,如下所示:

$ python demo.py sum -h
usage: app sum [-h] x y

Sum of two integers

positional arguments:
  x           type: <int>
  y           type: <int>

optional arguments:
  -h, --help  show this help message and exit

与 Argparse 集成

pycli.CLI是 的子类argparse.ArgumentParser,因此它有一些 API 作为ArgumentParser.

下载文件

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

源分布

py3cli-1.0.1.tar.gz (10.4 kB 查看哈希

已上传 source

内置分布

py3cli-1.0.1-py3-none-any.whl (6.6 kB 查看哈希

已上传 py3