Skip to content

Commit 0ea5509

Browse files
Handle errors in the --board parameter
Previously, it would just raise nullpointer or index out of bounds exceptions when the --board paramater was wrong.
1 parent 46c930c commit 0ea5509

File tree

1 file changed

+59
-15
lines changed

1 file changed

+59
-15
lines changed

app/src/processing/app/Base.java

Lines changed: 59 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -395,21 +395,8 @@ public Base(String[] args) throws Exception {
395395
Thread.sleep(10);
396396

397397
// Do board selection if requested
398-
if (selectBoard != null) {
399-
String[] split = selectBoard.split(":");
400-
401-
TargetBoard targetBoard = getTargetPlatform(split[0], split[1]).getBoard(split[2]);
402-
selectBoard(targetBoard);
403-
404-
if (split.length > 3) {
405-
String[] customsParts = split[3].split(",");
406-
for (String customParts : customsParts) {
407-
String[] keyValue = customParts.split("=");
408-
Preferences.set("custom_" + keyValue[0].trim(), targetBoard.getId() + "_" + keyValue[1].trim());
409-
}
410-
}
411-
}
412-
398+
processBoardArgument(selectBoard);
399+
413400
if (doUpload) {
414401
// Build and upload
415402
if (selectPort != null)
@@ -444,6 +431,54 @@ public Base(String[] args) throws Exception {
444431
}
445432
}
446433

434+
protected void processBoardArgument(String selectBoard) {
435+
// No board selected? Nothing to do
436+
if (selectBoard == null)
437+
return;
438+
439+
String[] split = selectBoard.split(":", 4);
440+
441+
if (split.length < 3) {
442+
showError(null, I18n.format(_("{0}: Invalid board name, it should be of the form \"package:arch:board\" or \"package:arch:board:options\""), selectBoard), null);
443+
}
444+
445+
TargetPackage targetPackage = getTargetPackage(split[0]);
446+
if (targetPackage == null) {
447+
showError(null, I18n.format(_("{0}: Unknown package"), split[0]), null);
448+
}
449+
450+
TargetPlatform targetPlatform = targetPackage.get(split[1]);
451+
if (targetPlatform == null) {
452+
showError(null, I18n.format(_("{0}: Unknown architecture"), split[1]), null);
453+
}
454+
455+
TargetBoard targetBoard = targetPlatform.getBoard(split[2]);
456+
if (targetBoard == null) {
457+
showError(null, I18n.format(_("{0}: Unknown board"), split[2]), null);
458+
}
459+
460+
selectBoard(targetBoard);
461+
462+
if (split.length > 3) {
463+
String[] options = split[3].split(",");
464+
for (String option : options) {
465+
String[] keyValue = option.split("=", 2);
466+
467+
if (keyValue.length != 2)
468+
showError(null, I18n.format(_("{0}: Invalid option, should be of the form \"name=value\""), option, targetBoard.getId()), null);
469+
String key = keyValue[0].trim();
470+
String value = keyValue[1].trim();
471+
472+
if (!targetBoard.hasMenu(key))
473+
showError(null, I18n.format(_("{0}: Invalid option for board \"{1}\""), key, targetBoard.getId()), null);
474+
if (targetBoard.getMenuLabel(key, value) == null)
475+
showError(null, I18n.format(_("{0}: Invalid option for \"{1}\" option for board \"{2}\""), value, key, targetBoard.getId()), null);
476+
477+
Preferences.set("custom_" + key, targetBoard.getId() + "_" + value);
478+
}
479+
}
480+
}
481+
447482
public Map<String, Map<String, Object>> getBoardsViaNetwork() {
448483
return new HashMap<String, Map<String, Object>>(boardsViaNetwork);
449484
}
@@ -2002,6 +2037,15 @@ static public String getAvrBasePath() {
20022037
return path;
20032038
}
20042039

2040+
/**
2041+
* Returns a specific TargetPackage
2042+
*
2043+
* @param packageName
2044+
* @return
2045+
*/
2046+
static public TargetPackage getTargetPackage(String packageName) {
2047+
return packages.get(packageName);
2048+
}
20052049

20062050
/**
20072051
* Returns the currently selected TargetPlatform.

0 commit comments

Comments
 (0)