1 unstable release
Uses old Rust 2015
0.9.1 | Jan 8, 2020 |
---|
#1479 in Encoding
38KB
535 lines
cargo_metadata
Structured access to the output of cargo metadata
. Usually used from within a cargo-*
executable.
Also supports serialization to aid in implementing --message-format=json
-like
output generation in cargo-*
subcommands, since some of the types in what
cargo --message-format=json
emits are exactly the same as the ones from cargo metadata
.
Documentation - contains examples of use with std::env::args
, docopt
, clap
, and structopt
.
lib.rs
:
Structured access to the output of cargo metadata
and cargo --message-format=json
.
Usually used from within a cargo-*
executable
See the cargo book for details on cargo itself.
Examples
With std::env::args()
:
let mut args = std::env::args().skip_while(|val| !val.starts_with("--manifest-path"));
let mut cmd = cargo_metadata::MetadataCommand::new();
let manifest_path = match args.next() {
Some(ref p) if p == "--manifest-path" => {
cmd.manifest_path(args.next().unwrap());
}
Some(p) => {
cmd.manifest_path(p.trim_start_matches("--manifest-path="));
}
None => {}
};
let _metadata = cmd.exec().unwrap();
With docopt
:
const USAGE: &str = "
Cargo metadata test function
Usage:
cargo_metadata [--manifest-path PATH]
";
#[derive(Debug, Deserialize)]
struct Args {
arg_manifest_path: Option<String>,
}
let args: Args = Docopt::new(USAGE)
.and_then(|d| d.deserialize())
.unwrap_or_else(|e| e.exit());
let mut cmd = cargo_metadata::MetadataCommand::new();
if let Some(path) = args.arg_manifest_path {
cmd.manifest_path(path);
}
let _metadata = cmd.exec().unwrap();
With clap
:
let matches = clap::App::new("myapp")
.arg(
clap::Arg::with_name("manifest-path")
.long("manifest-path")
.value_name("PATH")
.takes_value(true),
)
.get_matches();
let mut cmd = cargo_metadata::MetadataCommand::new();
if let Some(path) = matches.value_of("manifest-path") {
cmd.manifest_path(path);
}
let _metadata = cmd.exec().unwrap();
With structopt
:
#[derive(Debug, StructOpt)]
struct Opt {
#[structopt(name = "PATH", long="manifest-path", parse(from_os_str))]
manifest_path: Option<PathBuf>,
}
let opt = Opt::from_args();
let mut cmd = cargo_metadata::MetadataCommand::new();
if let Some(path) = opt.manifest_path {
cmd.manifest_path(path);
}
let _metadata = cmd.exec().unwrap();
Pass features flags
use cargo_metadata::{MetadataCommand, CargoOpt};
let _metadata = MetadataCommand::new()
.manifest_path("./Cargo.toml")
.features(CargoOpt::AllFeatures)
.exec()
.unwrap();
Parse message-format output:
use std::process::{Stdio, Command};
use cargo_metadata::Message;
let mut command = Command::new("cargo")
.args(&["build", "--message-format=json"])
.stdout(Stdio::piped())
.spawn()
.unwrap();
for message in cargo_metadata::parse_messages(command.stdout.take().unwrap()) {
match message.unwrap() {
Message::CompilerMessage(msg) => {
println!("{:?}", msg);
},
Message::CompilerArtifact(artifact) => {
println!("{:?}", artifact);
},
Message::BuildScriptExecuted(script) => {
println!("{:?}", script);
},
_ => () // Unknown message
}
}
let output = command.wait().expect("Couldn't get cargo's exit status");
Dependencies
~3MB
~65K SLoC