SHOW:
|
|
- or go back to the newest paste.
| 1 | import java.util.Map; | |
| 2 | import java.util.HashMap; | |
| 3 | import java.util.List; | |
| 4 | import java.util.ArrayList; | |
| 5 | import java.util.InputMismatchException; | |
| 6 | ||
| 7 | public class ArgParser{
| |
| 8 | ||
| 9 | public static void main(String[] args) {
| |
| 10 | Map<String, List<String>> parsedArgs = ParseArgs(args); | |
| 11 | - | Map<String, Integer> rules = new HashMap<String, Integer>(); |
| 11 | + | Map<String, String[]> rules = new HashMap<String, String[]>(); |
| 12 | - | rules.put("filmes", 3);
|
| 12 | + | |
| 13 | - | rules.put("episodios", 2);
|
| 13 | + | rules.put("filmes", new String[]{"nome_ator1", "nome_ator2", "decada"});
|
| 14 | - | rules.put("generos", 1);
|
| 14 | + | rules.put("episodios", new String[]{"nome_ator", "titulo_serie"});
|
| 15 | rules.put("generos", new String[]{"nome_ator"});
| |
| 16 | ||
| 17 | ValidateArgs(parsedArgs, rules); | |
| 18 | ||
| 19 | for(Map.Entry<String, List<String>> arg: parsedArgs.entrySet()) {
| |
| 20 | System.out.println(arg.getKey()); | |
| 21 | ||
| 22 | for(String subArg: arg.getValue()) | |
| 23 | System.out.println("\t" + subArg);
| |
| 24 | } | |
| 25 | } | |
| 26 | ||
| 27 | public static Map<String, List<String>> ParseArgs(String[] args) {
| |
| 28 | - | for(String arg: args) {
|
| 28 | + | |
| 29 | - | if(arg.startsWith("-")) {
|
| 29 | + | |
| 30 | - | subArgs = new ArrayList<String>(); |
| 30 | + | |
| 31 | - | result.put(arg.substring(1), subArgs); |
| 31 | + | for(String arg: args) |
| 32 | if(arg.startsWith("-"))
| |
| 33 | - | else {
|
| 33 | + | result.put(arg.substring(1), subArgs = new ArrayList<String>()); |
| 34 | - | if(subArgs != null) |
| 34 | + | else if(subArgs != null) |
| 35 | - | subArgs.add(arg); |
| 35 | + | subArgs.add(arg); |
| 36 | - | else |
| 36 | + | else |
| 37 | - | throw new IllegalArgumentException("Invalid argument");
|
| 37 | + | throw new IllegalArgumentException("Argumento Invalido!");
|
| 38 | ||
| 39 | return result; | |
| 40 | } | |
| 41 | ||
| 42 | public static void ValidateArgs(Map<String, List<String>> args, Map<String, String[]> rules) {
| |
| 43 | for(Map.Entry<String, String[]> rule: rules.entrySet()) {
| |
| 44 | - | public static void ValidateArgs(Map<String, List<String>> args, Map<String, Integer> rules) {
|
| 44 | + | |
| 45 | - | for(Map.Entry<String, Integer> rule: rules.entrySet()) {
|
| 45 | + | String[] argNames = rule.getValue(); |
| 46 | ||
| 47 | - | if(subArgs == null || subArgs.size() != rule.getValue()) |
| 47 | + | if(subArgs == null || subArgs.size() != argNames.length) {
|
| 48 | - | throw new InputMismatchException("Expected subargument count " + rule.getValue());
|
| 48 | + | String Msg = String.format("O parametro <%s> espera %d subargumentos.\nModo de uso: <-%s>"
|
| 49 | , rule.getKey(), argNames.length, rule.getKey()); | |
| 50 | ||
| 51 | for(String str: argNames) | |
| 52 | Msg += " <" + str + ">"; | |
| 53 | ||
| 54 | throw new InputMismatchException(Msg); | |
| 55 | } | |
| 56 | } | |
| 57 | } | |
| 58 | } |