summaryrefslogtreecommitdiff
path: root/src/main/java/cuchaz/enigma/CommandMain.java
diff options
context:
space:
mode:
Diffstat (limited to 'src/main/java/cuchaz/enigma/CommandMain.java')
-rw-r--r--src/main/java/cuchaz/enigma/CommandMain.java105
1 files changed, 0 insertions, 105 deletions
diff --git a/src/main/java/cuchaz/enigma/CommandMain.java b/src/main/java/cuchaz/enigma/CommandMain.java
deleted file mode 100644
index 93b013d..0000000
--- a/src/main/java/cuchaz/enigma/CommandMain.java
+++ /dev/null
@@ -1,105 +0,0 @@
1/*******************************************************************************
2 * Copyright (c) 2015 Jeff Martin.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the GNU Lesser General Public
5 * License v3.0 which accompanies this distribution, and is available at
6 * http://www.gnu.org/licenses/lgpl.html
7 * <p>
8 * Contributors:
9 * Jeff Martin - initial API and implementation
10 ******************************************************************************/
11
12package cuchaz.enigma;
13
14import cuchaz.enigma.command.*;
15
16import java.util.LinkedHashMap;
17import java.util.Locale;
18import java.util.Map;
19
20public class CommandMain {
21
22 private static final Map<String, Command> COMMANDS = new LinkedHashMap<>();
23
24 public static void main(String... args) throws Exception {
25 try {
26 // process the command
27 if (args.length < 1)
28 throw new IllegalArgumentException("Requires a command");
29 String command = args[0].toLowerCase(Locale.ROOT);
30
31 Command cmd = COMMANDS.get(command);
32 if (cmd == null)
33 throw new IllegalArgumentException("Command not recognized: " + command);
34
35 if (!cmd.isValidArgument(args.length - 1)) {
36 throw new CommandHelpException(cmd);
37 }
38
39 String[] cmdArgs = new String[args.length - 1];
40 System.arraycopy(args, 1, cmdArgs, 0, args.length - 1);
41
42 try {
43 cmd.run(cmdArgs);
44 } catch (Exception ex) {
45 throw new CommandHelpException(cmd, ex);
46 }
47 } catch (CommandHelpException ex) {
48 System.err.println(ex.getMessage());
49 System.out.println(String.format("%s - %s", Constants.NAME, Constants.VERSION));
50 System.out.println("Command " + ex.command.name + " has encountered an error! Usage:");
51 printHelp(ex.command);
52 System.exit(1);
53 } catch (IllegalArgumentException ex) {
54 System.err.println(ex.getMessage());
55 printHelp();
56 System.exit(1);
57 }
58 }
59
60 private static void printHelp() {
61 System.out.println(String.format("%s - %s", Constants.NAME, Constants.VERSION));
62 System.out.println("Usage:");
63 System.out.println("\tjava -cp enigma.jar cuchaz.enigma.CommandMain <command>");
64 System.out.println("\twhere <command> is one of:");
65
66 for (Command command : COMMANDS.values()) {
67 printHelp(command);
68 }
69 }
70
71 private static void printHelp(Command command) {
72 System.out.println("\t\t" + command.name + " " + command.getUsage());
73 }
74
75 private static void register(Command command) {
76 Command old = COMMANDS.put(command.name, command);
77 if (old != null) {
78 System.err.println("Command " + old + " with name " + command.name + " has been substituted by " + command);
79 }
80 }
81
82 static {
83 register(new DeobfuscateCommand());
84 register(new DecompileCommand());
85 register(new ConvertMappingsCommand());
86 register(new ComposeMappingsCommand());
87 register(new InvertMappingsCommand());
88 register(new CheckMappingsCommand());
89 register(new MapSpecializedMethodsCommand());
90 }
91
92 private static final class CommandHelpException extends IllegalArgumentException {
93
94 final Command command;
95
96 CommandHelpException(Command command) {
97 this.command = command;
98 }
99
100 CommandHelpException(Command command, Throwable cause) {
101 super(cause);
102 this.command = command;
103 }
104 }
105}