Skip to content

Commit

Permalink
Some CLI args are parsed before initializing packages. --board argume…
Browse files Browse the repository at this point in the history
…nt is parsed after. Fixes esp8266#3261
  • Loading branch information
Federico Fissore committed Jun 5, 2015
1 parent 455fecf commit 84d10a8
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 22 deletions.
5 changes: 4 additions & 1 deletion app/src/processing/app/Base.java
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,9 @@ public Base(String[] args) throws Exception {
BaseNoGui.notifier = new GUIUserNotifier(this);
this.recentSketchesMenuItems = new LinkedList<JMenuItem>();

CommandlineParser parser = new CommandlineParser(args);
parser.parseArgumentsPhase1();

BaseNoGui.checkInstallationFolder();

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

CommandlineParser parser = CommandlineParser.newCommandlineParser(args);
parser.parseArgumentsPhase2();

for (String path : parser.getFilenames()) {
// Correctly resolve relative paths
Expand Down
9 changes: 6 additions & 3 deletions arduino-core/src/processing/app/BaseNoGui.java
Original file line number Diff line number Diff line change
Expand Up @@ -427,6 +427,9 @@ static public String[] headerListFromIncludePath(File path) throws IOException {
}

static public void init(String[] args) throws Exception {
CommandlineParser parser = new CommandlineParser(args);
parser.parseArgumentsPhase1();

String sketchbookPath = getSketchbookPath();

// If no path is set, get the default sketchbook folder for this platform
Expand All @@ -436,13 +439,13 @@ static public void init(String[] args) throws Exception {
else
showError(_("No sketchbook"), _("Sketchbook path not defined"), null);
}

BaseNoGui.initPackages();

// Setup board-dependent variables.
onBoardOrPortChange();
CommandlineParser parser = CommandlineParser.newCommandlineParser(args);

parser.parseArgumentsPhase2();

for (String path: parser.getFilenames()) {
// Correctly resolve relative paths
Expand Down
45 changes: 27 additions & 18 deletions arduino-core/src/processing/app/helpers/CommandlineParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,7 @@
import processing.app.legacy.PApplet;

import java.io.File;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.*;

import static processing.app.I18n._;

Expand All @@ -32,6 +29,8 @@ private enum ACTION {
}
}

private final String[] args;
private final Map<String, ACTION> actions;
private ACTION action = ACTION.GUI;
private boolean doVerboseBuild = false;
private boolean doVerboseUpload = false;
Expand All @@ -44,30 +43,24 @@ private enum ACTION {
private String libraryToInstall;
private List<String> filenames = new LinkedList<String>();

public static CommandlineParser newCommandlineParser(String[] args) {
return new CommandlineParser(args);
}

private CommandlineParser(String[] args) {
parseArguments(args);
checkAction();
}
public CommandlineParser(String[] args) {
this.args = args;

private void parseArguments(String[] args) {
// Map of possible actions and corresponding options
final Map<String, ACTION> actions = new HashMap<String, ACTION>();
actions = new HashMap<String, ACTION>();
actions.put("--verify", ACTION.VERIFY);
actions.put("--upload", ACTION.UPLOAD);
actions.put("--get-pref", ACTION.GET_PREF);
actions.put("--install-boards", ACTION.INSTALL_BOARD);
actions.put("--install-library", ACTION.INSTALL_LIBRARY);
}

// Check if any files were passed in on the command line
public void parseArgumentsPhase1() {
for (int i = 0; i < args.length; i++) {
ACTION a = actions.get(args[i]);
if (a != null) {
if (action != ACTION.GUI && action != ACTION.NOOP) {
String[] valid = actions.keySet().toArray(new String[0]);
Set<String> strings = actions.keySet();
String[] valid = strings.toArray(new String[strings.size()]);
String mess = I18n.format(_("Can only pass one of: {0}"), PApplet.join(valid, ", "));
BaseNoGui.showError(null, mess, 3);
}
Expand Down Expand Up @@ -139,7 +132,6 @@ private void parseArguments(String[] args) {
i++;
if (i >= args.length)
BaseNoGui.showError(null, _("Argument required for --board"), 3);
processBoardArgument(args[i]);
if (action == ACTION.GUI)
action = ACTION.NOOP;
continue;
Expand Down Expand Up @@ -200,6 +192,23 @@ private void parseArguments(String[] args) {

filenames.add(args[i]);
}

checkAction();
}

public void parseArgumentsPhase2() {
for (int i = 0; i < args.length; i++) {
if (args[i].equals("--board")) {
i++;
if (i >= args.length) {
BaseNoGui.showError(null, _("Argument required for --board"), 3);
}
processBoardArgument(args[i]);
if (action == ACTION.GUI) {
action = ACTION.NOOP;
}
}
}
}

private void checkAction() {
Expand Down

0 comments on commit 84d10a8

Please sign in to comment.