Skip to content

Commit 809702b

Browse files
convert Argument to record, eliminate ComposedArgument
1 parent 689b4a8 commit 809702b

17 files changed

+156
-162
lines changed
Lines changed: 1 addition & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -1,84 +1,3 @@
11
package org.quiltmc.enigma.command;
22

3-
public enum Argument {
4-
INPUT_JAR("<input-jar>",
5-
"""
6-
A path to the .jar file to use in executing the command."""
7-
),
8-
INPUT_MAPPINGS("<input-mappings>",
9-
"""
10-
A path to the file or folder to read mappings from."""
11-
),
12-
LEFT_MAPPINGS("<left-mappings>",
13-
"""
14-
A path to the left file or folder to read mappings from, used in commands which take two mapping inputs."""
15-
),
16-
RIGHT_MAPPINGS("<right-mappings>",
17-
"""
18-
A path to the right file or folder to read mappings from, used in commands which take two mapping inputs."""
19-
),
20-
MAPPING_OUTPUT("<mapping-output>",
21-
"""
22-
A path to the file or folder to write mappings to. Will be created if missing."""
23-
),
24-
KEEP_MODE("<keep-mode>",
25-
"""
26-
Which mappings should overwrite the others when composing conflicting mappings. Allowed values are "left", "right", and "both"."""
27-
),
28-
DECOMPILER("<decompiler>",
29-
"""
30-
The decompiler to use when producing output. Allowed values are (case-insensitive):
31-
- VINEFLOWER
32-
- CFR
33-
- PROCYON
34-
- BYTECODE"""
35-
),
36-
OUTPUT_FOLDER("<output-folder>",
37-
"""
38-
A path to the file or folder to write output to."""
39-
),
40-
OUTPUT_JAR("<output-jar>",
41-
"""
42-
A path to the .jar file to write output to. Will be created if missing."""
43-
),
44-
FILL_ALL("<fill-all>",
45-
"""
46-
Whether to fill all possible mappings. Allowed values are "true" and "false"."""
47-
),
48-
ENIGMA_PROFILE("<enigma-profile>",
49-
"""
50-
A path to an Enigma profile JSON file, used to apply things like plugins."""
51-
),
52-
OBFUSCATED_NAMESPACE("<obfuscated-namespace>",
53-
"""
54-
The namespace to use for obfuscated names when writing mappings. Only used in certain mapping formats."""
55-
),
56-
DEOBFUSCATED_NAMESPACE("<deobfuscated-namespace>",
57-
"""
58-
The namespace to use for deobfuscated names when writing mappings. Only used in certain mapping formats."""
59-
);
60-
61-
private final String displayForm;
62-
private final String explanation;
63-
64-
Argument(String displayForm, String explanation) {
65-
this.displayForm = displayForm;
66-
this.explanation = explanation;
67-
}
68-
69-
public String getDisplayForm() {
70-
return this.displayForm;
71-
}
72-
73-
public String getExplanation() {
74-
return this.explanation;
75-
}
76-
77-
public ComposedArgument required() {
78-
return new ComposedArgument(this, false);
79-
}
80-
81-
public ComposedArgument optional() {
82-
return new ComposedArgument(this, true);
83-
}
84-
}
3+
record Argument(String displayForm, String explanation) { }

enigma-cli/src/main/java/org/quiltmc/enigma/command/CheckMappingsCommand.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,7 @@
1212

1313
public class CheckMappingsCommand extends Command {
1414
public CheckMappingsCommand() {
15-
super(Argument.INPUT_JAR.required(),
16-
Argument.INPUT_MAPPINGS.required()
17-
);
15+
super(CommonArguments.INPUT_JAR, CommonArguments.INPUT_MAPPINGS);
1816
}
1917

2018
@Override

enigma-cli/src/main/java/org/quiltmc/enigma/command/Command.java

Lines changed: 27 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package org.quiltmc.enigma.command;
22

3+
import com.google.common.collect.ImmutableList;
34
import org.quiltmc.enigma.api.Enigma;
45
import org.quiltmc.enigma.api.EnigmaProfile;
56
import org.quiltmc.enigma.api.EnigmaProject;
@@ -25,32 +26,25 @@
2526
import java.nio.file.Files;
2627
import java.nio.file.Paths;
2728
import java.nio.file.Path;
28-
import java.util.ArrayList;
29+
import java.util.Collection;
2930
import java.util.List;
3031
import java.util.Locale;
3132
import javax.annotation.Nullable;
3233

3334
public abstract class Command {
34-
protected final List<Argument> requiredArguments = new ArrayList<>();
35-
protected final List<Argument> optionalArguments = new ArrayList<>();
36-
protected final List<ComposedArgument> allArguments;
35+
final ImmutableList<Argument> requiredArguments;
36+
final ImmutableList<Argument> optionalArguments;
3737

38-
protected Command(ComposedArgument... arguments) {
39-
this.allArguments = new ArrayList<>();
38+
final int totalArgumentCount;
4039

41-
for (ComposedArgument argument : arguments) {
42-
if (argument.optional()) {
43-
this.optionalArguments.add(argument.argument());
44-
this.allArguments.add(argument);
45-
} else {
46-
this.requiredArguments.add(argument.argument());
47-
this.allArguments.add(argument);
40+
Command(Argument... requiredArguments) {
41+
this(ImmutableList.copyOf(requiredArguments), ImmutableList.of());
42+
}
4843

49-
if (!this.optionalArguments.isEmpty()) {
50-
throw new IllegalArgumentException("optional arguments should be grouped at the end of command arguments! (declaring arg " + argument + ")");
51-
}
52-
}
53-
}
44+
Command(Collection<Argument> requiredArguments, Collection<Argument> optionalArguments) {
45+
this.requiredArguments = ImmutableList.copyOf(requiredArguments);
46+
this.optionalArguments = ImmutableList.copyOf(optionalArguments);
47+
this.totalArgumentCount = this.requiredArguments.size() + this.optionalArguments.size();
5448
}
5549

5650
public String getUsage() {
@@ -68,7 +62,7 @@ public String getUsage() {
6862

6963
private static void appendArguments(StringBuilder builder, List<Argument> arguments) {
7064
for (int i = 0; i < arguments.size(); i++) {
71-
builder.append(arguments.get(i).getDisplayForm());
65+
builder.append(arguments.get(i).displayForm());
7266
if (i < arguments.size() - 1) {
7367
builder.append(" ");
7468
}
@@ -82,7 +76,7 @@ private static void appendArguments(StringBuilder builder, List<Argument> argume
8276
*/
8377
public boolean checkArgumentCount(int length) {
8478
// valid if length is equal to the amount of required arguments or between required argument count and total argument count
85-
return length == this.requiredArguments.size() || length > this.requiredArguments.size() && length <= this.allArguments.size();
79+
return length == this.requiredArguments.size() || length > this.requiredArguments.size() && length <= this.totalArgumentCount;
8680
}
8781

8882
/**
@@ -125,10 +119,15 @@ public static Enigma createEnigma() {
125119
* @return the argument, as a string
126120
*/
127121
protected String getArg(String[] args, int index) {
128-
if (index < this.allArguments.size() && index >= 0) {
129-
return getArg(args, index, this.allArguments.get(index));
122+
if (index < this.totalArgumentCount && index >= 0) {
123+
final int requiredCount = this.requiredArguments.size();
124+
if (index < requiredCount) {
125+
return getArg(args, index, this.requiredArguments.get(index), false);
126+
} else {
127+
return getArg(args, index, this.optionalArguments.get(index - requiredCount), true);
128+
}
130129
} else {
131-
throw new RuntimeException("arg index is outside of range of possible arguments! (index: " + index + ", allowed arg count: " + this.allArguments.size() + ")");
130+
throw new RuntimeException("arg index is outside of range of possible arguments! (index: " + index + ", allowed arg count: " + this.totalArgumentCount + ")");
132131
}
133132
}
134133

@@ -244,16 +243,16 @@ protected static Path getWritablePath(String path) {
244243
return dir;
245244
}
246245

247-
private static String getArg(String[] args, int i, ComposedArgument argument) {
248-
if (i >= args.length) {
249-
if (!argument.optional()) {
250-
throw new IllegalArgumentException(argument.argument().getDisplayForm() + " is required");
246+
private static String getArg(String[] args, int index, Argument argument, boolean optional) {
247+
if (index >= args.length) {
248+
if (!optional) {
249+
throw new IllegalArgumentException(argument.displayForm() + " is required");
251250
} else {
252251
return null;
253252
}
254253
}
255254

256-
return args[i];
255+
return args[index];
257256
}
258257

259258
protected static boolean shouldDebug(String name) {

enigma-cli/src/main/java/org/quiltmc/enigma/command/CommandsUtil.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@
88
import java.nio.file.Path;
99

1010
public class CommandsUtil {
11+
private CommandsUtil() {
12+
throw new UnsupportedOperationException();
13+
}
14+
1115
public static ReadWriteService getReadWriteService(Enigma enigma, Path file) {
1216
var service = enigma.getReadWriteService(file);
1317
if (service.isEmpty()) {
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package org.quiltmc.enigma.command;
2+
3+
final class CommonArguments {
4+
private CommonArguments() {
5+
throw new UnsupportedOperationException();
6+
}
7+
8+
static final Argument INPUT_JAR = new Argument("<input-jar>",
9+
"""
10+
A path to the .jar file to use in executing the command."""
11+
);
12+
static final Argument INPUT_MAPPINGS = new Argument("<input-mappings>",
13+
"""
14+
A path to the file or folder to read mappings from."""
15+
);
16+
static final Argument MAPPING_OUTPUT = new Argument("<mapping-output>",
17+
"""
18+
A path to the file or folder to write mappings to. Will be created if missing."""
19+
);
20+
static final Argument OUTPUT_JAR = new Argument("<output-jar>",
21+
"""
22+
A path to the .jar file to write output to. Will be created if missing."""
23+
);
24+
static final Argument ENIGMA_PROFILE = new Argument("<enigma-profile>",
25+
"""
26+
A path to an Enigma profile JSON file, used to apply things like plugins."""
27+
);
28+
static final Argument OBFUSCATED_NAMESPACE = new Argument("<obfuscated-namespace>",
29+
"""
30+
The namespace to use for obfuscated names when writing mappings. Only used in certain mapping formats."""
31+
);
32+
static final Argument DEOBFUSCATED_NAMESPACE = new Argument("<deobfuscated-namespace>",
33+
"""
34+
The namespace to use for deobfuscated names when writing mappings. Only used in certain mapping formats."""
35+
);
36+
}

enigma-cli/src/main/java/org/quiltmc/enigma/command/ComposeMappingsCommand.java

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,21 @@
1717
import java.nio.file.Path;
1818

1919
public class ComposeMappingsCommand extends Command {
20+
private static final Argument LEFT_MAPPINGS = new Argument("<left-mappings>",
21+
"""
22+
A path to the left file or folder to read mappings from, used in commands which take two mapping inputs."""
23+
);
24+
private static final Argument RIGHT_MAPPINGS = new Argument("<right-mappings>",
25+
"""
26+
A path to the right file or folder to read mappings from, used in commands which take two mapping inputs."""
27+
);
28+
private static final Argument KEEP_MODE = new Argument("<keep-mode>",
29+
"""
30+
Which mappings should overwrite the others when composing conflicting mappings. Allowed values are "left", "right", and "both"."""
31+
);
32+
2033
public ComposeMappingsCommand() {
21-
super(Argument.LEFT_MAPPINGS.required(),
22-
Argument.RIGHT_MAPPINGS.required(),
23-
Argument.MAPPING_OUTPUT.required(),
24-
Argument.KEEP_MODE.required()
25-
);
34+
super(LEFT_MAPPINGS, RIGHT_MAPPINGS, CommonArguments.MAPPING_OUTPUT, KEEP_MODE);
2635
}
2736

2837
@Override

enigma-cli/src/main/java/org/quiltmc/enigma/command/ComposedArgument.java

Lines changed: 0 additions & 4 deletions
This file was deleted.

enigma-cli/src/main/java/org/quiltmc/enigma/command/ConvertMappingsCommand.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,11 @@
1717

1818
public class ConvertMappingsCommand extends Command {
1919
public ConvertMappingsCommand() {
20-
super(Argument.INPUT_MAPPINGS.required(),
21-
Argument.MAPPING_OUTPUT.required(),
22-
Argument.OBFUSCATED_NAMESPACE.required(),
23-
Argument.DEOBFUSCATED_NAMESPACE.required()
20+
super(
21+
CommonArguments.INPUT_MAPPINGS,
22+
CommonArguments.MAPPING_OUTPUT,
23+
CommonArguments.OBFUSCATED_NAMESPACE,
24+
CommonArguments.DEOBFUSCATED_NAMESPACE
2425
);
2526
}
2627

enigma-cli/src/main/java/org/quiltmc/enigma/command/DecompileCommand.java

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package org.quiltmc.enigma.command;
22

3+
import com.google.common.collect.ImmutableList;
34
import org.quiltmc.enigma.api.EnigmaProject;
45
import org.quiltmc.enigma.api.ProgressListener;
56
import org.quiltmc.enigma.api.EnigmaProject.DecompileErrorStrategy;
@@ -12,11 +13,20 @@
1213
import java.util.Locale;
1314

1415
public class DecompileCommand extends Command {
16+
private static final Argument DECOMPILER = new Argument("<decompiler>",
17+
"""
18+
The decompiler to use when producing output. Allowed values are (case-insensitive):
19+
- VINEFLOWER
20+
- CFR
21+
- PROCYON
22+
- BYTECODE"""
23+
);
24+
1525
public DecompileCommand() {
16-
super(Argument.DECOMPILER.required(),
17-
Argument.INPUT_JAR.required(),
18-
Argument.OUTPUT_JAR.required(),
19-
Argument.INPUT_MAPPINGS.optional());
26+
super(
27+
ImmutableList.of(DECOMPILER, CommonArguments.INPUT_JAR, CommonArguments.OUTPUT_JAR),
28+
ImmutableList.of(CommonArguments.INPUT_MAPPINGS)
29+
);
2030
}
2131

2232
@Override

enigma-cli/src/main/java/org/quiltmc/enigma/command/DeobfuscateCommand.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
package org.quiltmc.enigma.command;
22

3+
import com.google.common.collect.ImmutableList;
34
import org.quiltmc.enigma.api.EnigmaProject;
45
import org.quiltmc.enigma.api.ProgressListener;
56

67
import java.nio.file.Path;
78

89
public class DeobfuscateCommand extends Command {
910
public DeobfuscateCommand() {
10-
super(Argument.INPUT_JAR.required(),
11-
Argument.OUTPUT_JAR.required(),
12-
Argument.INPUT_MAPPINGS.optional()
11+
super(
12+
ImmutableList.of(CommonArguments.INPUT_JAR, CommonArguments.OUTPUT_JAR),
13+
ImmutableList.of(CommonArguments.INPUT_MAPPINGS)
1314
);
1415
}
1516

0 commit comments

Comments
 (0)