479 lines
20 KiB
Java
479 lines
20 KiB
Java
package com.zwitserloot.cmdreader;
|
|
|
|
import java.lang.reflect.Constructor;
|
|
import java.lang.reflect.Field;
|
|
import java.lang.reflect.InvocationTargetException;
|
|
import java.lang.reflect.Modifier;
|
|
import java.util.ArrayList;
|
|
import java.util.Collection;
|
|
import java.util.Collections;
|
|
import java.util.Iterator;
|
|
import java.util.List;
|
|
import java.util.Map;
|
|
/* loaded from: com.discord-120111.apk:com/zwitserloot/cmdreader/CmdReader.SCL.lombok */
|
|
public class CmdReader<T> {
|
|
private final Class<T> settingsDescriptor;
|
|
private final List<ParseItem> items = Collections.unmodifiableList(init());
|
|
private final Map<Character, ParseItem> shorthands = ParseItem.makeShortHandMap(this.items);
|
|
private final List<ParseItem> seqList = makeSeqList(this.items);
|
|
private static final int SCREEN_WIDTH = 72;
|
|
|
|
/* renamed from: com.zwitserloot.cmdreader.CmdReader$1State reason: invalid class name */
|
|
/* loaded from: com.discord-120111.apk:com/zwitserloot/cmdreader/CmdReader$1State.SCL.lombok */
|
|
class C1State {
|
|
List<ParseItem> used = new ArrayList();
|
|
final /* synthetic */ Object val$obj;
|
|
|
|
C1State(Object obj) {
|
|
this.val$obj = obj;
|
|
}
|
|
|
|
void handle(ParseItem parseItem, String str) {
|
|
parseItem.set(this.val$obj, str);
|
|
this.used.add(parseItem);
|
|
}
|
|
|
|
void finish() throws InvalidCommandLineException {
|
|
checkForGlobalMandatories();
|
|
checkForExcludes();
|
|
checkForRequires();
|
|
checkForMandatoriesIf();
|
|
checkForMandatoriesIfNot();
|
|
}
|
|
|
|
private void checkForGlobalMandatories() throws InvalidCommandLineException {
|
|
for (ParseItem parseItem : CmdReader.access$000(CmdReader.this)) {
|
|
if (parseItem.isMandatory() && !this.used.contains(parseItem)) {
|
|
throw new InvalidCommandLineException("You did not specify mandatory parameter " + parseItem.getFullName());
|
|
}
|
|
}
|
|
}
|
|
|
|
private void checkForExcludes() throws InvalidCommandLineException {
|
|
for (ParseItem parseItem : CmdReader.access$000(CmdReader.this)) {
|
|
if (this.used.contains(parseItem)) {
|
|
for (String str : parseItem.getExcludes()) {
|
|
for (ParseItem parseItem2 : CmdReader.access$000(CmdReader.this)) {
|
|
if (parseItem2.getFullName().equals(str) && this.used.contains(parseItem2)) {
|
|
throw new InvalidCommandLineException("You specified parameter " + parseItem2.getFullName() + " which cannot be used together with " + parseItem.getFullName());
|
|
}
|
|
}
|
|
}
|
|
continue;
|
|
}
|
|
}
|
|
}
|
|
|
|
private void checkForRequires() throws InvalidCommandLineException {
|
|
for (ParseItem parseItem : CmdReader.access$000(CmdReader.this)) {
|
|
if (this.used.contains(parseItem)) {
|
|
for (String str : parseItem.getRequires()) {
|
|
for (ParseItem parseItem2 : CmdReader.access$000(CmdReader.this)) {
|
|
if (parseItem2.getFullName().equals(str) && !this.used.contains(parseItem2)) {
|
|
throw new InvalidCommandLineException("You specified parameter " + parseItem.getFullName() + " which requires that you also supply " + parseItem2.getFullName());
|
|
}
|
|
}
|
|
}
|
|
continue;
|
|
}
|
|
}
|
|
}
|
|
|
|
private void checkForMandatoriesIf() throws InvalidCommandLineException {
|
|
for (ParseItem parseItem : CmdReader.access$000(CmdReader.this)) {
|
|
if (!this.used.contains(parseItem) && parseItem.getMandatoryIf().size() != 0) {
|
|
for (String str : parseItem.getMandatoryIf()) {
|
|
for (ParseItem parseItem2 : CmdReader.access$000(CmdReader.this)) {
|
|
if (parseItem2.getFullName().equals(str) && this.used.contains(parseItem2)) {
|
|
throw new InvalidCommandLineException("You did not specify parameter " + parseItem.getFullName() + " which is mandatory if you use " + parseItem2.getFullName());
|
|
}
|
|
}
|
|
}
|
|
continue;
|
|
}
|
|
}
|
|
}
|
|
|
|
private void checkForMandatoriesIfNot() throws InvalidCommandLineException {
|
|
for (ParseItem parseItem : CmdReader.access$000(CmdReader.this)) {
|
|
if (!this.used.contains(parseItem) && parseItem.getMandatoryIfNot().size() != 0) {
|
|
for (String str : parseItem.getMandatoryIfNot()) {
|
|
for (ParseItem parseItem2 : CmdReader.access$000(CmdReader.this)) {
|
|
if (!parseItem2.getFullName().equals(str) || !this.used.contains(parseItem2)) {
|
|
}
|
|
}
|
|
}
|
|
StringBuilder sb = new StringBuilder();
|
|
if (parseItem.getMandatoryIfNot().size() > 1) {
|
|
sb.append("one of ");
|
|
}
|
|
for (String str2 : parseItem.getMandatoryIfNot()) {
|
|
sb.append(str2).append(", ");
|
|
}
|
|
sb.setLength(sb.length() - 2);
|
|
throw new InvalidCommandLineException("You did not specify parameter " + parseItem.getFullName() + " which is mandatory unless you use " + ((Object) sb));
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
private CmdReader(Class<T> cls) {
|
|
this.settingsDescriptor = cls;
|
|
}
|
|
|
|
public static <T> CmdReader<T> of(Class<T> cls) {
|
|
return new CmdReader<>(cls);
|
|
}
|
|
|
|
private List<ParseItem> init() {
|
|
Field[] declaredFields;
|
|
ArrayList arrayList = new ArrayList();
|
|
for (Class<T> cls = this.settingsDescriptor; cls != Object.class; cls = cls.getSuperclass()) {
|
|
for (Field field : this.settingsDescriptor.getDeclaredFields()) {
|
|
field.setAccessible(true);
|
|
if (!Modifier.isStatic(field.getModifiers())) {
|
|
arrayList.add(new ParseItem(field));
|
|
}
|
|
}
|
|
}
|
|
ParseItem.multiSanityChecks(arrayList);
|
|
return arrayList;
|
|
}
|
|
|
|
private static List<ParseItem> makeSeqList(List<ParseItem> list) {
|
|
ArrayList arrayList = new ArrayList();
|
|
for (ParseItem parseItem : list) {
|
|
if (parseItem.isSeq()) {
|
|
arrayList.add(parseItem);
|
|
}
|
|
}
|
|
return arrayList;
|
|
}
|
|
|
|
public String generateCommandLineHelp(String str) {
|
|
StringBuilder sb = new StringBuilder();
|
|
int i = 0;
|
|
int i2 = 0;
|
|
for (ParseItem parseItem : this.items) {
|
|
if (!parseItem.isSeq()) {
|
|
i = Math.max(i, parseItem.getFullName().length() + (parseItem.isParameterized() ? 4 : 0));
|
|
i2 = Math.max(i2, parseItem.getShorthand().length());
|
|
}
|
|
}
|
|
if (i2 == 0) {
|
|
i2++;
|
|
}
|
|
int i3 = (i2 * 3) - 1;
|
|
generateShortSummary(str, sb);
|
|
generateSequentialArgsHelp(sb);
|
|
generateMandatoryArgsHelp(i, i3, sb);
|
|
generateOptionalArgsHelp(i, i3, sb);
|
|
return sb.toString();
|
|
}
|
|
|
|
private void generateShortSummary(String str, StringBuilder sb) {
|
|
if (str != null && str.length() > 0) {
|
|
sb.append(str).append(" ");
|
|
}
|
|
StringBuilder sb2 = new StringBuilder();
|
|
for (ParseItem parseItem : this.items) {
|
|
if (!parseItem.isSeq() && !parseItem.isMandatory()) {
|
|
sb2.append(parseItem.getShorthand());
|
|
}
|
|
}
|
|
if (sb2.length() > 0) {
|
|
sb.append("[-").append((CharSequence) sb2).append("] ");
|
|
sb2.setLength(0);
|
|
}
|
|
for (ParseItem parseItem2 : this.items) {
|
|
if (!parseItem2.isSeq() && parseItem2.isMandatory()) {
|
|
sb2.append(parseItem2.getShorthand());
|
|
}
|
|
}
|
|
if (sb2.length() > 0) {
|
|
sb.append("-").append((CharSequence) sb2).append(" ");
|
|
sb2.setLength(0);
|
|
}
|
|
for (ParseItem parseItem3 : this.items) {
|
|
if (!parseItem3.isSeq() && parseItem3.isMandatory() && parseItem3.getShorthand().length() == 0) {
|
|
sb.append("--").append(parseItem3.getFullName()).append("=val ");
|
|
}
|
|
}
|
|
for (ParseItem parseItem4 : this.items) {
|
|
if (parseItem4.isSeq()) {
|
|
if (!parseItem4.isMandatory()) {
|
|
sb.append('[');
|
|
}
|
|
sb.append(parseItem4.getFullName());
|
|
if (!parseItem4.isMandatory()) {
|
|
sb.append(']');
|
|
}
|
|
sb.append(' ');
|
|
}
|
|
}
|
|
sb.append("\n");
|
|
}
|
|
|
|
private void generateSequentialArgsHelp(StringBuilder sb) {
|
|
ArrayList<ParseItem> arrayList = new ArrayList();
|
|
for (ParseItem parseItem : this.items) {
|
|
if (parseItem.isSeq() && parseItem.getFullDescription().length() > 0) {
|
|
arrayList.add(parseItem);
|
|
}
|
|
}
|
|
if (arrayList.size() != 0) {
|
|
int i = 0;
|
|
for (ParseItem parseItem2 : arrayList) {
|
|
i = Math.max(i, parseItem2.getFullName().length());
|
|
}
|
|
sb.append("\n Sequential arguments:\n");
|
|
for (ParseItem parseItem3 : arrayList) {
|
|
generateSequentialArgHelp(i, parseItem3, sb);
|
|
}
|
|
}
|
|
}
|
|
|
|
private void generateMandatoryArgsHelp(int i, int i2, StringBuilder sb) {
|
|
ArrayList<ParseItem> arrayList = new ArrayList();
|
|
for (ParseItem parseItem : this.items) {
|
|
if (parseItem.isMandatory() && !parseItem.isSeq()) {
|
|
arrayList.add(parseItem);
|
|
}
|
|
}
|
|
if (arrayList.size() != 0) {
|
|
sb.append("\n Mandatory arguments:\n");
|
|
for (ParseItem parseItem2 : arrayList) {
|
|
generateArgHelp(i, i2, parseItem2, sb);
|
|
}
|
|
}
|
|
}
|
|
|
|
private void generateOptionalArgsHelp(int i, int i2, StringBuilder sb) {
|
|
ArrayList<ParseItem> arrayList = new ArrayList();
|
|
for (ParseItem parseItem : this.items) {
|
|
if (!parseItem.isMandatory() && !parseItem.isSeq()) {
|
|
arrayList.add(parseItem);
|
|
}
|
|
}
|
|
if (arrayList.size() != 0) {
|
|
sb.append("\n Optional arguments:\n");
|
|
for (ParseItem parseItem2 : arrayList) {
|
|
generateArgHelp(i, i2, parseItem2, sb);
|
|
}
|
|
}
|
|
}
|
|
|
|
private void generateArgHelp(int i, int i2, ParseItem parseItem, StringBuilder sb) {
|
|
char[] charArray;
|
|
sb.append(" ");
|
|
sb.append(String.format("--%-" + i + "s ", parseItem.getFullName() + (parseItem.isParameterized() ? "=val" : "")));
|
|
StringBuilder sb2 = new StringBuilder();
|
|
for (char c : parseItem.getShorthand().toCharArray()) {
|
|
if (sb2.length() > 0) {
|
|
sb2.append(" ");
|
|
}
|
|
sb2.append("-").append(c);
|
|
}
|
|
sb.append(String.format("%-" + i2 + "s ", sb2));
|
|
int i3 = (64 - i2) - i;
|
|
String fullDescription = parseItem.getFullDescription();
|
|
if (fullDescription.length() == 0 || fullDescription.length() < i3) {
|
|
sb.append(fullDescription).append("\n");
|
|
return;
|
|
}
|
|
for (String str : wordbreak(parseItem.getFullDescription(), 64)) {
|
|
sb.append("\n ").append(str);
|
|
}
|
|
sb.append("\n");
|
|
}
|
|
|
|
private void generateSequentialArgHelp(int i, ParseItem parseItem, StringBuilder sb) {
|
|
sb.append(" ");
|
|
sb.append(String.format("%-" + i + "s ", parseItem.getFullName()));
|
|
int i2 = 65 - i;
|
|
String fullDescription = parseItem.getFullDescription();
|
|
if (fullDescription.length() == 0 || fullDescription.length() < i2) {
|
|
sb.append(fullDescription).append("\n");
|
|
return;
|
|
}
|
|
for (String str : wordbreak(parseItem.getFullDescription(), 64)) {
|
|
sb.append("\n ").append(str);
|
|
}
|
|
sb.append("\n");
|
|
}
|
|
|
|
private static List<String> wordbreak(String str, int i) {
|
|
StringBuilder sb = new StringBuilder();
|
|
ArrayList arrayList = new ArrayList();
|
|
int i2 = -1;
|
|
char[] charArray = str.toCharArray();
|
|
int length = charArray.length;
|
|
for (int i3 = 0; i3 < length; i3++) {
|
|
char c = charArray[i3];
|
|
if (c == '\t') {
|
|
c = ' ';
|
|
}
|
|
if (c == '\n') {
|
|
arrayList.add(sb.toString());
|
|
sb.setLength(0);
|
|
i2 = -1;
|
|
} else {
|
|
if (c == ' ') {
|
|
i2 = sb.length();
|
|
sb.append(' ');
|
|
} else {
|
|
sb.append(c);
|
|
}
|
|
if (sb.length() > i && i2 > 8) {
|
|
arrayList.add(sb.substring(0, i2));
|
|
String substring = sb.substring(i2 + 1);
|
|
sb.setLength(0);
|
|
sb.append(substring);
|
|
i2 = -1;
|
|
}
|
|
}
|
|
}
|
|
if (sb.length() > 0) {
|
|
arrayList.add(sb.toString());
|
|
}
|
|
return arrayList;
|
|
}
|
|
|
|
public T make(String str) throws InvalidCommandLineException, IllegalArgumentException {
|
|
char[] charArray;
|
|
ArrayList arrayList = new ArrayList();
|
|
StringBuilder sb = new StringBuilder();
|
|
boolean z2 = false;
|
|
boolean z3 = false;
|
|
for (char c : str.toCharArray()) {
|
|
if (z3) {
|
|
z3 = false;
|
|
if (c != '\n') {
|
|
sb.append(c);
|
|
}
|
|
}
|
|
if (c == '\\') {
|
|
z3 = true;
|
|
} else if (c == '\"') {
|
|
z2 = !z2;
|
|
} else if (c != ' ' || z2) {
|
|
sb.append(c);
|
|
} else {
|
|
String sb2 = sb.toString();
|
|
sb.setLength(0);
|
|
if (!sb2.equals("")) {
|
|
arrayList.add(sb2);
|
|
}
|
|
}
|
|
}
|
|
if (sb.length() > 0) {
|
|
arrayList.add(sb.toString());
|
|
}
|
|
return make((String[]) arrayList.toArray(new String[arrayList.size()]));
|
|
}
|
|
|
|
public T make(String[] strArr) throws InvalidCommandLineException {
|
|
char[] charArray;
|
|
T construct = construct();
|
|
if (strArr == null) {
|
|
strArr = new String[0];
|
|
}
|
|
int i = 0;
|
|
C1State r0 = new C1State(construct);
|
|
int i2 = 0;
|
|
while (i2 < strArr.length) {
|
|
if (strArr[i2].startsWith("--")) {
|
|
int indexOf = strArr[i2].indexOf(61);
|
|
String substring = indexOf == -1 ? strArr[i2].substring(2) : strArr[i2].substring(2, indexOf);
|
|
String substring2 = indexOf == -1 ? "" : strArr[i2].substring(indexOf + 1);
|
|
if (substring2.length() != 0 || indexOf == -1) {
|
|
boolean z2 = false;
|
|
Iterator<ParseItem> it = this.items.iterator();
|
|
while (true) {
|
|
if (!it.hasNext()) {
|
|
break;
|
|
}
|
|
ParseItem next = it.next();
|
|
if (next.getFullName().equalsIgnoreCase(substring)) {
|
|
if (next.isParameterized() && substring2.length() == 0) {
|
|
if (i2 >= strArr.length - 1 || strArr[i2 + 1].startsWith("-")) {
|
|
throw new InvalidCommandLineException(String.format("invalid command line argument - %s requires a parameter but there is none.", substring));
|
|
}
|
|
i2++;
|
|
substring2 = strArr[i2];
|
|
}
|
|
r0.handle(next, (next.isParameterized() || substring2.length() != 0) ? substring2 : null);
|
|
z2 = true;
|
|
}
|
|
}
|
|
if (!z2) {
|
|
throw new InvalidCommandLineException("invalid command line argument - I don't know about that option: " + strArr[i2]);
|
|
}
|
|
} else {
|
|
throw new InvalidCommandLineException("invalid command line argument - you should write something after the '=': " + strArr[i2]);
|
|
}
|
|
} else if (strArr[i2].startsWith("-")) {
|
|
for (char c : strArr[i2].substring(1).toCharArray()) {
|
|
ParseItem parseItem = this.shorthands.get(Character.valueOf(c));
|
|
if (parseItem == null) {
|
|
throw new InvalidCommandLineException(String.format("invalid command line argument - %s is not a known option: %s", Character.valueOf(c), strArr[i2]));
|
|
}
|
|
if (!parseItem.isParameterized()) {
|
|
r0.handle(parseItem, null);
|
|
} else if (i2 >= strArr.length - 1 || strArr[i2 + 1].startsWith("-")) {
|
|
throw new InvalidCommandLineException(String.format("invalid command line argument - %s requires a parameter but there is none.", Character.valueOf(c)));
|
|
} else {
|
|
i2++;
|
|
r0.handle(parseItem, strArr[i2]);
|
|
}
|
|
}
|
|
continue;
|
|
} else {
|
|
i++;
|
|
if (this.seqList.size() >= i) {
|
|
r0.handle(this.seqList.get(i - 1), strArr[i2]);
|
|
} else if (this.seqList.size() <= 0 || !this.seqList.get(this.seqList.size() - 1).isCollection()) {
|
|
throw new InvalidCommandLineException(String.format("invalid command line argument - you've provided too many free-standing arguments: %s", strArr[i2]));
|
|
} else {
|
|
r0.handle(this.seqList.get(this.seqList.size() - 1), strArr[i2]);
|
|
}
|
|
}
|
|
i2++;
|
|
}
|
|
r0.finish();
|
|
return construct;
|
|
}
|
|
|
|
private T construct() {
|
|
try {
|
|
Constructor<T> declaredConstructor = this.settingsDescriptor.getDeclaredConstructor(new Class[0]);
|
|
declaredConstructor.setAccessible(true);
|
|
return declaredConstructor.newInstance(new Object[0]);
|
|
} catch (IllegalAccessException e) {
|
|
throw new IllegalArgumentException("Huh?");
|
|
} catch (InstantiationException e2) {
|
|
throw new IllegalArgumentException(String.format("A CmdReader class must not be an interface or abstract: %s", this.settingsDescriptor));
|
|
} catch (NoSuchMethodException e3) {
|
|
throw new IllegalArgumentException(String.format("A CmdReader class must have a no-args constructor: %s", this.settingsDescriptor));
|
|
} catch (InvocationTargetException e4) {
|
|
throw new IllegalArgumentException("Exception occurred when constructing CmdReader class " + this.settingsDescriptor, e4.getCause());
|
|
}
|
|
}
|
|
|
|
public static String squash(Collection<String> collection) {
|
|
Iterator<String> it = collection.iterator();
|
|
StringBuilder sb = new StringBuilder();
|
|
while (it.hasNext()) {
|
|
sb.append(it.next());
|
|
if (it.hasNext()) {
|
|
sb.append(' ');
|
|
}
|
|
}
|
|
return sb.toString();
|
|
}
|
|
|
|
static /* synthetic */ List access$000(CmdReader cmdReader) {
|
|
return cmdReader.items;
|
|
}
|
|
}
|