3535import processing .app .debug .TargetBoard ;
3636import processing .app .debug .TargetPackage ;
3737import processing .app .debug .TargetPlatform ;
38+ import processing .app .helpers .CommandlineParser ;
3839import processing .app .helpers .FileUtils ;
3940import processing .app .helpers .GUIUserNotifier ;
4041import 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 /**
0 commit comments