11package com .sandwich .koan .cmdline ;
22
3+ import java .util .ArrayList ;
34import java .util .LinkedHashMap ;
4- import java .util .Map ;
5+ import java .util .List ;
56import java .util .Map .Entry ;
7+ import java .util .logging .Logger ;
68
79import com .sandwich .koan .ApplicationSettings ;
8- import com .sandwich .koan .KoanClassLoader ;
910import com .sandwich .koan .constant .ArgumentType ;
1011import com .sandwich .koan .ui .SuitePresenter ;
1112import com .sandwich .koan .util .ApplicationUtils ;
@@ -15,78 +16,45 @@ public class CommandLineArgumentBuilder extends LinkedHashMap<ArgumentType, Comm
1516 private static final long serialVersionUID = 7635285665311420603L ;
1617
1718 public CommandLineArgumentBuilder (String ...args ){
18- for (int index = 0 ; index < args .length ;){
19- String stringArg = args [index ];
20- if (stringArg == null || stringArg .trim ().length () == 0 ){
21- index ++;
22- continue ;
23- }
24- // is incremented in test of ternary
25- String stringArgPlusOne = args .length <= ++index ? null : args [index ];
26- stringArgPlusOne = stringArgPlusOne == null || stringArgPlusOne .trim ().length () == 0
27- ? null : stringArgPlusOne ;
28- ArgumentType argumentType = ArgumentType .findTypeByString (stringArg );
29- ArgumentType argumentTypePlusOne = ArgumentType .findTypeByString (stringArgPlusOne );
30- if (argumentType != null ){ // matches an anticipated argument string
31- // so does next value, must be an argument too, reevaluate w/ index incremented only once above
32- if (argumentTypePlusOne instanceof ArgumentType ){
33- put (argumentType , new CommandLineArgument (argumentType , null ));
34- // intentionally increment past next argument (only one) and
35- // evaluate in next iteration with its following argument
36- // - in other words, argumentType will be argumentTypePlusOne in next iteration
37- continue ;
38- }
39- // ok 2nd argument wasn't a recognized argument type - go
40- // ahead and see if it's a class, if not, it must be a
41- // method - or bogus
42- else if (stringArgPlusOne != null ){
43- guessAtMethodAndClass (this , stringArgPlusOne );
44- }
45- else {
46- put (argumentType , new CommandLineArgument (argumentType , null ));
47- }
48- index ++;
49- continue ;
50- }else if (stringArg != null ){
51- // no flag, but a string - likely class or class and method
52- guessAtMethodAndClass (this , stringArg );
53- // do not increment again, bump stringArgPlusOne into stringArg from prior increment
54- continue ;
55- }
56- }
57- applyAssumedStartupBehaviors ();
58- }
59-
60- private static void guessAtMethodAndClass (
61- Map <ArgumentType , CommandLineArgument > commandLineArguments ,
62- String potentialClassOrMethod ) {
63- boolean hasMethod = commandLineArguments .containsKey (ArgumentType .METHOD_ARG );
64- boolean hasClass = commandLineArguments .containsKey (ArgumentType .CLASS_ARG );
65- try {
66- if (potentialClassOrMethod != null ){
67- if (!hasClass ){
68- commandLineArguments .put (ArgumentType .CLASS_ARG ,
69- new CommandLineArgument (ArgumentType .CLASS_ARG ,
70- KoanClassLoader .getInstance ().loadClass (potentialClassOrMethod ).getName ()));
71- }else if (!hasMethod ){
72- commandLineArguments .put (ArgumentType .METHOD_ARG ,
73- new CommandLineArgument (ArgumentType .METHOD_ARG , potentialClassOrMethod ));
19+ if (args .length == 0 ){
20+ put (ArgumentType .RUN_KOANS , new CommandLineArgument (ArgumentType .RUN_KOANS , new String [0 ]));
21+ } else if (args .length == 1 && ArgumentType .findTypeByString (args [0 ]) == null ){
22+ put (ArgumentType .CLASS_ARG , new CommandLineArgument (ArgumentType .CLASS_ARG , args [0 ]));
23+ } else if (args .length == 2 && ArgumentType .findTypeByString (args [0 ]) == null && ArgumentType .findTypeByString (args [1 ]) == null ){
24+ put (ArgumentType .CLASS_ARG , new CommandLineArgument (ArgumentType .CLASS_ARG , args [0 ]));
25+ put (ArgumentType .METHOD_ARG , new CommandLineArgument (ArgumentType .METHOD_ARG , args [1 ]));
26+ } else {
27+ ArgumentType type = null ;
28+ List <String > params = null ;
29+ for (int index = 0 ; index < args .length ; index ++){
30+ ArgumentType tmpType = ArgumentType .findTypeByString (args [index ]);
31+ if (tmpType == null ){
32+ if (type == null ){
33+ Logger .getAnonymousLogger ().warning ("The argument: " + args [index ] + " is not recognized, it will be ignored" );
34+ }else {
35+ params .add (args [index ]);
36+ }
37+ }else {
38+ if (type == null ){
39+ type = tmpType ;
40+ params = new ArrayList <String >();
41+ }else {
42+ put (type , new CommandLineArgument (type , params .toArray (new String [params .size ()])));
43+ type = null ;
44+ params = new ArrayList <String >();
45+ }
7446 }
7547 }
76- }catch (Exception cnfe2 ){
77- if (!hasMethod ){
78- commandLineArguments .put (ArgumentType .METHOD_ARG ,
79- new CommandLineArgument (ArgumentType .METHOD_ARG , potentialClassOrMethod ));
80- }else {
81- throw new IllegalArgumentException (potentialClassOrMethod
82- + " does not match an expected argument, nor value." );
48+ if (type != null && params != null ){
49+ put (type , new CommandLineArgument (type , params .toArray (new String [params .size ()])));
8350 }
8451 }
52+ applyAssumedStartupBehaviors ();
8553 }
8654
8755 void applyAssumedStartupBehaviors () {
8856 if (ApplicationUtils .isFirstTimeAppHasBeenRun ()){
89- ArgumentType .BACKUP .run (null );
57+ ArgumentType .BACKUP .run (new String [ 0 ] );
9058 ApplicationUtils .getPresenter ().clearMessages ();
9159 }
9260 if (isEmpty () || !containsKey (ArgumentType .RUN_KOANS ) && (
@@ -100,7 +68,7 @@ void applyAssumedStartupBehaviors() {
10068 presenter .displayMessage ("Value: '" +argEntry .getValue ()+"'" );
10169 }
10270 }
103- put (ArgumentType .RUN_KOANS , new CommandLineArgument (ArgumentType .RUN_KOANS , null , true ));
71+ put (ArgumentType .RUN_KOANS , new CommandLineArgument (ArgumentType .RUN_KOANS , true , new String [ 0 ] ));
10472 }
10573 }
10674}
0 commit comments