1 个不稳定版本
0.1.0 | 2024年2月11日 |
---|
#1492 在 命令行实用工具
11KB
106 行
toq
从 toml 文件中快速获取所需内容
我创建了这个小型二进制程序,因为我发现当处理不依赖中间 json 文件步骤的 toml 文件时,几乎没有替代方案,所以我创建了一个。
安装
从 crates.io 下载二进制文件
cargo install toq
用法
二进制程序的设计是模仿 jq
的。虽然并没有实现 jq 的所有功能,但二进制程序在从 toml 文件中快速获取信息时仍然提供了功能。
例如,这是我们提供的 toml 文件
# test.toml
[[sources]]
name = "Some Name"
date = "Some date"
[[sources.times]]
time_1 = "1010010"
time_2 = "1010101"
[[sources.times]]
time_3 = "1111111"
time_4 = "0000000"
[[sources]]
name = "Other Name"
date = "Other date"
[[sources.times]]
time_1 = "202020"
time_2 = "20202020"
[[sources.times]]
time_3 = "222222"
time_4 = "00000000"
[[sources]]
name = "Other Other Name"
date = "Other Other date"
[[sources.times]]
time_1 = "30303030"
time_2 = "303030300"
[[sources.times]]
time_3 = "33333333"
time_4 = "000000000"
运行一个简单的查询,我们得到
$ toq '.' "test.toml"
{ sources = [{ date = "Some date", name = "Some Name", times = [{ time_1 = "1010010", time_2 = "1010101" }, { time_3 = "1111111", time_4 = "0000000" }] }, { date = "Other date", name = "Other Name", times = [{ time_1 = "202020", time_2 = "20202020" }, { time_3 = "222222", time_4 = "00000000" }] }, { date = "Other Other date", name = "Other Other Name", times = [{ time_1 = "30303030", time_2 = "303030300" }, { time_3 = "33333333", time_4 = "000000000" }] }] }
进一步探索
$ toq '.sources' "test.toml"
[{ date = "Some date", name = "Some Name", times = [{ time_1 = "1010010", time_2 = "1010101" }, { time_3 = "1111111", time_4 = "0000000" }] }, { date = "Other date", name = "Other Name", times = [{ time_1 = "202020", time_2 = "20202020" }, { time_3 = "222222", time_4 = "00000000" }] }, { date = "Other Other date", name = "Other Other Name", times = [{ time_1 = "30303030", time_2 = "303030300" }, { time_3 = "33333333", time_4 = "000000000" }] }]
遍历数组
$ toq '.sources[]' "test.toml"
{ date = "Some date", name = "Some Name", times = [{ time_1 = "1010010", time_2 = "1010101" }, { time_3 = "1111111", time_4 = "0000000" }] }
{ date = "Other date", name = "Other Name", times = [{ time_1 = "202020", time_2 = "20202020" }, { time_3 = "222222", time_4 = "00000000" }] }
{ date = "Other Other date", name = "Other Other Name", times = [{ time_1 = "30303030", time_2 = "303030300" }, { time_3 = "33333333", time_4 = "000000000" }] }
遍历嵌套数组
$ toq '.sources[].times[]' "test.toml"
{ time_1 = "1010010", time_2 = "1010101" }
{ time_3 = "1111111", time_4 = "0000000" }
{ time_1 = "202020", time_2 = "20202020" }
{ time_3 = "222222", time_4 = "00000000" }
{ time_1 = "30303030", time_2 = "303030300" }
{ time_3 = "33333333", time_4 = "000000000" }
在嵌套数组中选择一个条目将过滤出仅具有这些条目的对象
$ toq '.sources[].times[].time_1' "test.toml"
"1010010"
"202020"
"30303030"
如您所见,结合此二进制程序和其他 CLI 工具具有潜在的可能性,以在需要时加速此文件类型的文本处理。
依赖关系
~270–530KB
~11K SLoC