Skip to content

Commit 84d10a8

Browse files
author
Federico Fissore
committed
Some CLI args are parsed before initializing packages. --board argument is parsed after. Fixes #3261
1 parent 455fecf commit 84d10a8

File tree

3 files changed

+37
-22
lines changed

3 files changed

+37
-22
lines changed

app/src/processing/app/Base.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,9 @@ public Base(String[] args) throws Exception {
275275
BaseNoGui.notifier = new GUIUserNotifier(this);
276276
this.recentSketchesMenuItems = new LinkedList<JMenuItem>();
277277

278+
CommandlineParser parser = new CommandlineParser(args);
279+
parser.parseArgumentsPhase1();
280+
278281
BaseNoGui.checkInstallationFolder();
279282

280283
String sketchbookPath = BaseNoGui.getSketchbookPath();
@@ -302,7 +305,7 @@ public Base(String[] args) throws Exception {
302305
this.pdeKeywords = new PdeKeywords();
303306
this.pdeKeywords.reload();
304307

305-
CommandlineParser parser = CommandlineParser.newCommandlineParser(args);
308+
parser.parseArgumentsPhase2();
306309

307310
for (String path : parser.getFilenames()) {
308311
// Correctly resolve relative paths

arduino-core/src/processing/app/BaseNoGui.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -427,6 +427,9 @@ static public String[] headerListFromIncludePath(File path) throws IOException {
427427
}
428428

429429
static public void init(String[] args) throws Exception {
430+
CommandlineParser parser = new CommandlineParser(args);
431+
parser.parseArgumentsPhase1();
432+
430433
String sketchbookPath = getSketchbookPath();
431434

432435
// If no path is set, get the default sketchbook folder for this platform
@@ -436,13 +439,13 @@ static public void init(String[] args) throws Exception {
436439
else
437440
showError(_("No sketchbook"), _("Sketchbook path not defined"), null);
438441
}
439-
442+
440443
BaseNoGui.initPackages();
441444

442445
// Setup board-dependent variables.
443446
onBoardOrPortChange();
444-
445-
CommandlineParser parser = CommandlineParser.newCommandlineParser(args);
447+
448+
parser.parseArgumentsPhase2();
446449

447450
for (String path: parser.getFilenames()) {
448451
// Correctly resolve relative paths

arduino-core/src/processing/app/helpers/CommandlineParser.java

Lines changed: 27 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,7 @@
99
import processing.app.legacy.PApplet;
1010

1111
import java.io.File;
12-
import java.util.HashMap;
13-
import java.util.LinkedList;
14-
import java.util.List;
15-
import java.util.Map;
12+
import java.util.*;
1613

1714
import static processing.app.I18n._;
1815

@@ -32,6 +29,8 @@ private enum ACTION {
3229
}
3330
}
3431

32+
private final String[] args;
33+
private final Map<String, ACTION> actions;
3534
private ACTION action = ACTION.GUI;
3635
private boolean doVerboseBuild = false;
3736
private boolean doVerboseUpload = false;
@@ -44,30 +43,24 @@ private enum ACTION {
4443
private String libraryToInstall;
4544
private List<String> filenames = new LinkedList<String>();
4645

47-
public static CommandlineParser newCommandlineParser(String[] args) {
48-
return new CommandlineParser(args);
49-
}
50-
51-
private CommandlineParser(String[] args) {
52-
parseArguments(args);
53-
checkAction();
54-
}
46+
public CommandlineParser(String[] args) {
47+
this.args = args;
5548

56-
private void parseArguments(String[] args) {
57-
// Map of possible actions and corresponding options
58-
final Map<String, ACTION> actions = new HashMap<String, ACTION>();
49+
actions = new HashMap<String, ACTION>();
5950
actions.put("--verify", ACTION.VERIFY);
6051
actions.put("--upload", ACTION.UPLOAD);
6152
actions.put("--get-pref", ACTION.GET_PREF);
6253
actions.put("--install-boards", ACTION.INSTALL_BOARD);
6354
actions.put("--install-library", ACTION.INSTALL_LIBRARY);
55+
}
6456

65-
// Check if any files were passed in on the command line
57+
public void parseArgumentsPhase1() {
6658
for (int i = 0; i < args.length; i++) {
6759
ACTION a = actions.get(args[i]);
6860
if (a != null) {
6961
if (action != ACTION.GUI && action != ACTION.NOOP) {
70-
String[] valid = actions.keySet().toArray(new String[0]);
62+
Set<String> strings = actions.keySet();
63+
String[] valid = strings.toArray(new String[strings.size()]);
7164
String mess = I18n.format(_("Can only pass one of: {0}"), PApplet.join(valid, ", "));
7265
BaseNoGui.showError(null, mess, 3);
7366
}
@@ -139,7 +132,6 @@ private void parseArguments(String[] args) {
139132
i++;
140133
if (i >= args.length)
141134
BaseNoGui.showError(null, _("Argument required for --board"), 3);
142-
processBoardArgument(args[i]);
143135
if (action == ACTION.GUI)
144136
action = ACTION.NOOP;
145137
continue;
@@ -200,6 +192,23 @@ private void parseArguments(String[] args) {
200192

201193
filenames.add(args[i]);
202194
}
195+
196+
checkAction();
197+
}
198+
199+
public void parseArgumentsPhase2() {
200+
for (int i = 0; i < args.length; i++) {
201+
if (args[i].equals("--board")) {
202+
i++;
203+
if (i >= args.length) {
204+
BaseNoGui.showError(null, _("Argument required for --board"), 3);
205+
}
206+
processBoardArgument(args[i]);
207+
if (action == ACTION.GUI) {
208+
action = ACTION.NOOP;
209+
}
210+
}
211+
}
203212
}
204213

205214
private void checkAction() {

0 commit comments

Comments
 (0)