Skip to content

Commit 92aa64f

Browse files
Use an "action" enum when processing commandline arguments
Previously, two separate booleans (doUpload and doVerify) were used. However, since it always makes sense to specify only one of them, it makes more sense to keep a single action enum variable, which slightly simplifies the code (especially when more actions are added later). Additionally, an error is now shown when both --verify and --upload are specified on the commandline.
1 parent b2500b3 commit 92aa64f

File tree

1 file changed

+20
-13
lines changed

1 file changed

+20
-13
lines changed

app/src/processing/app/Base.java

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,7 @@ static protected void initRequirements() {
268268
}
269269

270270

271+
protected static enum ACTION { GUI, VERIFY, UPLOAD };
271272
public Base(String[] args) throws Exception {
272273
platform.init(this);
273274

@@ -318,23 +319,29 @@ public Base(String[] args) throws Exception {
318319
// Setup board-dependent variables.
319320
onBoardOrPortChange();
320321

321-
boolean doUpload = false;
322-
boolean doVerify = false;
322+
ACTION action = ACTION.GUI;
323323
boolean doVerboseBuild = false;
324324
boolean doVerboseUpload = false;;
325325
String selectBoard = null;
326326
String selectPort = null;
327327
String currentDirectory = System.getProperty("user.dir");
328328
List<String> filenames = new LinkedList<String>();
329329

330+
// Map of possible actions and corresponding options
331+
final Map<String, ACTION> actions = new HashMap<String, ACTION>();
332+
actions.put("--verify", ACTION.VERIFY);
333+
actions.put("--upload", ACTION.UPLOAD);
334+
330335
// Check if any files were passed in on the command line
331336
for (int i = 0; i < args.length; i++) {
332-
if (args[i].equals("--upload")) {
333-
doUpload = true;
334-
continue;
335-
}
336-
if (args[i].equals("--verify")) {
337-
doVerify = true;
337+
ACTION a = actions.get(args[i]);
338+
if (a != null) {
339+
if (action != ACTION.GUI) {
340+
String[] valid = actions.keySet().toArray(new String[0]);
341+
String mess = I18n.format(_("Can only pass one of: {0}"), PApplet.join(valid, ", "));
342+
showError(null, mess, 3);
343+
}
344+
action = a;
338345
continue;
339346
}
340347
if (args[i].equals("--verbose") || args[i].equals("-v")) {
@@ -391,7 +398,7 @@ public Base(String[] args) throws Exception {
391398
filenames.add(args[i]);
392399
}
393400

394-
if ((doUpload || doVerify) && filenames.size() != 1)
401+
if ((action == ACTION.UPLOAD || action == ACTION.VERIFY) && filenames.size() != 1)
395402
showError(null, _("Must specify exactly one sketch file"), 3);
396403

397404
for (String path: filenames) {
@@ -412,17 +419,17 @@ public Base(String[] args) throws Exception {
412419
path = new File(currentDirectory, path).getAbsolutePath();
413420
}
414421

415-
if (handleOpen(path, nextEditorLocation(), !(doUpload || doVerify)) == null) {
422+
if (handleOpen(path, nextEditorLocation(), !(action == ACTION.UPLOAD || action == ACTION.VERIFY)) == null) {
416423
String mess = I18n.format(_("Failed to open sketch: \"{0}\""), path);
417424
// Open failure is fatal in upload/verify mode
418-
if (doUpload || doVerify)
425+
if (action == ACTION.VERIFY || action == ACTION.UPLOAD)
419426
showError(null, mess, 2);
420427
else
421428
showWarning(null, mess, null);
422429
}
423430
}
424431

425-
if (doUpload || doVerify) {
432+
if (action == ACTION.UPLOAD || action == ACTION.VERIFY) {
426433
// Set verbosity for command line build
427434
Preferences.set("build.verbose", "" + doVerboseBuild);
428435
Preferences.set("upload.verbose", "" + doVerboseUpload);
@@ -432,7 +439,7 @@ public Base(String[] args) throws Exception {
432439
// Do board selection if requested
433440
processBoardArgument(selectBoard);
434441

435-
if (doUpload) {
442+
if (action == ACTION.UPLOAD) {
436443
// Build and upload
437444
if (selectPort != null)
438445
editor.selectSerialPort(selectPort);

0 commit comments

Comments
 (0)