#script #command #builds #set #cli-tool #zsh #parser

build easy-cli

从一组脚本中构建 CLI 工具

2 个稳定版本

1.0.1 2022年11月27日
1.0.0 2022年11月20日

#437 in 构建工具

MIT 许可证

17KB
354 代码行

Crates.io

easy-cli

用于构建个人和团队 CLI 工具的工具。

easy-cli 接受 CLI 工具名称选项和脚本目录路径作为参数。它假设每个脚本都是一个 CLI 命令,并构建相应的 CLI 解析器。

它可以作为别名使用,但在我的经验中(在 zsh 中)这不适用于补全。您可以为您 CLI 定义一个函数,例如

mycli() {
    <Path>/easy-cli --name mycli <Path-to-easy-cli-root>/example -- $@
}

然后调用

mycli

将产生输出

error: 'mycli' requires a subcommand but one was not provided
  [subcommands: hello, list, help]

Usage: mycli <COMMAND>

For more information try '--help'

然后调用

mycli hello

将打印

Hello, world!

命令后面的参数将被传递给相关的脚本。

参数解析

easy-cli 在脚本中寻找有关命令和参数的元数据。目前,以下模式被识别

  • #@description <description>:用于命令行帮助文本中描述命令。
  • #@argument <argname> <has_args> <description>:定义一个可用的 "--argname" 参数。它可能或可能没有值(has_args),并且可以有帮助文本(description)
  • #@anyarg:如果存在,允许命令接受任何参数。否则,只接受定义的参数。

补全

easy-cli 在多个 shell 中提供您的 CLI 的补全功能 - 由 clap_complete 支持的 shell。要为您 CLI 生成补全,请运行

easy-cli --name <cli-name> <Path-to-cli-dir> --completions <shell> > <completions_file>

并按照您的 shell 所需进行操作。

计划中的新功能

  1. CLI 子命令(例如,cli <commandA> <subcommandA1>),以分组相关命令。
  2. 从脚本中解析脚本选项并将其添加到 CLI 中,以提供更好的帮助。
  3. 环境,以便 CLI 可以轻松地应用于不同的环境(例如,用于测试)。

依赖关系

~3–4.5MB
~71K SLoC