Skip to content

Commit 5ef0830

Browse files
Merge #833
833: Fix error message with cross-util with invalid command. r=Emilgardis a=Alexhuszagh Changes the error message to properly report the invalid command, when an invalid command is provided. Co-authored-by: Alex Huszagh <[email protected]>
2 parents 05c4490 + ae5c1fb commit 5ef0830

File tree

2 files changed

+20
-4
lines changed

2 files changed

+20
-4
lines changed

src/bin/cross-util.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#![deny(missing_debug_implementations, rust_2018_idioms)]
22

3-
use clap::{Parser, Subcommand};
3+
use clap::{CommandFactory, Parser, Subcommand};
44

55
mod commands;
66

@@ -14,6 +14,13 @@ struct Cli {
1414
command: Commands,
1515
}
1616

17+
// hidden implied parser so we can get matches without recursion.
18+
#[derive(Parser, Debug)]
19+
struct CliHidden {
20+
#[clap(subcommand)]
21+
command: Commands,
22+
}
23+
1724
#[derive(Subcommand, Debug)]
1825
enum Commands {
1926
/// List cross images in local storage.
@@ -25,7 +32,8 @@ fn is_toolchain(toolchain: &str) -> cross::Result<String> {
2532
if toolchain.starts_with('+') {
2633
Ok(toolchain.chars().skip(1).collect())
2734
} else {
28-
eyre::bail!("not a toolchain")
35+
let _ = <CliHidden as CommandFactory>::command().get_matches();
36+
unreachable!();
2937
}
3038
}
3139

xtask/src/main.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ pub mod util;
1010
use std::path::PathBuf;
1111

1212
use ci::CiJob;
13-
use clap::{Parser, Subcommand};
13+
use clap::{CommandFactory, Parser, Subcommand};
1414
use util::ImageTarget;
1515

1616
use self::build_docker_image::BuildDockerImage;
@@ -28,6 +28,13 @@ struct Cli {
2828
command: Commands,
2929
}
3030

31+
// hidden implied parser so we can get matches without recursion.
32+
#[derive(Parser, Debug)]
33+
struct CliHidden {
34+
#[clap(subcommand)]
35+
command: Commands,
36+
}
37+
3138
#[derive(Subcommand, Debug)]
3239
enum Commands {
3340
/// Extract and print info for targets.
@@ -49,7 +56,8 @@ fn is_toolchain(toolchain: &str) -> cross::Result<String> {
4956
if toolchain.starts_with('+') {
5057
Ok(toolchain.chars().skip(1).collect())
5158
} else {
52-
eyre::bail!("not a toolchain")
59+
let _ = <CliHidden as CommandFactory>::command().get_matches();
60+
unreachable!();
5361
}
5462
}
5563

0 commit comments

Comments
 (0)