#toml #config-file #command-line #editing #shell #querying #interface

app toml-cli

一个用于编辑和查询TOML文件的简单CLI

5个版本

0.2.3 2022年12月5日
0.2.2 2022年12月4日
0.2.1 2022年12月3日
0.2.0 2019年9月24日
0.1.0 2019年9月2日

#935 in 解析实现

Download history 750/week @ 2024-03-14 770/week @ 2024-03-21 954/week @ 2024-03-28 867/week @ 2024-04-04 938/week @ 2024-04-11 1000/week @ 2024-04-18 940/week @ 2024-04-25 978/week @ 2024-05-02 1178/week @ 2024-05-09 1036/week @ 2024-05-16 777/week @ 2024-05-23 1033/week @ 2024-05-30 1414/week @ 2024-06-06 1959/week @ 2024-06-13 2878/week @ 2024-06-20 1600/week @ 2024-06-27

8,076 每月下载量

MIT 许可证

27KB
565 代码行

toml-cli

这是toml命令的家园,这是一个用于编辑和查询TOML文件的简单CLI。

toml命令的目的是要实用

  • 在shell脚本中,用于查阅或编辑配置文件;
  • 以及在人类可以遵循的指令中,用作可复制粘贴并运行的命令。

界面灵感的来源是git config命令,该命令很好地服务于这两个目的,而不必了解Git配置文件的含义——只需了解它们的通用结构。

一个关键特性是,在编辑时,我们力求保留格式和注释——文件的唯一更改应该是用户特别请求的更改。为此,我们依赖toml_edit存储库,该存储库也是cargo-edit的基础。在toml_edit中存在一些边缘情况,其中toml_edit可以重新排列格式奇特的文件(在toml_edit文档中描述);但对于典型的TOML文件,我们以完美的忠实度维护这一特性。

该命令的状态为实验性。当前的界面还没有达到它应有的效果,并且预计会有不兼容的更改

用法

读取:toml get

要读取特定数据,传递一个TOML路径:一系列路径段,每个段可以是

  • .KEY,用于索引表或内联表,或
  • [INDEX],用于索引表数组或数组。

默认情况下,数据以JSON格式输出

$ toml get Cargo.toml bin[0]
{"name":"toml","path":"src/main.rs"}

当数据是字符串时,使用--raw-r选项可以直接打印它,方便在脚本等环境中使用。

$ toml get Cargo.toml dependencies.serde --raw
1.0

如果您需要一个更复杂的查询,可以考虑使用jq这样的工具,而toml只需将文件转换为JSON。

$ toml get pyoxidizer.toml . | jq '
    .embedded_python_config[] | select(.build_target | not) | .raw_allocator
  ' -r
jemalloc

(TOML路径.是空路径的别名,描述整个文件。)

编写方式:toml set

要编辑数据,传递一个TOML路径来指定在哪里放置解析树中的数据,然后放置的数据值。

$ cat >foo.toml <<EOF
[a]
b = "c"
EOF

$ toml set foo.toml x.y z
[a]
b = "c"

[x]
y = "z"

这个子命令在两个方面相当原始

  • 实际上我们没有编辑文件;我们只打印出新版本。
  • 要设置的值必须是一个字符串;布尔值、数组等的输入尚未实现。

参考

基本命令toml

$ toml --help
toml-cli 0.2.3
A simple CLI for editing and querying TOML files.

USAGE:
    toml <SUBCOMMAND>

FLAGS:
    -h, --help       Prints help information
    -V, --version    Prints version information

SUBCOMMANDS:
    get     Print some data from the file
    help    Prints this message or the help of the given subcommand(s)
    set     Edit the file to set some data (currently, just print modified version)

toml get

$ toml get --help
toml-get 0.2.3
Print some data from the file

Read the given TOML file, find the data within it at the given query,
and print.

If the TOML document does not have the given key, exit with a
failure status.

Output is JSON by default.  With `--raw`/`-r`, if the data is a
string, print it directly.  With `--output-toml`, print the data
as a fragment of TOML.

USAGE:
    toml get [FLAGS] <path> <query>

FLAGS:
    -h, --help           Prints help information
        --output-toml    Print as a TOML fragment (default: print as JSON)
    -r, --raw            Print strings raw, not as JSON
    -V, --version        Prints version information

ARGS:
    <path>     Path to the TOML file to read
    <query>    Query within the TOML data (e.g. `dependencies.serde`, `foo[0].bar`)

toml set

$ toml set --help
toml-set 0.2.3
Edit the file to set some data (currently, just print modified version)

USAGE:
    toml set <path> <query> <value-str>

FLAGS:
    -h, --help       Prints help information
    -V, --version    Prints version information

ARGS:
    <path>         Path to the TOML file to read
    <query>        Query within the TOML data (e.g. `dependencies.serde`, `foo[0].bar`)
    <value-str>    String value to place at the given spot (bool, array, etc. are TODO)

依赖项

~7MB
~132K SLoC