在第 2 拍中,以下内容:
\n.arg(\n Arg::with_name("files")\n .value_name("FILE")\n .help("Input file(s)")\n .multiple(true)\n .default_value("-"),\n)\nRun Code Online (Sandbox Code Playgroud)\n将产生:
\nUSAGE:\n catr [FLAGS] [FILE]...\n\nFLAGS:\n -h, --help Prints help information\n\nRun Code Online (Sandbox Code Playgroud)\n我想在 Clap 4 中使用deriveAPI 来表达这一点。
所以,我得到了结构:
\npub struct Config{ // Define a public struct called Config.\n /// The input files\n #[arg(default_value_t= vec!["-".to_string()])]\n files: Vec<String>,\nRun Code Online (Sandbox Code Playgroud)\n当我 时cargo build,我得到以下信息:
[ 19:37:42 ] \xe2\x9d\xaf cargo build\n Compiling catr v0.1.0 (/home/david/Work/Bitbucket/OReilly/Books/cmdlinerust/ch03/catr)\nerror[E0277]: `Vec<std::string::String>` doesn\'t implement `std::fmt::Display`\n --> src/lib.rs:10:11\n |\n10 | #[arg(default_value_t=vec!["-".to_string()])]\n | ^^^^^^^^^^^^^^^ `Vec<std::string::String>` cannot be formatted with the default formatter\n |\n = help: the trait `std::fmt::Display` is not implemented for `Vec<std::string::String>`\n = note: in format strings you may be able to use `{:?}` (or {:#?} for pretty-print) instead\n = note: required for `Vec<std::string::String>` to implement `ToString`\nRun Code Online (Sandbox Code Playgroud)\n作为 Rust 初学者,我知道这Vec<String>并没有实现该Display特征,但我认为我无法为Vec<String>.
无论如何,问题是如何将 Clap 2 中的代码转换为 Clap 4(我相信因为我有,所以Vec<String>不必显式指定“多个部分”)?
解决方案是用一个字母:default_values_t而不是default_value_t.
#[arg(default_values_t = ["-".to_string()])]
Run Code Online (Sandbox Code Playgroud)
你也不需要,vec!因为它需要IntoIterator<Item = String>.
| 归档时间: |
|
| 查看次数: |
2573 次 |
| 最近记录: |