Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing program arguments through Cargo

Tags:

rust

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"

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

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?

like image 872
Juxhin Avatar asked Sep 04 '15 14:09

Juxhin


People also ask

How does cargo run work?

When no target selection options are given, cargo run will run the binary target. If there are multiple binary targets, you must pass a target flag to choose one. Or, the default-run field may be specified in the [package] section of Cargo. toml to choose the name of the binary to run by default.

What is cargo in Rust programming?

Cargo is Rust's build system and package manager. Most Rustaceans use this tool to manage their Rust projects because Cargo handles a lot of tasks for you, such as building your code, downloading the libraries your code depends on, and building those libraries. (We call the libraries that your code needs dependencies.)

What does cargo test do?

It extracts code samples from documentation comments of the library target, and then executes them. Different from normal test targets, each code block compiles to a doctest executable on the fly with rustc .


1 Answers

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

cargo run -- --test

From the man page:

All of the trailing arguments are passed to the binary to run. If you're passing arguments to both Cargo and the binary, the ones after -- go to the binary, the ones before go to Cargo.

like image 178
TartanLlama Avatar answered Sep 20 '22 17:09

TartanLlama