Skip to content

Commit b9c664f

Browse files
authored
Merge pull request #149 from PhilippMolitor/feature/flags
implement argument flags
2 parents a0fb1c5 + 7a0e049 commit b9c664f

File tree

1 file changed

+20
-12
lines changed

1 file changed

+20
-12
lines changed

src/lib.rs

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,7 @@ To see all the flags the proxied tool accepts run `cargo-{} -- --help`.{}",
178178
Arg::new("quiet")
179179
.long("quiet")
180180
.short('q')
181+
.action(ArgAction::SetTrue)
181182
.help("Don't print build output from `cargo build`"),
182183
Arg::new("package")
183184
.long("package")
@@ -191,6 +192,7 @@ To see all the flags the proxied tool accepts run `cargo-{} -- --help`.{}",
191192
.help("Number of parallel jobs, defaults to # of CPUs"),
192193
Arg::new("lib")
193194
.long("lib")
195+
.action(ArgAction::SetTrue)
194196
.conflicts_with_all(["bin", "example", "test", "bench"])
195197
.help("Build only this package's library"),
196198
Arg::new("bin")
@@ -215,6 +217,7 @@ To see all the flags the proxied tool accepts run `cargo-{} -- --help`.{}",
215217
.help("Build only the specified bench target"),
216218
Arg::new("release")
217219
.long("release")
220+
.action(ArgAction::SetTrue)
218221
.help("Build artifacts in release mode, with optimizations"),
219222
Arg::new("profile")
220223
.long("profile")
@@ -227,9 +230,11 @@ To see all the flags the proxied tool accepts run `cargo-{} -- --help`.{}",
227230
.help("Space-separated list of features to activate"),
228231
Arg::new("all-features")
229232
.long("all-features")
233+
.action(ArgAction::SetTrue)
230234
.help("Activate all available features"),
231235
Arg::new("no-default-features")
232236
.long("no-default-features")
237+
.action(ArgAction::SetTrue)
233238
.help("Do not activate the `default` feature"),
234239
Arg::new("target")
235240
.long("target")
@@ -248,12 +253,15 @@ To see all the flags the proxied tool accepts run `cargo-{} -- --help`.{}",
248253
.help("Coloring: auto, always, never"),
249254
Arg::new("frozen")
250255
.long("frozen")
256+
.action(ArgAction::SetTrue)
251257
.help("Require Cargo.lock and cache are up to date"),
252258
Arg::new("locked")
253259
.long("locked")
260+
.action(ArgAction::SetTrue)
254261
.help("Require Cargo.lock is up to date"),
255262
Arg::new("offline")
256263
.long("offline")
264+
.action(ArgAction::SetTrue)
257265
.help("Run without accessing the network"),
258266
Arg::new("unstable-features")
259267
.short('Z')
@@ -274,10 +282,10 @@ pub fn run(tool: Tool, matches: ArgMatches) -> Result<i32> {
274282
features.map(|s| s.to_owned()).collect(),
275283
));
276284
}
277-
if matches.contains_id("no-default-features") {
285+
if matches.get_flag("no-default-features") {
278286
metadata_command.features(CargoOpt::NoDefaultFeatures);
279287
}
280-
if matches.contains_id("all-features") {
288+
if matches.get_flag("all-features") {
281289
metadata_command.features(CargoOpt::AllFeatures);
282290
}
283291
let metadata = metadata_command.exec()?;
@@ -364,7 +372,7 @@ pub fn run(tool: Tool, matches: ArgMatches) -> Result<i32> {
364372
// User flags
365373
lltool.args(&tool_args);
366374

367-
if matches.contains_id("verbose") {
375+
if matches.get_count("verbose") > 0 {
368376
eprintln!("{lltool:?}");
369377
}
370378

@@ -401,7 +409,7 @@ fn cargo_build(matches: &ArgMatches, metadata: &Metadata) -> Result<Option<Artif
401409
cargo.arg("build");
402410

403411
let (build_type, verbose) = cargo_build_args(matches, &mut cargo);
404-
let quiet = matches.contains_id("quiet");
412+
let quiet = matches.get_flag("quiet");
405413

406414
cargo.arg("--message-format=json");
407415
cargo.stdout(Stdio::piped());
@@ -454,7 +462,7 @@ fn cargo_build(matches: &ArgMatches, metadata: &Metadata) -> Result<Option<Artif
454462
}
455463

456464
fn cargo_build_args<'a>(matches: &'a ArgMatches, cargo: &mut Command) -> (BuildType<'a>, u64) {
457-
if matches.contains_id("quiet") {
465+
if matches.get_flag("quiet") {
458466
cargo.arg("--quiet");
459467
}
460468

@@ -474,7 +482,7 @@ fn cargo_build_args<'a>(matches: &'a ArgMatches, cargo: &mut Command) -> (BuildT
474482
cargo.arg(jobs);
475483
}
476484

477-
let build_type = if matches.contains_id("lib") {
485+
let build_type = if matches.get_flag("lib") {
478486
cargo.args(["--lib"]);
479487
BuildType::Lib
480488
} else if let Some(bin_name) = matches.get_one::<String>("bin") {
@@ -493,7 +501,7 @@ fn cargo_build_args<'a>(matches: &'a ArgMatches, cargo: &mut Command) -> (BuildT
493501
BuildType::Any
494502
};
495503

496-
if matches.contains_id("release") {
504+
if matches.get_flag("release") {
497505
cargo.arg("--release");
498506
}
499507

@@ -507,10 +515,10 @@ fn cargo_build_args<'a>(matches: &'a ArgMatches, cargo: &mut Command) -> (BuildT
507515
cargo.args(["--features", feature]);
508516
}
509517
}
510-
if matches.contains_id("no-default-features") {
518+
if matches.get_flag("no-default-features") {
511519
cargo.arg("--no-default-features");
512520
}
513-
if matches.contains_id("all-features") {
521+
if matches.get_flag("all-features") {
514522
cargo.arg("--all-features");
515523
}
516524

@@ -530,15 +538,15 @@ fn cargo_build_args<'a>(matches: &'a ArgMatches, cargo: &mut Command) -> (BuildT
530538
cargo.arg(color);
531539
}
532540

533-
if matches.contains_id("frozen") {
541+
if matches.get_flag("frozen") {
534542
cargo.arg("--frozen");
535543
}
536544

537-
if matches.contains_id("locked") {
545+
if matches.get_flag("locked") {
538546
cargo.arg("--locked");
539547
}
540548

541-
if matches.contains_id("offline") {
549+
if matches.get_flag("offline") {
542550
cargo.arg("--offline");
543551
}
544552

0 commit comments

Comments
 (0)