@@ -0,0 +1,2139 @@
+package dev.kkorolyov.simpleopts;%0A%0Aimport java.util.HashMap;%0Aimport java.util.Map;%0A%0A/**%0A * Parses command line arguments using an %7B@code Options%7D object.%0A */%0Apublic class ArgumentParser %7B%0A%09private Options validOptions;%0A%09private String%5B%5D args;%0A%09private Map%3COption, String%3E options = new HashMap%3C%3E();%0A%09%0A%09/**%0A%09 * Constructs a new %7B@code ArgumentParser%7D with set valid options for an array of arguments.%0A%09 * The parser will attempt to parse the arguments array upon instantiation.%0A%09 * @param validOptions set of all options recognized by this parser%0A%09 * @param args arguments array to parse%0A%09 * @throws IllegalArgumentException if any option in %7B@code args%7D fails to match to an option in %7B@code validOptions%7D, or any option requiring an argument does not receive a valid argument%0A%09 */%0A%09public ArgumentParser(Options validOptions, String%5B%5D args) %7B%0A%09%09setValidOptions(validOptions);%0A%09%09setArgs(args);%0A%09%09%0A%09%09parse();%0A%09%7D%0A%09private void parse() %7B%0A%09%09int counter = 0;%0A%09%09%0A%09%09while (counter %3C args.length) %7B%0A%09%09%09String currentArg = args%5Bcounter++%5D;%0A%09%09%09Option currentOption = validOptions.get(currentArg);%0A%09%09%09%0A%09%09%09if (currentOption == null)%0A%09%09%09%09throw new IllegalArgumentException(%22Not a valid option: %22 + currentArg);%0A%09%09%09%0A%09%09%09String currentOptionArg = currentOption.requiresArg() ? args%5Bcounter++%5D : null;%0A%09%09%09%0A%09%09%09if (currentOption.requiresArg() && (currentOptionArg == null %7C%7C validOptions.contains(currentOptionArg)))%0A%09%09%09%09throw new IllegalArgumentException(%22Not a valid argument for %22 + currentOption.getLongName() + %22: %22 + currentOptionArg);%0A%09%09%09%0A%09%09%09options.put(currentOption, currentOptionArg);%0A%09%09%7D%0A%09%7D%09%0A%09%0A%09/** @return the set of valid options recognized by this parser */%0A%09public Options getValidOptions() %7B%0A%09%09return validOptions;%0A%09%7D%0A%09private void setValidOptions(Options newValidOptions) %7B%0A%09%09validOptions = newValidOptions;%0A%09%7D%0A%09%0A%09/** @return the arguments parsed by this parser */%0A%09public String%5B%5D getArgs() %7B%0A%09%09return args;%0A%09%7D%0A%09private void setArgs(String%5B%5D newArgs) %7B%0A%09%09args = newArgs;%0A%09%7D%0A%09%0A%09/** @return all options obtained by this parser and their respective arguments, if applicable */%0A%09public Map%3COption, String%3E getOptions() %7B%0A%09%09return options;%0A%09%7D%0A%7D%0A

Add ArgumentParser