Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve noargs message and add a help argument + detailed message #188

Merged
merged 5 commits into from
Aug 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,9 @@
import java.nio.file.Paths;
import java.util.*;
import java.util.function.Function;
import java.util.function.Supplier;
import java.util.jar.JarOutputStream;
import java.util.jar.Manifest;
import java.util.stream.Stream;
import java.util.zip.CheckedInputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import java.util.zip.ZipOutputStream;
Expand Down Expand Up @@ -60,6 +58,11 @@ else if (args.length > x+1) {
}
args = params.toArray(new String[params.size()]);

if (Arrays.stream(args).anyMatch(arg -> arg.equals("-h") || arg.equals("--help") || arg.equals("-help"))) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The content of this if body should be moved out to another method (or maybe even another class) because the main method is getting very long

ConsoleHelp.printHelp();
return;
}

if (args.length < 2) {
System.out.println(
"Usage: java -jar fernflower.jar [-<option>=<value>]* [<source>]+ <destination>\n" +
Expand Down
114 changes: 114 additions & 0 deletions src/org/jetbrains/java/decompiler/main/decompiler/ConsoleHelp.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
package org.jetbrains.java.decompiler.main.decompiler;

import org.jetbrains.java.decompiler.main.extern.IFernflowerPreferences;

import java.lang.reflect.Field;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public class ConsoleHelp {
private static final String[] DEFAULT_HELP = {
"Usage: java -jar quiltflower.jar [-<option>=<value>]* [<source>]+ <destination>",
"At least one source file or directory must be specified.",
"Options:",
"--help: Show this help",
"", "Saving options",
"A maximum of one of the options can be specified:",
"--file - Write the decompiled source to a file",
"--folder - Write the decompiled source to a folder",
"--legacy-saving - Use the legacy console-specific method of saving",
"If unspecified, the decompiled source will be automatically detected based on destination name.",
"", "General options",
"These options can be specified multiple times.",
"-e=<path> - Add the specified path to the list of external libraries",
"-only=<class> - Only decompile the specified class",
"", "Additional options",
"These options take the last specified value.",
"They each are specified with a three-character name followed by an equals sign, followed by the value.",
"Booleans are traditionally indicated with `0` or `1`, but may also be specified with `true` or `false`.",
"Because of this, an option indicated as a boolean may actually be a number."
// Options are added at runtime
};

static void printHelp() {
for (String line : DEFAULT_HELP) {
System.out.println(line);
}

List<Field> fields = Arrays.stream(IFernflowerPreferences.class.getDeclaredFields())
.filter(field -> field.getType() == String.class)
.collect(Collectors.toList());

Map<String, Object> defaults = IFernflowerPreferences.DEFAULTS;

for (Field field : fields) {
IFernflowerPreferences.Name name = field.getAnnotation(IFernflowerPreferences.Name.class);
IFernflowerPreferences.Description description = field.getAnnotation(IFernflowerPreferences.Description.class);

String paramName;
try {
paramName = (String) field.get(null);
} catch (IllegalAccessException e) {
continue;
}

if (paramName.length() != 3) {
continue;
}

StringBuilder sb = new StringBuilder();
sb.append("-").append(paramName).append("=<");

String type;
String defaultValue = (String) defaults.get(paramName);
if (defaultValue == null) {
sb.append("string>");
type = null;
} else if (defaultValue.equals("0") || defaultValue.equals("1")) {
sb.append("bool> ");
type = "bool";
} else {
try {
Integer.parseInt(defaultValue);
sb.append("int> ");
type = "int";
} catch (NumberFormatException e) {
sb.append("string>");
type = "string";
}
}

sb.append(" - ");

if (name != null) {
sb.append(name.value());
} else {
sb.append(field.getName());
}

if (description != null) {
sb.append(": ").append(description.value());
}

if (type != null) {
sb.append(" (default: ");
switch (type) {
case "bool":
sb.append(defaultValue.equals("1"));
break;
case "int":
sb.append(defaultValue);
break;
case "string":
sb.append('"').append(defaultValue).append('"');
break;
}
sb.append(")");
}

System.out.println(sb);
}
}
}