| 1 | package felix.util; |
| 2 | |
| 3 | import org.kohsuke.args4j.CmdLineException; |
| 4 | import org.kohsuke.args4j.CmdLineParser; |
| 5 | |
| 6 | import tuffy.parse.CommandOptions; |
| 7 | import tuffy.util.Config; |
| 8 | import tuffy.util.UIMan; |
| 9 | import felix.parser.FelixCommandOptions; |
| 10 | |
| 11 | /** |
| 12 | * Container of user-interface utilities. |
| 13 | */ |
| 14 | public class FelixUIMan extends UIMan{ |
| 15 | |
| 16 | /** |
| 17 | * Returns string representation of given array. |
| 18 | * @param _array |
| 19 | * @return |
| 20 | */ |
| 21 | public static String joinArray(int[] _array){ |
| 22 | if(_array.length == 0){ |
| 23 | return ""; |
| 24 | } |
| 25 | |
| 26 | String ret = ""; |
| 27 | for(int i=0;i<_array.length - 1;i++){ |
| 28 | ret = ret + _array[i] + ", "; |
| 29 | } |
| 30 | ret = ret + _array[_array.length-1]; |
| 31 | return ret; |
| 32 | } |
| 33 | |
| 34 | /** |
| 35 | * Println with verbose level, indent. |
| 36 | * @param verbose |
| 37 | * @param indent |
| 38 | * @param strings |
| 39 | */ |
| 40 | public static void println(int verbose, int indent, String... strings){ |
| 41 | if(FelixConfig.verbose_level >= verbose){ |
| 42 | |
| 43 | FelixConfig.currentIndent = indent; |
| 44 | if(silent) return; |
| 45 | |
| 46 | |
| 47 | if(Config.console_line_header != null){ |
| 48 | System.out.print("@" + Config.console_line_header + " "); |
| 49 | } |
| 50 | |
| 51 | for(String s : strings){ |
| 52 | System.out.print(s); |
| 53 | writeToDribbleFile(s); |
| 54 | } |
| 55 | System.out.println(); |
| 56 | writeToDribbleFile("\n"); |
| 57 | FelixConfig.currentIndent = 0; |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | /** |
| 62 | * Println with verbose level. |
| 63 | * @param verbose |
| 64 | * @param strings |
| 65 | */ |
| 66 | public static void println(int verbose, String... strings){ |
| 67 | if(FelixConfig.verbose_level >= verbose){ |
| 68 | if(silent) return; |
| 69 | |
| 70 | if(Config.console_line_header != null){ |
| 71 | System.out.print("@" + Config.console_line_header + " "); |
| 72 | } |
| 73 | |
| 74 | for(String s : strings){ |
| 75 | System.out.print(s); |
| 76 | writeToDribbleFile(s); |
| 77 | } |
| 78 | System.out.println(); |
| 79 | writeToDribbleFile("\n"); |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | /** |
| 84 | * Print object with verbose level, indent. |
| 85 | * @param verbose |
| 86 | * @param indent |
| 87 | * @param obj |
| 88 | */ |
| 89 | public static <A> void printobj(int verbose, int indent, A obj){ |
| 90 | if(FelixConfig.verbose_level >= verbose){ |
| 91 | FelixConfig.currentIndent = indent; |
| 92 | if(silent) return; |
| 93 | FelixUIMan.println(verbose, indent, obj.toString()); |
| 94 | System.out.println(); |
| 95 | writeToDribbleFile("\n"); |
| 96 | FelixConfig.currentIndent = 0; |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | /** |
| 101 | * Processes command line options. |
| 102 | * @param opt |
| 103 | * @return |
| 104 | */ |
| 105 | public static FelixCommandOptions processOptions(FelixCommandOptions opt){ |
| 106 | |
| 107 | CommandOptions topt = UIMan.processOptions(opt); |
| 108 | if(topt == null) return null; |
| 109 | opt = (FelixCommandOptions) topt; |
| 110 | FelixConfig.explainMode = opt.explainMode; |
| 111 | //TODO set up more options |
| 112 | return opt; |
| 113 | |
| 114 | } |
| 115 | |
| 116 | /** |
| 117 | * Parses command line options |
| 118 | * @param args |
| 119 | * @return FelixCommandOptions |
| 120 | */ |
| 121 | public static FelixCommandOptions parseCommand(String[] args){ |
| 122 | FelixCommandOptions opt = new FelixCommandOptions(); |
| 123 | CmdLineParser parser = new CmdLineParser(opt); |
| 124 | try{ |
| 125 | parser.parseArgument(args); |
| 126 | if(opt.showHelp){ |
| 127 | UIMan.println("USAGE:"); |
| 128 | parser.printUsage(System.out); |
| 129 | return null; |
| 130 | } |
| 131 | }catch(CmdLineException e){ |
| 132 | System.err.println(e.getMessage()); |
| 133 | UIMan.println("USAGE:"); |
| 134 | parser.printUsage(System.out); |
| 135 | return null; |
| 136 | } |
| 137 | |
| 138 | opt = processOptions(opt); |
| 139 | return opt; |
| 140 | } |
| 141 | |
| 142 | } |