[英]Why does clap fail to compile when added to Cargo.toml?
I'm fairly new to Rust and decided to use it to port an existing project into it. 我对Rust还是很陌生,因此决定使用它将现有项目移植到其中。 I intended to use clap
to handle CLI options, but I keep getting errors. 我打算使用clap
来处理CLI选项,但是我总是遇到错误。
What do I need to do for clap
to install correctly so that it's usable in my project as a dependency (eg extern crate clap; [...] use clap::App; [...]
? 我需要做些什么才能正确安装clap
,以便它可以在我的项目中作为依赖项使用(例如extern crate clap; [...] use clap::App; [...]
?
I haven't had problems with other crates (so far), so I'm not sure what's so different here or if there's a problem with the crate itself. 到目前为止,我还没有遇到其他板条箱的问题,所以我不确定这里有什么不同之处,或者板条箱本身是否有问题。
I've already seen a few questions (eg this one ), which simply suggests that the dependency be added into the .toml
file or don't seem to provide a solution to what I'm seeing. 我已经看到了几个问题(例如, 这个问题),它只是建议将依赖项添加到.toml
文件中,或者似乎无法为我所看到的提供解决方案。
I'm in Ubuntu Linux, if that makes a difference. 我在Ubuntu Linux中,如果有所作为。
Adding clap = "2.33.0"
to my Cargo.toml
file (see https://crates.io/crates/clap ) causes VSCode (through RLS) to log the following: 在我的Cargo.toml
文件中添加clap = "2.33.0"
(请参阅https://crates.io/crates/clap )使VSCode(通过RLS)记录以下内容:
{
"resource": "[...]/Projects/takeout/Cargo.toml",
"owner": "rust",
"severity": 8,
"message": "Could not compile `clap`.\nprocess didn't exit successfully: `[...]/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/bin/rls --crate-name clap [...]/.cargo/registry/src/github.com-1ecc6299db9ec823/clap-2.33.0/src/lib.rs --color never --crate-type lib --emit=dep-info,metadata -C debuginfo=2 --cfg 'feature=\"ansi_term\"' --cfg 'feature=\"atty\"' --cfg 'feature=\"color\"' --cfg 'feature=\"default\"' --cfg 'feature=\"strsim\"' --cfg 'feature=\"suggestions\"' --cfg 'feature=\"vec_map\"' -C metadata=630980a214d5fd10 -C extra-filename=-630980a214d5fd10 --out-dir [...]/Projects/takeout/target/rls/debug/deps -L dependency=[...]/Projects/takeout/target/rls/debug/deps --extern ansi_term=[...]/Projects/takeout/target/rls/debug/deps/libansi_term-1510a9addefc0253.rmeta --extern atty=[...]/Projects/takeout/target/rls/debug/deps/libatty-7c4847fd9fc1e3d9.rmeta --extern bitflags=[...]/Projects/takeout/target/rls/debug/deps/libbitflags-8369a9aec15a5abb.rmeta --extern strsim=[...]/Projects/takeout/target/rls/debug/deps/libstrsim-301d1cf239e9cd24.rmeta --extern textwrap=[...]/Projects/takeout/target/rls/debug/deps/libtextwrap-a799d71e2d028df4.rmeta --extern unicode_width=[...]/Projects/takeout/target/rls/debug/deps/libunicode_width-58e38dd9d658dcfb.rmeta --extern vec_map=[...]/Projects/takeout/target/rls/debug/deps/libvec_map-4f8e59c92e9953d8.rmeta --cap-lints allow --error-format=json --sysroot [...]/.rustup/toolchains/stable-x86_64-unknown-linux-gnu` (exit code: 101)",
"startLineNumber": 1,
"startColumn": 1,
"endLineNumber": 10000,
"endColumn": 1
}
According to the README in the clap repo itself, just adding it should work: 据在自述拍回购本身,只是添加它应该工作:
For full usage, add clap as a dependency in your Cargo.toml to use from crates.io: 为了充分利用,请将clap作为依赖项添加到Cargo.toml中,以便从crates.io使用:
[dependencies] clap = "~2.33"
But it doesn't. 但事实并非如此。
I've tried it with and without the ~
prefix as well as clap = {version = "2.33", features = ["yaml"]}
, which is also shown in the repo, but no luck. 我已经尝试过使用带和不带~
前缀以及clap = {version = "2.33", features = ["yaml"]}
,它也在回购协议中显示,但是没有运气。 (Yes, I'm trying to load the CLI options from a .yaml
file.) (是的,我正在尝试从.yaml
文件加载CLI选项。)
Trying cargo install clap --version 2.33.0
from the shell simply returns an error message saying: error: specified package has no binaries
. 尝试从shell进行cargo install clap --version 2.33.0
只是返回一条错误消息,内容为: error: specified package has no binaries
。
Aiming directly at the Git repo also produces the same error message: 直接针对Git存储库也会产生相同的错误消息:
cargo install --git https://github.com/clap-rs/clap.git --tag v2.31.2 --features yaml 101 ↵
Updating git repository `https://github.com/clap-rs/clap.git`
Installing clap v2.31.2 (https://github.com/clap-rs/clap.git?tag=v2.31.2#07c15d28)
error: specified package has no binaries
Note that there's no v2.33.0
tag in the Git repo (at the time of this writing). 请注意,Git存储库中没有v2.33.0
标签(在撰写本文时)。
Bonus if you know how to get VSCode to stop marking everything as an error: 如果您知道如何使VSCode停止将所有内容都标记为错误,则有好处:
There's misunderstanding about the cargo install
command. 关于cargo install
命令存在误解。 You can learn more about it here . 您可以在此处了解更多信息。
This command manages Cargo's local set of installed binary crates. 此命令管理Cargo的已安装二进制板条箱的本地集。 Only packages which have executable
[[bin]]
or[[example]]
targets can be installed, and all executables are installed into the installation root'sbin
folder. 只能安装具有可执行文件[[bin]]
或[[example]]
目标的软件包,并且所有可执行文件都将安装到安装根目录的bin
文件夹中。
It's not your case. 这不是你的情况。 The only thing you have to do is to list clap
in the dependencies
section ( Cargo.toml
). 您唯一要做的就是在“ dependencies
部分( Cargo.toml
)中列出clap
Cargo.toml
。 That's all. 就这样。 No need to use cargo install
at all. 完全不需要使用cargo install
。 cargo build
, cargo run
, ... commands will download & compile & statically link all dependencies. cargo build
, cargo run
,...命令将下载并编译并静态链接所有依赖项。
Folder structure: 资料夹结构:
.
├── Cargo.toml
└── src
├── cli.yaml
└── main.rs
Current directory: 当前目录:
$ pwd
/Users/robertvojta/Projects/stackoverflow/clap-yaml
Cargo.toml
content: Cargo.toml
内容:
[package]
name = "clap-yaml"
version = "0.1.0"
authors = ["Zrzka"]
edition = "2018"
[dependencies]
clap = { version = "2.33.0", features = ["yaml"] }
src/cli.yaml
content: src/cli.yaml
内容:
name: clap-yaml
version: "1.0"
author: Zrzka
about: Stackoverflow sample
args:
- lang:
short: l
long: lang
default_value: cz
takes_value: true
possible_values:
- cz
- en
src/main.rs
content: src/main.rs
内容:
use clap::{App, load_yaml};
fn main() {
let yaml = load_yaml!("cli.yaml");
let matches = App::from_yaml(yaml).get_matches();
match matches.value_of("lang").unwrap() {
"cz" => println!("Ahoj"),
"en" => println!("Hello"),
_ => unreachable!("see possible_values in yaml, handled by clap"),
};
}
Run it with cargo
: 用cargo
运行:
$ cargo -q run -- --lang en
Hello
Run it directly: 直接运行它:
$ cargo build
...
Finished dev [unoptimized + debuginfo] target(s) in 0.01s
$ target/debug/clap-yaml --lang cz
Ahoj
I still have vscode complaining and underlining everything in red in the
Cargo.toml
file. 我仍然有vscode抱怨并在Cargo.toml
文件Cargo.toml
红色强调所有内容。 Any suggestions to fix this completely? 有什么建议可以彻底解决此问题吗? It seems close to a full resolution. 似乎接近完整的分辨率。
I can confirm that this problem do exist in Rust 1.34.0. 我可以确认Rust 1.34.0中确实存在此问题。 I did install this version and I've got same symptoms: 我确实安装了此版本,并且出现了相同的症状:
Cargo.toml
is underlined (error) 整个Cargo.toml
下划线(错误) There're two ways how to fix this. 有两种解决方法。
Update your Cargo.toml
file dependencies
section manually if you'd like to stick with Rust 1.34.0: 如果您要坚持使用Rust 1.34.0,请手动更新您的Cargo.toml
文件dependencies
部分:
[dependencies]
bitflags = "=1.0.4"
clap = { version = "2.33.0", features = ["yaml"] }
Or update your Rust toolchain to >= 1.35.0. 或将您的Rust工具链更新为> = 1.35.0。
I just tested both ways and it works. 我只是测试了两种方式,它都可以工作。
Related issues: 相关问题:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.