-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Auto merge of #6505 - DamianX:print_examples, r=ehuss
--{example,bin,bench,test} with no argument now lists all available targets ``` cargo run --bin error: "--bin" takes one argument. Available binaries: cargo error: process didn't exit successfully: `target\debug\cargo.exe run --bin` (exit code: 101) ``` Previous PR: #5062 Closes #2548 Notes: - `optional_opt` is a weird name, can someone come up with a better one? - Should I call clap's `usage()` as well when printing the error message?
- Loading branch information
Showing
15 changed files
with
347 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
use crate::core::{Target, Workspace}; | ||
use crate::ops::CompileOptions; | ||
use crate::util::CargoResult; | ||
|
||
use std::fmt::Write; | ||
|
||
fn get_available_targets<'a>( | ||
filter_fn: fn(&Target) -> bool, | ||
ws: &'a Workspace<'_>, | ||
options: &'a CompileOptions<'_>, | ||
) -> CargoResult<Vec<&'a Target>> { | ||
let packages = options.spec.get_packages(ws)?; | ||
|
||
let mut targets: Vec<_> = packages | ||
.into_iter() | ||
.flat_map(|pkg| { | ||
pkg.manifest() | ||
.targets() | ||
.into_iter() | ||
.filter(|target| filter_fn(target)) | ||
}) | ||
.collect(); | ||
|
||
targets.sort(); | ||
|
||
Ok(targets) | ||
} | ||
|
||
fn print_available( | ||
filter_fn: fn(&Target) -> bool, | ||
ws: &Workspace<'_>, | ||
options: &CompileOptions<'_>, | ||
option_name: &str, | ||
plural_name: &str, | ||
) -> CargoResult<()> { | ||
let targets = get_available_targets(filter_fn, ws, options)?; | ||
|
||
let mut output = String::new(); | ||
writeln!(output, "\"{}\" takes one argument.", option_name)?; | ||
|
||
if targets.is_empty() { | ||
writeln!(output, "No {} available.", plural_name)?; | ||
} else { | ||
writeln!(output, "Available {}:", plural_name)?; | ||
for target in targets { | ||
writeln!(output, " {}", target.name())?; | ||
} | ||
} | ||
Err(failure::err_msg(output))? | ||
} | ||
|
||
pub fn print_available_examples( | ||
ws: &Workspace<'_>, | ||
options: &CompileOptions<'_>, | ||
) -> CargoResult<()> { | ||
print_available(Target::is_example, ws, options, "--example", "examples") | ||
} | ||
|
||
pub fn print_available_binaries( | ||
ws: &Workspace<'_>, | ||
options: &CompileOptions<'_>, | ||
) -> CargoResult<()> { | ||
print_available(Target::is_bin, ws, options, "--bin", "binaries") | ||
} | ||
|
||
pub fn print_available_benches( | ||
ws: &Workspace<'_>, | ||
options: &CompileOptions<'_>, | ||
) -> CargoResult<()> { | ||
print_available(Target::is_bench, ws, options, "--bench", "benches") | ||
} | ||
|
||
pub fn print_available_tests(ws: &Workspace<'_>, options: &CompileOptions<'_>) -> CargoResult<()> { | ||
print_available(Target::is_test, ws, options, "--test", "tests") | ||
} |
Oops, something went wrong.