SHOW:
|
|
- or go back to the newest paste.
| 1 | import java.util.Map; | |
| 2 | import java.util.HashMap; | |
| 3 | import java.util.LinkedHashMap; | |
| 4 | import java.util.List; | |
| 5 | import java.util.ArrayList; | |
| 6 | import java.util.InputMismatchException; | |
| 7 | ||
| 8 | public class ArgParser{
| |
| 9 | ||
| 10 | public static void main(String[] args) {
| |
| 11 | - | Map<String, List<String>> parsedArgs = ParseArgs(args); |
| 11 | + | Map<String, List<String>> parsedArgs = parseArgs(args); |
| 12 | Map<String, String[]> rules = new HashMap<String, String[]>(); | |
| 13 | ||
| 14 | - | rules.put("filmes", new String[]{"nome_ator1", "nome_ator2", "decada"});
|
| 14 | + | rules.put("-filmes", new String[]{"nome_ator1", "nome_ator2", "decada"});
|
| 15 | - | rules.put("episodios", new String[]{"nome_ator", "titulo_serie"});
|
| 15 | + | rules.put("-episodios", new String[]{"nome_ator", "titulo_serie"});
|
| 16 | - | rules.put("generos", new String[]{"nome_ator"});
|
| 16 | + | rules.put("-generos", new String[]{"nome_ator"});
|
| 17 | ||
| 18 | - | if(!ValidateArgs(parsedArgs, rules, true)) |
| 18 | + | if(!validateArgs(parsedArgs, rules, true, true)) |
| 19 | return; | |
| 20 | ||
| 21 | for(Map.Entry<String, List<String>> arg: parsedArgs.entrySet()) {
| |
| 22 | System.out.println(arg.getKey()); | |
| 23 | ||
| 24 | - | for(String subArg: arg.getValue()) |
| 24 | + | if(arg.getValue() != null) |
| 25 | - | System.out.println("\t" + subArg);
|
| 25 | + | for(String subArg: arg.getValue()) |
| 26 | System.out.println("\t" + subArg);
| |
| 27 | } | |
| 28 | } | |
| 29 | - | public static Map<String, List<String>> ParseArgs(String[] args) {
|
| 29 | + | |
| 30 | public static Map<String, List<String>> parseArgs(String[] args) {
| |
| 31 | - | List<String> subArgs = null; |
| 31 | + | |
| 32 | List<String> params = null; | |
| 33 | ||
| 34 | for(String arg: args) | |
| 35 | - | result.put(arg.substring(1), subArgs = new ArrayList<String>()); |
| 35 | + | |
| 36 | - | else if(subArgs != null) |
| 36 | + | result.put(arg, params = new ArrayList<String>()); |
| 37 | - | subArgs.add(arg); |
| 37 | + | else if(params != null) |
| 38 | params.add(arg); | |
| 39 | else | |
| 40 | result.put(arg, null); | |
| 41 | ||
| 42 | return result; | |
| 43 | } | |
| 44 | - | public static Boolean ValidateArgs(Map<String, List<String>> args, Map<String, String[]> rules, Boolean aLeastOne) {
|
| 44 | + | |
| 45 | public static Boolean validateArgs(Map<String, List<String>> args, Map<String, String[]> rules, Boolean anonArgs, Boolean aLeastOne) {
| |
| 46 | Boolean result = true; | |
| 47 | ||
| 48 | - | System.err.println("E necessario ao menos um parametro!");
|
| 48 | + | |
| 49 | System.err.println("E necessario ao menos um argumento!");
| |
| 50 | ||
| 51 | for(Map.Entry<String, List<String>> arg: args.entrySet()) {
| |
| 52 | - | String[] subArgs = rules.get(arg.getKey()); |
| 52 | + | String[] params = rules.get(arg.getKey()); |
| 53 | - | |
| 53 | + | |
| 54 | - | if(subArgs != null && (arg.getValue() == null || arg.getValue().size() != subArgs.length)) {
|
| 54 | + | |
| 55 | - | String Msg = String.format("O parametro <-%s> espera %d subargumentos.\nModo de uso: <-%s>"
|
| 55 | + | if(params != null && (arg.getValue() == null || arg.getValue().size() != params.length)) {
|
| 56 | - | , arg.getKey(), subArgs.length, arg.getKey()); |
| 56 | + | String Msg = String.format("O argumento <%s> espera %d parametro(s).\nModo de uso: <%s>"
|
| 57 | , arg.getKey(), params.length, arg.getKey()); | |
| 58 | - | for(String str: subArgs) |
| 58 | + | |
| 59 | for(String str: params) | |
| 60 | Msg += " <" + str + ">"; | |
| 61 | ||
| 62 | System.err.println(Msg); | |
| 63 | result = false; | |
| 64 | - | } else {
|
| 64 | + | |
| 65 | - | System.err.println(String.format("Parametro desconhecido <%s%s>"
|
| 65 | + | } else if(arg.getValue() != null || !anonArgs) {
|
| 66 | - | , arg.getValue() != null ? "-" : "", arg.getKey())); |
| 66 | + | System.err.println(String.format("Argumento desconhecido <%s>", arg.getKey()));
|
| 67 | result = false; | |
| 68 | } | |
| 69 | } | |
| 70 | ||
| 71 | return result; | |
| 72 | } | |
| 73 | } |