Skip to content

Commit 76b9397

Browse files
George Cosmaeva-cosma
authored andcommitted
chore: add possible values to the 'board' cli option
Signed-off-by: George Cosma <george.cosma@wyliodrin.com>
1 parent 63daf64 commit 76b9397

2 files changed

Lines changed: 39 additions & 15 deletions

File tree

tockloader-cli/src/cli.rs

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
use clap::error::ErrorKind;
66
use clap::{arg, crate_version, value_parser, ArgMatches, Command};
77

8-
use crate::known_boards::KnownBoardNames;
8+
use crate::known_boards::list_known_board_names;
99

1010
/// Create the [command](clap::Command) object which will handle all of the command line arguments.
1111
pub fn make_cli() -> Command {
@@ -65,11 +65,17 @@ fn get_channel_args() -> Vec<clap::Arg> {
6565
let probe_args_ids = get_probe_args_ids().into_iter();
6666
let serial_args_ids = get_serial_args_ids().into_iter();
6767

68+
let known_board_names = list_known_board_names()
69+
.into_iter()
70+
.map(|x| x.to_str())
71+
.collect::<Vec<_>>();
72+
6873
vec![
6974
arg!(--serial "Use the serial bootloader to flash")
7075
.action(clap::ArgAction::SetTrue)
7176
.conflicts_with_all(probe_args_ids.clone().collect::<Vec<_>>()),
7277
arg!(--board <BOARD> "Explicitly specify the board that is being targeted")
78+
.value_parser(known_board_names)
7379
.conflicts_with_all(
7480
serial_args_ids
7581
.clone()
@@ -130,23 +136,10 @@ pub fn validate(cmd: &mut Command, user_options: &ArgMatches) {
130136
{
131137
cmd.error(
132138
ErrorKind::MissingRequiredArgument,
133-
"the argument '--chip' is required for probe connections when not using a known board.",
139+
"the argument '--chip' is required for probe connections. This can be inferred using a known board ('--board').",
134140
)
135141
.exit();
136142
}
137-
138-
// Make sure 'board' is a known board
139-
if let Some(board) = user_options.get_one::<String>("board") {
140-
match KnownBoardNames::from_str(board) {
141-
Some(_) => (),
142-
None => cmd
143-
.error(
144-
ErrorKind::InvalidValue,
145-
"the argument '--board' has an invalid value.",
146-
)
147-
.exit(),
148-
}
149-
}
150143
}
151144

152145
mod test {

tockloader-cli/src/known_boards.rs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#[derive(Debug, PartialEq)]
12
pub enum KnownBoardNames {
23
NucleoF4,
34
MicrobitV2,
@@ -11,4 +12,34 @@ impl KnownBoardNames {
1112
_ => None,
1213
}
1314
}
15+
16+
pub fn to_str(&self) -> &'static str {
17+
match &self {
18+
KnownBoardNames::NucleoF4 => "nucleo-f4",
19+
KnownBoardNames::MicrobitV2 => "microbit-v2",
20+
}
21+
}
22+
}
23+
24+
pub fn list_known_board_names() -> Vec<KnownBoardNames> {
25+
vec![KnownBoardNames::NucleoF4, KnownBoardNames::MicrobitV2]
26+
}
27+
28+
#[cfg(test)]
29+
mod tests {
30+
use super::*;
31+
32+
#[test]
33+
fn from_str_and_to_str_consistent() {
34+
for board in list_known_board_names() {
35+
assert_eq!(KnownBoardNames::from_str(board.to_str()).unwrap(), board);
36+
}
37+
}
38+
39+
#[test]
40+
fn list_known_boards_updated() {
41+
let backup_list = vec![KnownBoardNames::NucleoF4, KnownBoardNames::MicrobitV2];
42+
43+
assert_eq!(list_known_board_names(), backup_list, "If this fails it means that you likely forgot to update `list_known_boards`, and subsequently the `list_known_boards_updated` test");
44+
}
1445
}

0 commit comments

Comments
 (0)