首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Clap无法解析YAML文件:未能将YAML字符串(“1”)值转换为字符串

Clap无法解析YAML文件:未能将YAML字符串(“1”)值转换为字符串
EN

Stack Overflow用户
提问于 2018-04-20 12:14:43
回答 1查看 1.2K关注 0票数 2

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

代码语言:javascript
复制
#[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
}

如果不对相关代码进行任何更改,该代码将停止工作,并得到以下错误:

代码语言:javascript
复制
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文件的最低版本,它会产生错误:

代码语言:javascript
复制
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机箱的组合之外,还有什么可以做的吗?

版本信息

Cargo.toml摘录

代码语言:javascript
复制
[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摘录

代码语言:javascript
复制
[[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"
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-04-20 13:53:45

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

代码语言:javascript
复制
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"
代码语言:javascript
复制
$ 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/]

我无法确定为什么这在以前的版本中有效。实现有可能允许它超出预期的功能。否则,这可能是与早期版本的Clap和YAML配置支持的不同之处。Cargo.toml文件中的依赖关系对于长期使用来说过于灵活,使得这些问题更有可能发生。坚持默认(插入)版本范围通常是正确的(参见指定依赖项)。

票数 5
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/49941435

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档