|  | 
|  | 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