|
| 1 | +package edu.wpi.grip.core; |
| 2 | + |
| 3 | +import com.google.common.annotations.VisibleForTesting; |
| 4 | + |
| 5 | +import org.apache.commons.cli.CommandLine; |
| 6 | +import org.apache.commons.cli.DefaultParser; |
| 7 | +import org.apache.commons.cli.HelpFormatter; |
| 8 | +import org.apache.commons.cli.Option; |
| 9 | +import org.apache.commons.cli.Options; |
| 10 | +import org.apache.commons.cli.ParseException; |
| 11 | + |
| 12 | +import java.util.List; |
| 13 | + |
| 14 | +/** |
| 15 | + * A helper class for command line options for GRIP. |
| 16 | + */ |
| 17 | +public class CoreCommandLineHelper { |
| 18 | + |
| 19 | + public static final String FILE_OPTION = "f"; // "f" for "file" |
| 20 | + public static final String PORT_OPTION = "p"; // "p" for "port" |
| 21 | + public static final String HELP_OPTION = "h"; // "h" for "help" (this is standard) |
| 22 | + public static final String VERSION_OPTION = "v"; // "v" for "version" (this is standard) |
| 23 | + |
| 24 | + private final Options options = new Options(); |
| 25 | + private static final Option saveOption = |
| 26 | + Option.builder(FILE_OPTION) |
| 27 | + .longOpt("file") |
| 28 | + .desc("Set the GRIP save file to load") |
| 29 | + .hasArg() |
| 30 | + .numberOfArgs(1) |
| 31 | + .argName("path") |
| 32 | + .build(); |
| 33 | + private static final Option portOption = |
| 34 | + Option.builder(PORT_OPTION) |
| 35 | + .longOpt("port") |
| 36 | + .desc("Set the port to run the HTTP server on") |
| 37 | + .hasArg() |
| 38 | + .numberOfArgs(1) |
| 39 | + .argName("port") |
| 40 | + .build(); |
| 41 | + private static final Option helpOption |
| 42 | + = new Option(HELP_OPTION, "help", false, "Prints the command line options"); |
| 43 | + private static final Option versionOption |
| 44 | + = new Option(VERSION_OPTION, "version", false, "Prints the version of GRIP"); |
| 45 | + |
| 46 | + /** |
| 47 | + * Creates a new core commandline helper with the standard options. |
| 48 | + */ |
| 49 | + public CoreCommandLineHelper() { |
| 50 | + options.addOption(saveOption); |
| 51 | + options.addOption(portOption); |
| 52 | + options.addOption(helpOption); |
| 53 | + options.addOption(versionOption); |
| 54 | + } |
| 55 | + |
| 56 | + /** |
| 57 | + * Creates a command line helper with all the standard options, plus any additional options |
| 58 | + * required by subclasses. |
| 59 | + * |
| 60 | + * @param additionalOptions additional command line arguments |
| 61 | + */ |
| 62 | + protected CoreCommandLineHelper(Option... additionalOptions) { |
| 63 | + this(); |
| 64 | + for (Option o : additionalOptions) { |
| 65 | + options.addOption(o); |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + /** |
| 70 | + * Parses an array of command line arguments. If there are unknown arguments or the arguments are |
| 71 | + * otherwise malformed, a help message will be printed and the application will exit without |
| 72 | + * returning from this method. This will also occur if the help option is specified. |
| 73 | + * |
| 74 | + * @param args the command line arguments to parse |
| 75 | + * |
| 76 | + * @return a CommandLine object that can be queried for command line options and their values |
| 77 | + */ |
| 78 | + @SuppressWarnings({"checkstyle:regexp", "PMD.SystemPrintln"}) |
| 79 | + public CommandLine parse(String... args) { |
| 80 | + try { |
| 81 | + DefaultParser parser = new DefaultParser(); |
| 82 | + CommandLine parsed = parser.parse(options, args); |
| 83 | + if (parsed.hasOption(HELP_OPTION)) { |
| 84 | + printHelpAndExit(); |
| 85 | + } else if (parsed.hasOption(VERSION_OPTION)) { |
| 86 | + printVersionAndExit(); |
| 87 | + } |
| 88 | + return parsed; |
| 89 | + } catch (ParseException e) { |
| 90 | + System.out.println("Incorrect command line arguments: " + e.getMessage()); |
| 91 | + printHelpAndExit(); |
| 92 | + } |
| 93 | + return null; // This is OK -- will only happen in tests |
| 94 | + } |
| 95 | + |
| 96 | + /** |
| 97 | + * Parses a list of command line arguments. This coverts the list to an array and passes it to |
| 98 | + * {@link #parse(String[])}. |
| 99 | + * |
| 100 | + * @see #parse(String[]) |
| 101 | + */ |
| 102 | + public CommandLine parse(List<String> args) { |
| 103 | + return parse(args.toArray(new String[args.size()])); |
| 104 | + } |
| 105 | + |
| 106 | + /** |
| 107 | + * Prints a help message for the command line arguments and exits the application. |
| 108 | + */ |
| 109 | + @VisibleForTesting |
| 110 | + void printHelpAndExit() { |
| 111 | + HelpFormatter hf = new HelpFormatter(); |
| 112 | + hf.printHelp("GRIP", options); |
| 113 | + exit(); |
| 114 | + } |
| 115 | + |
| 116 | + /** |
| 117 | + * Prints the app version and exits the application. |
| 118 | + */ |
| 119 | + @VisibleForTesting |
| 120 | + @SuppressWarnings({"checkstyle:regexp", "PMD.SystemPrintln"}) |
| 121 | + void printVersionAndExit() { |
| 122 | + System.out.printf( |
| 123 | + "GRIP version %s%n", |
| 124 | + CoreCommandLineHelper.class.getPackage().getImplementationVersion() |
| 125 | + ); |
| 126 | + exit(); |
| 127 | + } |
| 128 | + |
| 129 | + /** |
| 130 | + * Exits the app. This method only exists so testing can work. |
| 131 | + */ |
| 132 | + @VisibleForTesting |
| 133 | + void exit() { |
| 134 | + System.exit(0); |
| 135 | + } |
| 136 | + |
| 137 | +} |
0 commit comments