35
35
import processing .app .debug .TargetBoard ;
36
36
import processing .app .debug .TargetPackage ;
37
37
import processing .app .debug .TargetPlatform ;
38
+ import processing .app .helpers .CommandlineParser ;
38
39
import processing .app .helpers .FileUtils ;
39
40
import processing .app .helpers .GUIUserNotifier ;
40
41
import processing .app .helpers .OSUtils ;
@@ -211,8 +212,6 @@ static public File absoluteFile(String path) {
211
212
return BaseNoGui .absoluteFile (path );
212
213
}
213
214
214
- protected static enum ACTION { GUI , NOOP , VERIFY , UPLOAD , GET_PREF };
215
-
216
215
public Base (String [] args ) throws Exception {
217
216
getPlatform ().init ();
218
217
if (OSUtils .isMacOS ())
@@ -237,117 +236,9 @@ public Base(String[] args) throws Exception {
237
236
// Setup board-dependent variables.
238
237
onBoardOrPortChange ();
239
238
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 );
343
240
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 ()) {
351
242
// Correctly resolve relative paths
352
243
File file = absoluteFile (path );
353
244
@@ -363,13 +254,13 @@ public Base(String[] args) throws Exception {
363
254
}
364
255
}
365
256
366
- boolean showEditor = ( action == ACTION . GUI );
367
- if (!forceSavePrefs )
257
+ boolean showEditor = parser . isGuiMode ( );
258
+ if (!parser . isForceSavePrefs () )
368
259
Preferences .setDoSave (showEditor );
369
260
if (handleOpen (file , nextEditorLocation (), showEditor ) == null ) {
370
261
String mess = I18n .format (_ ("Failed to open sketch: \" {0}\" " ), path );
371
262
// Open failure is fatal in upload/verify mode
372
- if (action == ACTION . VERIFY || action == ACTION . UPLOAD )
263
+ if (parser . isVerifyOrUploadMode () )
373
264
showError (null , mess , 2 );
374
265
else
375
266
showWarning (null , mess , null );
@@ -381,20 +272,18 @@ public Base(String[] args) throws Exception {
381
272
// them.
382
273
Preferences .save ();
383
274
384
- switch (action ) {
385
- case VERIFY :
386
- case UPLOAD :
275
+ if (parser .isVerifyOrUploadMode ()) {
387
276
// 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 () );
390
279
391
280
// Make sure these verbosity preferences are only for the
392
281
// current session
393
282
Preferences .setDoSave (false );
394
283
395
284
Editor editor = editors .get (0 );
396
285
397
- if (action == ACTION . UPLOAD ) {
286
+ if (parser . isUploadMode () ) {
398
287
// Build and upload
399
288
editor .exportHandler .run ();
400
289
} else {
@@ -409,8 +298,8 @@ public Base(String[] args) throws Exception {
409
298
410
299
// No errors exit gracefully
411
300
System .exit (0 );
412
- break ;
413
- case GUI :
301
+ }
302
+ else if ( parser . isGuiMode ()) {
414
303
// Check if there were previously opened sketches to be restored
415
304
restoreSketches ();
416
305
@@ -423,29 +312,20 @@ public Base(String[] args) throws Exception {
423
312
if (Preferences .getBoolean ("update.check" )) {
424
313
new UpdateCheck (this );
425
314
}
426
- break ;
427
- case NOOP :
315
+ }
316
+ else if ( parser . isNoOpMode ()) {
428
317
// Do nothing (intended for only changing preferences)
429
318
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 );
433
322
if (value != null ) {
434
323
System .out .println (value );
435
324
System .exit (0 );
436
325
} else {
437
326
System .exit (4 );
438
327
}
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
+ }
449
329
}
450
330
451
331
/**
0 commit comments