Skip to content

Commit 59b9837

Browse files
committed
west build: display closest matching board names on error
This commit modifies the 'west build' command to display the closest matching boards when an invalid board is specified, making it easier for users to find a typo in the used board name. The user is also instructed to run 'west boards' if he wants to get the full list of available boards. Signed-off-by: Luca Burelli <l.burelli@arduino.cc>
1 parent 77c2c45 commit 59b9837

File tree

2 files changed

+15
-5
lines changed

2 files changed

+15
-5
lines changed

cmake/modules/boards.cmake

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -218,10 +218,9 @@ elseif(BOARD_DIR)
218218
"Please run a pristine build."
219219
)
220220
else()
221-
message("No board named '${BOARD}' found.\n\n"
222-
"Please choose one of the following boards:\n"
223-
)
224-
execute_process(${list_boards_commands})
221+
message("No board named '${BOARD}' found. Did you mean:\n")
222+
execute_process(${list_boards_commands} --fuzzy-match ${BOARD})
223+
message("\nRun 'west boards' for the full list.")
225224
unset(CACHED_BOARD CACHE)
226225
message(FATAL_ERROR "Invalid BOARD; see above.")
227226
endif()

scripts/list_boards.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import argparse
77
from collections import defaultdict, Counter
88
from dataclasses import dataclass, field
9+
import difflib
910
import itertools
1011
from pathlib import Path
1112
import pykwalify.core
@@ -372,7 +373,9 @@ def add_args(parser):
372373
parser.add_argument("--board", dest='board', default=None,
373374
help='lookup the specific board, fail if not found')
374375
parser.add_argument("--board-dir", default=[], type=Path, action='append',
375-
help='Only look for boards at the specific location')
376+
help='only look for boards at the specific location')
377+
parser.add_argument("--fuzzy-match", default=None,
378+
help='lookup boards similar to the given board name')
376379

377380

378381
def add_args_formatting(parser):
@@ -414,6 +417,9 @@ def board_v2_qualifiers_csv(board):
414417

415418
def dump_v2_boards(args):
416419
boards = find_v2_boards(args)
420+
if args.fuzzy_match is not None:
421+
close_boards = difflib.get_close_matches(args.fuzzy_match, boards.keys())
422+
boards = {b: boards[b] for b in close_boards}
417423

418424
for b in boards.values():
419425
qualifiers_list = board_v2_qualifiers(b)
@@ -441,6 +447,11 @@ def dump_v2_boards(args):
441447
def dump_boards(args):
442448
arch2boards = find_arch2boards(args)
443449
for arch, boards in arch2boards.items():
450+
if args.fuzzy_match is not None:
451+
close_boards = difflib.get_close_matches(args.fuzzy_match, [b.name for b in boards])
452+
if not close_boards:
453+
continue
454+
boards = [b for b in boards if b.name in close_boards]
444455
if args.cmakeformat is None:
445456
print(f'{arch}:')
446457
for board in boards:

0 commit comments

Comments
 (0)