Clap 无法解析 YAML 文件:无法将 YAML String("1") 值转换为字符串

Clap can not parse YAML file: failed to convert YAML String("1") value to a string

我使用 YAML 文件来定义我的命令行界面。我使用 Clap 的 load_yaml! 宏解析了该文件,它在一段时间内运行良好:

#[macro_use]
extern crate clap;
use clap::{App, ArgMatches};

fn main() {
    let yml = load_yaml!("cl_arguments.yml");
    let matches = App::from_yaml(yml).get_matches();
    # some code goes here
}

我没有对相关代码进行任何更改,它停止工作并且出现以下错误:

thread 'main' panicked at 'failed to convert YAML String("1") value to a string',
/home/me/.cargo/registry/src/github.com-1ecc6299db9ec823/clap-2.31.2/src/args/arg.rs:112:28

我不确定更新是否是造成此问题的原因。我更新了 Rust 并删除了 Cargo.lock 以排除不兼容问题,但这没有帮助。

这是产生错误的 YAML 文件的最小版本:

name: "tool"
version: "0.1"
about: "description"
author: "m00am"

subcommands:
  - subcommand1:
      args:
        - path:
            index: "1"
            required: true
            default_value: "/tmp/"
            help: "Dummy Path"
  - subcommand2:
      args:
         - other_path:
             index: "1"
             required: true
             help: "A second dummy path"

这是一个已知问题吗?这里出了什么问题?除了尝试旧版本的 Clap 和 YAML crate 的组合之外,我还能做些什么吗?

版本信息

摘自Cargo.toml

[dependencies]
rand = "*"
cute = "0.3.0"
fasthash = "*"
bio = "*"
rulinalg = "*"
serde = "*"
serde_derive = "*"
bincode = "*"
statrs = "*"
separator = "*"
termion = "*"
sysinfo = "*"
clap = { version = "*", features = ["yaml"] }
needletail = "*"
gnuplot = "*"
time = "*"
cue = "*"

摘自Cargo.lock

[[package]]
name = "clap"
version = "2.31.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
 "ansi_term 0.11.0 (registry+https://github.com/rust-lang/crates.io-index)",
 "atty 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)",
 "bitflags 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)",
 "strsim 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)",
 "textwrap 0.9.0 (registry+https://github.com/rust-lang/crates.io-index)",
 "unicode-width 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)",
 "vec_map 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)",
 "yaml-rust 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)",
]

...

[[package]]
name = "yaml-rust"
version = "0.3.5"
source = "registry+https://github.com/rust-lang/crates.io-index"

参数规范的index 属性 应该是整数,而不是字符串。可以在 documentation 中找到带有显式索引的位置参数示例。删除该值周围的引号,配置文件将再次工作。

name: "tool"
version: "0.1"
about: "description"
author: "m00am"

subcommands:
  - subcommand1:
      args:
        - path:
            index: 1
            required: true
            default_value: "/tmp/"
            help: "Dummy Path"
  - subcommand2:
      args:
         - other_path:
             index: 1
             required: true
             help: "A second dummy path"
$ cargo run -- subcommand1 --help

USAGE:
    tool subcommand1 <path>

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

ARGS:
    <path>    Dummy Path [default: /tmp/]

我无法确定为什么这在以前的版本中有效。实现有可能允许它超出预期的功能。否则,它可能是对具有 YAML 配置支持的 Clap 早期版本的重大更改。您的 Cargo.toml 文件中的依赖项对于长期使用而言过于灵活,并且更容易发生这些问题。坚持默认(插入符号)版本范围通常是正确的做法(参见 specifying dependencies)。