CLIG
cli采用
CLIG是命令行用户接口规范(Command Line Interface Guidelines),也就是如果设计一个命令行工具,应该如何设计,如何实现,如何使用,如何测试等等。 注意这里的命令行工具是指CLI,而不是TUI,后者是指基于文本的图形用户界面。
这里列举一些常见语言的命令行工具库:
- Multi-platform: docopt
- Bash: argc
- Go: Cobra, cli
- Haskell: optparse-applicative
- Java: picocli
- Node: oclif
- Deno: std/cli
- Perl: Getopt::Long
- PHP: CLImate
- Python: Argparse, Click, Typer
- Ruby: TTY
- Rust: clap, structopt
- Swift: swift-argument-parser
12 Factor CLI Apps也是很好的参考:
- Great help is essential
- Prefer flags to args
- What version am I on?
- Mind the streams
- Handle things going wrong
- Be fancy!
- Prompt if you can
- Use tables
- Be speedy
- Encourage contributions
- Be clear about subcommands
- Follow XDG-spec
个人在日常开发中经常要开发一些命令行工具,主要是开发简单,高效,配合其他命令行使用,非常容易实现自动化。 而开发一个好的CLI应用,CLIG规范是非常重要的参考。
命令行的典型配置项:
- NO_COLOR="1" 请参考 https://no-color.org/
- CLI_SILENT: 只输出错误,返回值不为0
- CLI_VERBOSE:详细输出
References
- CLIG: https://clig.dev/
- 12 Factor CLI Apps: https://medium.com/@jdxcode/12-factor-cli-apps-dd3c227a0e46