Skip to content

Commit 688a603

Browse files
authored
Improve noargs message and add a help argument + detailed message (#188)
* Initial ideas for improved help message * Undo small unnecessary change in older code * Separate the main function into its own class * I forgot to update the main class * Undo moving entire main class separately Now only the help message is its own class
1 parent 0370fb9 commit 688a603

File tree

2 files changed

+119
-2
lines changed

2 files changed

+119
-2
lines changed

src/org/jetbrains/java/decompiler/main/decompiler/ConsoleDecompiler.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,9 @@
1616
import java.nio.file.Paths;
1717
import java.util.*;
1818
import java.util.function.Function;
19-
import java.util.function.Supplier;
2019
import java.util.jar.JarOutputStream;
2120
import java.util.jar.Manifest;
2221
import java.util.stream.Stream;
23-
import java.util.zip.CheckedInputStream;
2422
import java.util.zip.ZipEntry;
2523
import java.util.zip.ZipFile;
2624
import java.util.zip.ZipOutputStream;
@@ -60,6 +58,11 @@ else if (args.length > x+1) {
6058
}
6159
args = params.toArray(new String[params.size()]);
6260

61+
if (Arrays.stream(args).anyMatch(arg -> arg.equals("-h") || arg.equals("--help") || arg.equals("-help"))) {
62+
ConsoleHelp.printHelp();
63+
return;
64+
}
65+
6366
if (args.length < 2) {
6467
System.out.println(
6568
"Usage: java -jar fernflower.jar [-<option>=<value>]* [<source>]+ <destination>\n" +
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
package org.jetbrains.java.decompiler.main.decompiler;
2+
3+
import org.jetbrains.java.decompiler.main.extern.IFernflowerPreferences;
4+
5+
import java.lang.reflect.Field;
6+
import java.util.Arrays;
7+
import java.util.List;
8+
import java.util.Map;
9+
import java.util.stream.Collectors;
10+
11+
public class ConsoleHelp {
12+
private static final String[] DEFAULT_HELP = {
13+
"Usage: java -jar quiltflower.jar [-<option>=<value>]* [<source>]+ <destination>",
14+
"At least one source file or directory must be specified.",
15+
"Options:",
16+
"--help: Show this help",
17+
"", "Saving options",
18+
"A maximum of one of the options can be specified:",
19+
"--file - Write the decompiled source to a file",
20+
"--folder - Write the decompiled source to a folder",
21+
"--legacy-saving - Use the legacy console-specific method of saving",
22+
"If unspecified, the decompiled source will be automatically detected based on destination name.",
23+
"", "General options",
24+
"These options can be specified multiple times.",
25+
"-e=<path> - Add the specified path to the list of external libraries",
26+
"-only=<class> - Only decompile the specified class",
27+
"", "Additional options",
28+
"These options take the last specified value.",
29+
"They each are specified with a three-character name followed by an equals sign, followed by the value.",
30+
"Booleans are traditionally indicated with `0` or `1`, but may also be specified with `true` or `false`.",
31+
"Because of this, an option indicated as a boolean may actually be a number."
32+
// Options are added at runtime
33+
};
34+
35+
static void printHelp() {
36+
for (String line : DEFAULT_HELP) {
37+
System.out.println(line);
38+
}
39+
40+
List<Field> fields = Arrays.stream(IFernflowerPreferences.class.getDeclaredFields())
41+
.filter(field -> field.getType() == String.class)
42+
.collect(Collectors.toList());
43+
44+
Map<String, Object> defaults = IFernflowerPreferences.DEFAULTS;
45+
46+
for (Field field : fields) {
47+
IFernflowerPreferences.Name name = field.getAnnotation(IFernflowerPreferences.Name.class);
48+
IFernflowerPreferences.Description description = field.getAnnotation(IFernflowerPreferences.Description.class);
49+
50+
String paramName;
51+
try {
52+
paramName = (String) field.get(null);
53+
} catch (IllegalAccessException e) {
54+
continue;
55+
}
56+
57+
if (paramName.length() != 3) {
58+
continue;
59+
}
60+
61+
StringBuilder sb = new StringBuilder();
62+
sb.append("-").append(paramName).append("=<");
63+
64+
String type;
65+
String defaultValue = (String) defaults.get(paramName);
66+
if (defaultValue == null) {
67+
sb.append("string>");
68+
type = null;
69+
} else if (defaultValue.equals("0") || defaultValue.equals("1")) {
70+
sb.append("bool> ");
71+
type = "bool";
72+
} else {
73+
try {
74+
Integer.parseInt(defaultValue);
75+
sb.append("int> ");
76+
type = "int";
77+
} catch (NumberFormatException e) {
78+
sb.append("string>");
79+
type = "string";
80+
}
81+
}
82+
83+
sb.append(" - ");
84+
85+
if (name != null) {
86+
sb.append(name.value());
87+
} else {
88+
sb.append(field.getName());
89+
}
90+
91+
if (description != null) {
92+
sb.append(": ").append(description.value());
93+
}
94+
95+
if (type != null) {
96+
sb.append(" (default: ");
97+
switch (type) {
98+
case "bool":
99+
sb.append(defaultValue.equals("1"));
100+
break;
101+
case "int":
102+
sb.append(defaultValue);
103+
break;
104+
case "string":
105+
sb.append('"').append(defaultValue).append('"');
106+
break;
107+
}
108+
sb.append(")");
109+
}
110+
111+
System.out.println(sb);
112+
}
113+
}
114+
}

0 commit comments

Comments
 (0)