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); | |
| 12 | Map<String, String[]> rules = new HashMap<String, String[]>(); | |
| 13 | ||
| 14 | rules.put("filmes", new String[]{"nome_ator1", "nome_ator2", "decada"});
| |
| 15 | rules.put("episodios", new String[]{"nome_ator", "titulo_serie"});
| |
| 16 | rules.put("generos", new String[]{"nome_ator"});
| |
| 17 | - | ValidateArgs(parsedArgs, rules); |
| 17 | + | |
| 18 | if(!ValidateArgs(parsedArgs, rules, 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()) | |
| 25 | System.out.println("\t" + subArg);
| |
| 26 | } | |
| 27 | } | |
| 28 | - | Map<String, List<String>> result = new HashMap<String, List<String>>(); |
| 28 | + | |
| 29 | public static Map<String, List<String>> ParseArgs(String[] args) {
| |
| 30 | Map<String, List<String>> result = new LinkedHashMap<String, List<String>>(); | |
| 31 | List<String> subArgs = null; | |
| 32 | ||
| 33 | for(String arg: args) | |
| 34 | if(arg.startsWith("-"))
| |
| 35 | result.put(arg.substring(1), subArgs = new ArrayList<String>()); | |
| 36 | - | else |
| 36 | + | |
| 37 | - | throw new IllegalArgumentException("Argumento Invalido!");
|
| 37 | + | |
| 38 | else | |
| 39 | result.put(arg, null); | |
| 40 | ||
| 41 | return result; | |
| 42 | - | public static void ValidateArgs(Map<String, List<String>> args, Map<String, String[]> rules) {
|
| 42 | + | |
| 43 | - | for(Map.Entry<String, String[]> rule: rules.entrySet()) {
|
| 43 | + | |
| 44 | - | List<String> subArgs = args.get(rule.getKey()); |
| 44 | + | public static Boolean ValidateArgs(Map<String, List<String>> args, Map<String, String[]> rules, Boolean aLeastOne) {
|
| 45 | - | String[] argNames = rule.getValue(); |
| 45 | + | Boolean result = true; |
| 46 | ||
| 47 | - | if(subArgs == null || subArgs.size() != argNames.length) {
|
| 47 | + | if(aLeastOne && args.isEmpty()) |
| 48 | - | String Msg = String.format("O parametro <%s> espera %d subargumentos.\nModo de uso: <-%s>"
|
| 48 | + | System.err.println("E necessario ao menos um parametro!");
|
| 49 | - | , rule.getKey(), argNames.length, rule.getKey()); |
| 49 | + | |
| 50 | for(Map.Entry<String, List<String>> arg: args.entrySet()) {
| |
| 51 | - | for(String str: argNames) |
| 51 | + | if(rules.containsKey(arg.getKey())) {
|
| 52 | - | Msg += " <" + str + ">"; |
| 52 | + | String[] subArgs = rules.get(arg.getKey()); |
| 53 | ||
| 54 | - | throw new InputMismatchException(Msg); |
| 54 | + | if(subArgs != null && (arg.getValue() == null || arg.getValue().size() != subArgs.length)) {
|
| 55 | String Msg = String.format("O parametro <-%s> espera %d subargumentos.\nModo de uso: <-%s>"
| |
| 56 | , arg.getKey(), subArgs.length, arg.getKey()); | |
| 57 | ||
| 58 | for(String str: subArgs) | |
| 59 | Msg += " <" + str + ">"; | |
| 60 | ||
| 61 | System.err.println(Msg); | |
| 62 | result = false; | |
| 63 | } | |
| 64 | } else {
| |
| 65 | System.err.println(String.format("Parametro desconhecido <%s%s>"
| |
| 66 | , arg.getValue() != null ? "-" : "", arg.getKey())); | |
| 67 | result = false; | |
| 68 | } | |
| 69 | } | |
| 70 | ||
| 71 | return result; | |
| 72 | } | |
| 73 | } |