-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathMain.java
More file actions
executable file
·178 lines (153 loc) · 5.44 KB
/
Copy pathMain.java
File metadata and controls
executable file
·178 lines (153 loc) · 5.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
package synapticloop.h2zero;
/*
* Copyright (c) 2012-2024 synapticloop.
*
* All rights reserved.
*
* This source code and any derived binaries are covered by the terms and
* conditions of the Licence agreement ("the Licence"). You may not use this
* source code or any derived binaries except in compliance with the Licence.
* A copy of the Licence is available in the file named LICENCE shipped with
* this source code or binaries.
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the Licence is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* Licence for the specific language governing permissions and limitations
* under the Licence.
*/
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Map;
import synapticloop.h2zero.plugin.ant.H2ZeroTask;
import synapticloop.h2zero.util.SimpleLogger;
import synapticloop.h2zero.util.SimpleLogger.LoggerType;
public class Main {
private static final String USAGE_TXT = "/usage.txt";
private static final String CLI_OPTION_GENERATE = "generate";
private static final String CLI_OPTION_REVENGE = "revenge";
private static final String CLI_OPTION_QUICK = "quick";
private static final int CLI_GENERATE = 0;
private static final int CLI_REVENGE = 1;
private static final int CLI_QUICK = 2;
private static final Map<String, Integer> COMMAND_LINE_OPTIONS = new HashMap<>();
static {
COMMAND_LINE_OPTIONS.put(CLI_OPTION_GENERATE, CLI_GENERATE);
COMMAND_LINE_OPTIONS.put(CLI_OPTION_REVENGE, CLI_REVENGE);
COMMAND_LINE_OPTIONS.put(CLI_OPTION_QUICK, CLI_QUICK);
}
private static final int GENERATE_VERBOSE = 0;
private static final int GENERATE_IN = 1;
private static final int GENERATE_OUT = 2;
private static final String PARAMETER_VERBOSE = "-verbose";
private static final String PARAMETER_IN = "-in";
private static final String PARAMETER_OUT = "-out";
private static final Map<String, Integer> GENERATE_COMMAND_LINE_OPTIONS = new HashMap<>();
static {
GENERATE_COMMAND_LINE_OPTIONS.put(PARAMETER_VERBOSE, GENERATE_VERBOSE);
GENERATE_COMMAND_LINE_OPTIONS.put(PARAMETER_IN, GENERATE_IN);
GENERATE_COMMAND_LINE_OPTIONS.put(PARAMETER_OUT, GENERATE_OUT);
}
// all of the instance(???) variables
private static boolean isVerbose = false;
private static String inFile = null;
private static String outDir = null;
private Main() {}
private static void usageAndExit(String message) throws IOException {
if(null != message) {
System.out.println(message);
}
InputStream inputStream = Main.class.getResourceAsStream(USAGE_TXT);
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
String line = null;
while((line = bufferedReader.readLine()) != null) {
System.out.println(line);
}
System.exit(0);
}
private static void parseAndExecute(String[] args) throws IOException {
if(args.length == 0) {
usageAndExit(null);
}
// the first argument
String mode = args[0];
if(!COMMAND_LINE_OPTIONS.containsKey(mode)) {
usageAndExit("Unknown mode of '" + mode + "'.");
}
// at this point - we know the mode and now we need to parse the options
switch (COMMAND_LINE_OPTIONS.get(mode)) {
case CLI_GENERATE:
parseAndExecuteGenerate(args);
break;
case CLI_REVENGE:
case CLI_QUICK:
usageAndExit("Mode '" + mode + "' not fully implemented through the command line");
break;
default:
// admittedly this should not happen as we have already done a lookup
usageAndExit("Unknown mode of '" + mode + "'.");
break;
}
}
private static void parseAndExecuteGenerate(String[] args) throws IOException {
String mode = args[0];
// the first argument is 'generate'
for(int i = 0; i < args.length; i++) {
if(i == 0) {
continue;
}
String arg = args[i];
// the first option __MUST__ always start with a '-' (hyphen) character
if(!arg.startsWith("-") || !GENERATE_COMMAND_LINE_OPTIONS.containsKey(arg)) {
usageAndExit("Unknown argument '" + arg + "' for mode '" + mode + "'");
}
// all good to go
switch (GENERATE_COMMAND_LINE_OPTIONS.get(arg)) {
case GENERATE_VERBOSE:
isVerbose = true;
break;
case GENERATE_IN:
i++;
try {
inFile = args[i];
} catch(ArrayIndexOutOfBoundsException aioobex) {
usageAndExit("Found an argument of '" + arg + "', but no value for the option");
}
break;
case GENERATE_OUT:
i++;
try {
outDir = args[i];
} catch(ArrayIndexOutOfBoundsException aioobex) {
usageAndExit("Found an argument of '" + arg + "', but no value for the option");
}
break;
default:
break;
}
}
// check all of the parameters
if(null == inFile) { usageAndExit("Parameter '" + PARAMETER_IN + "' cannot be null"); }
if(null == outDir) { usageAndExit("Parameter '" + PARAMETER_OUT + "' cannot be null"); }
// now it is time to kick things off
H2ZeroTask h2ZeroTask = new H2ZeroTask();
h2ZeroTask.setVerbose(isVerbose);
h2ZeroTask.setInFile(inFile);
h2ZeroTask.setOutDir(outDir);
h2ZeroTask.execute();
}
public static void main(String[] args) {
try {
if(null == args) {
// unlikely but tested
usageAndExit(null);
}
parseAndExecute(args);
} catch (IOException ex) {
SimpleLogger.logFatal(LoggerType.MAIN, "Could neither find, nor read the file '" + USAGE_TXT + "' within the jar.");
}
}
}