-
Notifications
You must be signed in to change notification settings - Fork 0
/
CommandLine.java
179 lines (161 loc) · 6.51 KB
/
CommandLine.java
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
179
/***************************************************************************************************
* Copyright (C) 2019 - 2020, IHEXON
* This file is part of the WatchMe.
*
* WatchMe is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* WatchMe is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with WatchMe; see the file COPYING. If not, see <http://www.gnu.org/licenses/>.
*
* This Copyright copy from shadowsocks-libev. with little modified
**************************************************************************************************/
package io.github.ihexon;
import io.github.ihexon.services.logsystem.Log;
import java.io.File;
import java.util.Hashtable;
public class CommandLine {
public final String DIR = "-d";
public final String RECURSE = "-r";
public final String EXCLUDES_HID = "-exchid";
public final String HELP0x00 = "-h";
public final String HELP0x01 = "--help";
public final String VERSION0x00 = "--version";
public final String VERSION0x01 = "-v";
public final String LOGFILE = "--log";
public final String EXCLUDEREGX = "-ex";
private final Hashtable<String, String> keywords = new Hashtable<>();
public CommandLine(String[] args) throws Exception {
args = args == null ? new String[0] : args;
parseArgs(args);
}
/**
* Start parsing two type of parameters.
* <p><strong>parse KeysPair : </strong></p> eg: -d [dir], --log [file] <br />
* the parameter which comes in pairs,
* <p><strong>parse Switch : </strong></p> eg : -h, --help, -v, --version<br />
* the parameter whch comes to be single.
*
* @param args which the String[] contains the parameters
* @throws Exception
*/
private void parseArgs(String[] args) throws Exception {
for (int i = 0; i < args.length; i++) {
if (parseSwitchs(args, i)) continue;
if (parseKeywords(args, i)) continue;
}
}
/**
* Selects the valid parameter in args, use {@link #checkPair(String[], String, int)}
* to parse the KeyPairs and store into a HashTable which named {@link #keywords}.
*
* @param args args which the String[] contains the parameters
* @param i the index of the parameter which to be checked
* @return boolean value, if the parameter in args to be checked is valid.
* than return true,otherwise return false
* @throws Exception throw {@link Exception} when the parameter can not be parse.
*/
private boolean parseKeywords(String[] args, int i) throws Exception {
boolean result = false;
if (checkPair(args, DIR, i)) {
result = true;
} else if (checkPair(args, LOGFILE, i)) {
result = true;
} else if (checkPair(args, EXCLUDEREGX, i)) {
result = true;
}
return result;
}
/**
* The {@link #parseSwitchs(String[], int)} use {@link #checkSwitch(String[], String, int)} to check the valid parameter.
* If the parameter is valid, than it will be store in a HashTable named keywords
*
* @param args args which the String[] contains the parameters
* @param i he index of the parameter which to be checked
* @return boolean value, if the parameter in args to be checked is valid.
* than return true,otherwise return false
* @throws Exception throw {@link Exception} when the parameter can not be parse.
*/
private boolean parseSwitchs(String[] args, int i) throws Exception {
boolean result = false;
if (checkSwitch(args, RECURSE, i)) {
result = true;
} else if (checkSwitch(args, HELP0x00, i)) {
result = true;
} else if (checkSwitch(args, HELP0x01, i)) {
result = true;
} else if (checkSwitch(args, EXCLUDES_HID, i)) {
result = true;
}
return result;
}
public boolean isValidExistingDirectory(String path) {
if (path == null || path.trim().isEmpty()) return false;
File file = new File(path);
return file.isDirectory();
}
synchronized private boolean checkPair(String[] args, String paramName, int i) throws Exception {
String key = args[i];
String value = null;
if (key == null) {
return false;
}
// check the linux path name.
if (key.equalsIgnoreCase(DIR)) {
value = args[i + 1];
// USE REGEX TO TEST PATH, BUT IT BUGGLY
// IncludeExclude<String> ie = new IncludeExclude<>(RegexSet.class);
// ie.include("^/|(/[a-zA-Z0-9_-]+)+$");
if (!isValidExistingDirectory(value.trim())) {
Log.getInstance().info("Error parameter '" +key.trim()+ "'");
Log.getInstance().info("The path is not exist or is not a dir");
Log.getInstance().closeAppenders();
System.exit(-1);
}
// ie.clear();
}
if (key.equalsIgnoreCase(paramName)) {
value = args[i + 1];
if (value == null) {
throw new Exception("Missing parameter for keyword '" + paramName + "'.");
}
keywords.put(paramName, value);
args[i] = null;
args[i + 1] = null;
return true;
}
return false;
}
synchronized private boolean checkSwitch(String[] args, String paramName, int i) throws Exception {
String key = args[i];
if (key == null) {
return false;
}
if (key.equalsIgnoreCase(paramName)) {
keywords.put(paramName, "true");
args[i] = null;
return true;
}
return false;
}
public String getKeyPair(String key) {
if (keywords.get(key) != null && keywords.get(key).length() != 0)
return keywords.get(key);
return null;
}
public boolean getSwitchs(String key) {
if (key != null && key.length() != 0) {
String result = getKeyPair(key);
if (result != null && result.equalsIgnoreCase("true"))
return true;
}
return false;
}
}