blog icon indicating copy to clipboard operation
blog copied to clipboard

Zig: Create a command line tool with Zig

Open hhstore opened this issue 1 year ago • 10 comments

related:

  • ✅ #374
  • ✅ #377
  • ✅ #363

vscode:

  • #376

hhstore avatar Sep 08 '22 17:09 hhstore

使用 zig 编写 cli 命令行工具:

需求背景:

场景1:

  • 个人开发机器, git, zsh, fish, ssh, go, rust, zig 等一些工具, 模板配置(最优配置段).

场景2:

  • 初始化项目, 经常需要频繁添加一些模板文件(license, .gitignore, Python, go, rust, zig 等项目配置模板).
  • 虽然可以用项目模板工具来初始化, 但还是会遇到偶尔需要零星添加 or 改动一些文件.

小工具:

  • tryadd-cli: 暂定命名.

功能:

  1. 支持添加各种模板文件.
  2. 交互式命令, 组合添加. 比如 .gitignore 组合多个语言(go, python, rust) 并集.
  3. 支持远程 get 下载模板文件: 支持 github.
  4. 支持全局配置文件, 本地局部自定义配置.
  5. 支持 env 环境变量取参.

为什么使用 zig 开发?

  • ✅ zig 和 rust 类似, 很适合写 cli 工具. 跨平台, 依赖少, 安装简单, 体积小.
  • ✅ zig 语法比 rust 更简单+易上手.
  • ✅ zig 也有 zig-clap 这种高仿 rust clap 的杀手级 cli 工具包.

hhstore avatar Sep 08 '22 17:09 hhstore

依赖包:

  • ✅ https://github.com/Hejsil/zig-clap
    • 借鉴 rust clap
  • ✅ https://github.com/PrajwalCH/yazap
    • 借鉴 rust clap
  • ✅ https://github.com/sam701/zig-cli
    • 借鉴 go urfave/cli

文件下载:

  • https://github.com/marler8997/ziget
  • https://github.com/mattn/zig-curl

配置文件解析:

  • https://github.com/kubkon/zig-yaml
  • https://github.com/aeronavery/zig-toml

模板引擎:

  • 类似 jinja 之类的包
  • https://github.com/travisstaloch/template.zig
  • https://github.com/batiati/mustache-zig
  • https://github.com/nektro/zig-pek
  • https://github.com/haze/etch

其他语言类似:

  • ✅ rust: https://github.com/clap-rs/clap
  • ✅ go: https://github.com/urfave/cli

hhstore avatar Sep 08 '22 17:09 hhstore

cli 参考示例项目:

search:

  • https://github.com/topics/cli?l=zig
  • https://github.com/topics/command-line?l=rust

zig:

  • https://github.com/Hejsil/zig-clap/blob/master/example/simple.zig
  • https://github.com/natecraddock/zf

基于 zig-clap:

  • ✅ https://github.com/knadh/csv2json

rust:

  • ✅ https://github.com/ajeetdsouza/zoxide

hhstore avatar Sep 08 '22 17:09 hhstore

将 cli 工具发布到 homebrew:

  • homebrew 是 mac 平台第三方工具托管平台.
  • https://github.com/Homebrew/homebrew-core

参考:

  • https://docs.brew.sh/Formula-Cookbook
  • https://docs.brew.sh/How-to-Create-and-Maintain-a-Tap
  • https://xie.infoq.cn/article/fdb63902ad0516a03c8e3b4f5
  • https://guoxudong.io/post/golang-to-homebrew/
  • https://bharathvaj-ganesan.medium.com/how-to-publish-your-nodejs-application-on-homebrew-7a24de9ea973

hhstore avatar Sep 08 '22 17:09 hhstore

1

hhstore avatar Sep 08 '22 17:09 hhstore

1

hhstore avatar Sep 08 '22 19:09 hhstore

1

hhstore avatar Sep 08 '22 19:09 hhstore

1

hhstore avatar Sep 08 '22 19:09 hhstore

1

hhstore avatar Sep 08 '22 19:09 hhstore

1

hhstore avatar Sep 08 '22 19:09 hhstore