Skip to content

Commit 6d28e12

Browse files
bitroncmaglie
authored andcommitted
Moved commandline parsing code from Base/BaseNoGui to new CommandlineParser class.
1 parent 035e57e commit 6d28e12

File tree

3 files changed

+264
-195
lines changed

3 files changed

+264
-195
lines changed

app/src/processing/app/Base.java

Lines changed: 18 additions & 138 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
import processing.app.debug.TargetBoard;
3636
import processing.app.debug.TargetPackage;
3737
import processing.app.debug.TargetPlatform;
38+
import processing.app.helpers.CommandlineParser;
3839
import processing.app.helpers.FileUtils;
3940
import processing.app.helpers.GUIUserNotifier;
4041
import processing.app.helpers.OSUtils;
@@ -211,8 +212,6 @@ static public File absoluteFile(String path) {
211212
return BaseNoGui.absoluteFile(path);
212213
}
213214

214-
protected static enum ACTION { GUI, NOOP, VERIFY, UPLOAD, GET_PREF };
215-
216215
public Base(String[] args) throws Exception {
217216
getPlatform().init();
218217
if (OSUtils.isMacOS())
@@ -237,117 +236,9 @@ public Base(String[] args) throws Exception {
237236
// Setup board-dependent variables.
238237
onBoardOrPortChange();
239238

240-
ACTION action = ACTION.GUI;
241-
boolean doVerboseBuild = false;
242-
boolean doVerboseUpload = false;
243-
boolean forceSavePrefs = false;
244-
String getPref = null;
245-
List<String> filenames = new LinkedList<String>();
246-
247-
// Map of possible actions and corresponding options
248-
final Map<String, ACTION> actions = new HashMap<String, ACTION>();
249-
actions.put("--verify", ACTION.VERIFY);
250-
actions.put("--upload", ACTION.UPLOAD);
251-
actions.put("--get-pref", ACTION.GET_PREF);
252-
253-
// Check if any files were passed in on the command line
254-
for (int i = 0; i < args.length; i++) {
255-
ACTION a = actions.get(args[i]);
256-
if (a != null) {
257-
if (action != ACTION.GUI && action != ACTION.NOOP) {
258-
String[] valid = actions.keySet().toArray(new String[0]);
259-
String mess = I18n.format(_("Can only pass one of: {0}"), PApplet.join(valid, ", "));
260-
showError(null, mess, 3);
261-
}
262-
if (a == ACTION.GET_PREF) {
263-
i++;
264-
if (i >= args.length)
265-
showError(null, _("Argument required for --get-pref"), 3);
266-
getPref = args[i];
267-
}
268-
action = a;
269-
continue;
270-
}
271-
if (args[i].equals("--verbose") || args[i].equals("-v")) {
272-
doVerboseBuild = true;
273-
doVerboseUpload = true;
274-
if (action == ACTION.GUI)
275-
action = ACTION.NOOP;
276-
continue;
277-
}
278-
if (args[i].equals("--verbose-build")) {
279-
doVerboseBuild = true;
280-
if (action == ACTION.GUI)
281-
action = ACTION.NOOP;
282-
continue;
283-
}
284-
if (args[i].equals("--verbose-upload")) {
285-
doVerboseUpload = true;
286-
if (action == ACTION.GUI)
287-
action = ACTION.NOOP;
288-
continue;
289-
}
290-
if (args[i].equals("--board")) {
291-
i++;
292-
if (i >= args.length)
293-
showError(null, _("Argument required for --board"), 3);
294-
processBoardArgument(args[i]);
295-
if (action == ACTION.GUI)
296-
action = ACTION.NOOP;
297-
continue;
298-
}
299-
if (args[i].equals("--port")) {
300-
i++;
301-
if (i >= args.length)
302-
showError(null, _("Argument required for --port"), 3);
303-
selectSerialPort(args[i]);
304-
if (action == ACTION.GUI)
305-
action = ACTION.NOOP;
306-
continue;
307-
}
308-
if (args[i].equals("--curdir")) {
309-
i++;
310-
if (i >= args.length)
311-
showError(null, _("Argument required for --curdir"), 3);
312-
// Argument should be already processed by Base.main(...)
313-
continue;
314-
}
315-
if (args[i].equals("--pref")) {
316-
i++;
317-
if (i >= args.length)
318-
showError(null, _("Argument required for --pref"), 3);
319-
processPrefArgument(args[i]);
320-
if (action == ACTION.GUI)
321-
action = ACTION.NOOP;
322-
continue;
323-
}
324-
if (args[i].equals("--save-prefs")) {
325-
forceSavePrefs = true;
326-
continue;
327-
}
328-
if (args[i].equals("--preferences-file")) {
329-
i++;
330-
if (i >= args.length)
331-
showError(null, _("Argument required for --preferences-file"), 3);
332-
// Argument should be already processed by Base.main(...)
333-
continue;
334-
}
335-
if (args[i].startsWith("--"))
336-
showError(null, I18n.format(_("unknown option: {0}"), args[i]), 3);
337-
338-
filenames.add(args[i]);
339-
}
340-
341-
if ((action == ACTION.UPLOAD || action == ACTION.VERIFY) && filenames.size() != 1)
342-
showError(null, _("Must specify exactly one sketch file"), 3);
239+
CommandlineParser parser = CommandlineParser.newCommandlineParser(args);
343240

344-
if ((action == ACTION.NOOP || action == ACTION.GET_PREF) && filenames.size() != 0)
345-
showError(null, _("Cannot specify any sketch files"), 3);
346-
347-
if ((action != ACTION.UPLOAD && action != ACTION.VERIFY) && (doVerboseBuild || doVerboseUpload))
348-
showError(null, _("--verbose, --verbose-upload and --verbose-build can only be used together with --verify or --upload"), 3);
349-
350-
for (String path: filenames) {
241+
for (String path: parser.getFilenames()) {
351242
// Correctly resolve relative paths
352243
File file = absoluteFile(path);
353244

@@ -363,13 +254,13 @@ public Base(String[] args) throws Exception {
363254
}
364255
}
365256

366-
boolean showEditor = (action == ACTION.GUI);
367-
if (!forceSavePrefs)
257+
boolean showEditor = parser.isGuiMode();
258+
if (!parser.isForceSavePrefs())
368259
Preferences.setDoSave(showEditor);
369260
if (handleOpen(file, nextEditorLocation(), showEditor) == null) {
370261
String mess = I18n.format(_("Failed to open sketch: \"{0}\""), path);
371262
// Open failure is fatal in upload/verify mode
372-
if (action == ACTION.VERIFY || action == ACTION.UPLOAD)
263+
if (parser.isVerifyOrUploadMode())
373264
showError(null, mess, 2);
374265
else
375266
showWarning(null, mess, null);
@@ -381,20 +272,18 @@ public Base(String[] args) throws Exception {
381272
// them.
382273
Preferences.save();
383274

384-
switch (action) {
385-
case VERIFY:
386-
case UPLOAD:
275+
if (parser.isVerifyOrUploadMode()) {
387276
// Set verbosity for command line build
388-
Preferences.set("build.verbose", "" + doVerboseBuild);
389-
Preferences.set("upload.verbose", "" + doVerboseUpload);
277+
Preferences.set("build.verbose", "" + parser.isDoVerboseBuild());
278+
Preferences.set("upload.verbose", "" + parser.isDoVerboseUpload());
390279

391280
// Make sure these verbosity preferences are only for the
392281
// current session
393282
Preferences.setDoSave(false);
394283

395284
Editor editor = editors.get(0);
396285

397-
if (action == ACTION.UPLOAD) {
286+
if (parser.isUploadMode()) {
398287
// Build and upload
399288
editor.exportHandler.run();
400289
} else {
@@ -409,8 +298,8 @@ public Base(String[] args) throws Exception {
409298

410299
// No errors exit gracefully
411300
System.exit(0);
412-
break;
413-
case GUI:
301+
}
302+
else if (parser.isGuiMode()) {
414303
// Check if there were previously opened sketches to be restored
415304
restoreSketches();
416305

@@ -423,29 +312,20 @@ public Base(String[] args) throws Exception {
423312
if (Preferences.getBoolean("update.check")) {
424313
new UpdateCheck(this);
425314
}
426-
break;
427-
case NOOP:
315+
}
316+
else if (parser.isNoOpMode()) {
428317
// Do nothing (intended for only changing preferences)
429318
System.exit(0);
430-
break;
431-
case GET_PREF:
432-
String value = Preferences.get(getPref, null);
319+
}
320+
else if (parser.isGetPrefMode()) {
321+
String value = Preferences.get(parser.getGetPref(), null);
433322
if (value != null) {
434323
System.out.println(value);
435324
System.exit(0);
436325
} else {
437326
System.exit(4);
438327
}
439-
break;
440-
}
441-
}
442-
443-
protected void processBoardArgument(String selectBoard) {
444-
BaseNoGui.processBoardArgument(selectBoard);
445-
}
446-
447-
protected void processPrefArgument(String arg) {
448-
BaseNoGui.processPrefArgument(arg);
328+
}
449329
}
450330

451331
/**

app/src/processing/app/BaseNoGui.java

Lines changed: 1 addition & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -553,62 +553,6 @@ static public void prescanParameters(String args[]) {
553553
PreferencesData.init(absoluteFile(preferencesFile));
554554
}
555555

556-
static protected void processBoardArgument(String selectBoard) {
557-
// No board selected? Nothing to do
558-
if (selectBoard == null)
559-
return;
560-
561-
String[] split = selectBoard.split(":", 4);
562-
563-
if (split.length < 3) {
564-
showError(null, I18n.format(_("{0}: Invalid board name, it should be of the form \"package:arch:board\" or \"package:arch:board:options\""), selectBoard), 3);
565-
}
566-
567-
TargetPackage targetPackage = getTargetPackage(split[0]);
568-
if (targetPackage == null) {
569-
showError(null, I18n.format(_("{0}: Unknown package"), split[0]), 3);
570-
}
571-
572-
TargetPlatform targetPlatform = targetPackage.get(split[1]);
573-
if (targetPlatform == null) {
574-
showError(null, I18n.format(_("{0}: Unknown architecture"), split[1]), 3);
575-
}
576-
577-
TargetBoard targetBoard = targetPlatform.getBoard(split[2]);
578-
if (targetBoard == null) {
579-
showError(null, I18n.format(_("{0}: Unknown board"), split[2]), 3);
580-
}
581-
582-
selectBoard(targetBoard);
583-
584-
if (split.length > 3) {
585-
String[] options = split[3].split(",");
586-
for (String option : options) {
587-
String[] keyValue = option.split("=", 2);
588-
589-
if (keyValue.length != 2)
590-
showError(null, I18n.format(_("{0}: Invalid option, should be of the form \"name=value\""), option, targetBoard.getId()), 3);
591-
String key = keyValue[0].trim();
592-
String value = keyValue[1].trim();
593-
594-
if (!targetBoard.hasMenu(key))
595-
showError(null, I18n.format(_("{0}: Invalid option for board \"{1}\""), key, targetBoard.getId()), 3);
596-
if (targetBoard.getMenuLabel(key, value) == null)
597-
showError(null, I18n.format(_("{0}: Invalid option for \"{1}\" option for board \"{2}\""), value, key, targetBoard.getId()), 3);
598-
599-
PreferencesData.set("custom_" + key, targetBoard.getId() + "_" + value);
600-
}
601-
}
602-
}
603-
604-
static protected void processPrefArgument(String arg) {
605-
String[] split = arg.split("=", 2);
606-
if (split.length != 2 || split[0].isEmpty())
607-
showError(null, I18n.format(_("{0}: Invalid argument to --pref, should be of the form \"pref=value\""), arg), 3);
608-
609-
PreferencesData.set(split[0], split[1]);
610-
}
611-
612556
/**
613557
* Recursively remove all files within a directory,
614558
* used with removeDir(), or when the contents of a dir
@@ -759,7 +703,7 @@ static public LibraryList scanLibraries(File folder) throws IOException {
759703
return res;
760704
}
761705

762-
static protected void selectBoard(TargetBoard targetBoard) {
706+
static public void selectBoard(TargetBoard targetBoard) {
763707
TargetPlatform targetPlatform = targetBoard.getContainerPlatform();
764708
TargetPackage targetPackage = targetPlatform.getContainerPackage();
765709

0 commit comments

Comments
 (0)