You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
74 lines
2.1 KiB
74 lines
2.1 KiB
package com.ski.crawler.util;
|
|
|
|
import java.util.HashMap;
|
|
import java.util.Locale;
|
|
import java.util.Map;
|
|
|
|
public class CliArgs {
|
|
public static Map<String, String> parseOptions(String[] args, int startIndex) {
|
|
Map<String, String> out = new HashMap<>();
|
|
if (args == null) {
|
|
return out;
|
|
}
|
|
for (int i = startIndex; i < args.length; i++) {
|
|
String a = args[i];
|
|
if (a == null) {
|
|
continue;
|
|
}
|
|
String t = a.trim();
|
|
if (!t.startsWith("--")) {
|
|
continue;
|
|
}
|
|
String body = t.substring(2);
|
|
String key;
|
|
String value;
|
|
int eq = body.indexOf('=');
|
|
if (eq >= 0) {
|
|
key = body.substring(0, eq).trim().toLowerCase(Locale.ROOT);
|
|
value = body.substring(eq + 1).trim();
|
|
} else {
|
|
key = body.trim().toLowerCase(Locale.ROOT);
|
|
if (i + 1 < args.length && args[i + 1] != null && !args[i + 1].trim().startsWith("--")) {
|
|
value = args[++i].trim();
|
|
} else {
|
|
value = "true";
|
|
}
|
|
}
|
|
if (!key.isEmpty()) {
|
|
out.put(key, value);
|
|
}
|
|
}
|
|
return out;
|
|
}
|
|
|
|
public static int parseInt(String v, int def) {
|
|
try {
|
|
if (v == null || v.trim().isEmpty()) {
|
|
return def;
|
|
}
|
|
return Integer.parseInt(v.trim());
|
|
} catch (Exception e) {
|
|
return def;
|
|
}
|
|
}
|
|
|
|
public static Integer parseNullableInt(String v) {
|
|
try {
|
|
if (v == null || v.trim().isEmpty()) {
|
|
return null;
|
|
}
|
|
return Integer.parseInt(v.trim());
|
|
} catch (Exception e) {
|
|
return null;
|
|
}
|
|
}
|
|
|
|
public static boolean parseBoolean(String v) {
|
|
if (v == null) {
|
|
return false;
|
|
}
|
|
String t = v.trim().toLowerCase(Locale.ROOT);
|
|
return t.equals("true") || t.equals("1") || t.equals("yes") || t.equals("y") || t.equals("on");
|
|
}
|
|
}
|
|
|
|
|