本文介绍了通过 Cargo 传递程序参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些示例代码目前正在使用 getopts,它被指定为 getopts代码>Cargo.toml

I have some sample code that is currently making use of getopts which is specified as a dependency in Cargo.toml

[dependencies]
getopts = "0.2"

但是我似乎无法通过 Cargo 传递参数 (-t, --test)(例如 cargo run --test)出于显而易见的原因.

However I can not seem to pass argument (-t, --test) through Cargo (e.g. cargo run --test) for obvious reasons.

因为我已经指定了外部依赖,尝试运行 rustc src/main.rs --test 也不会工作:

Since I have specified that external dependency, trying to run rustc src/main.rs --test won't work either:

src/main.rs:2:5: 2:21 error: unresolved import `getopts::Options`. There is no `Options` in `getopts`
src/main.rs:2 use getopts::Options;
              ^~~~~~~~~~~~~~~~
error: aborting due to previous error

是否有其他方法可以实现这一目标或目前有一些常见的替代方法?

Is there another way to achieve this or some common alternative for the time being?

推荐答案

您可以使用 -- 将尾随参数传递给 cargo run:

You can pass trailing arguments to cargo run using --:

cargo run -- --test

man 页面:

所有尾随参数都传递给要运行的二进制文件.如果你路过Cargo 和二进制文件的参数, -- 之后的那些转到二进制文件,去 Cargo 之前的那些.

这篇关于通过 Cargo 传递程序参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-18 20:12