View difference between Paste ID: gGSZCQvu and y1LxLZNM
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
import java.util.Iterator;
8
9
public class ArgParser{
10
11
    public static void main(String[] args) {
12
        Map<String, List<String>> parsedArgs = parseArgs(args);
13
        Map<String, String[]> rules = new HashMap<String, String[]>();
14
        
15
        rules.put("-filmes", new String[]{"nome_ator1", "nome_ator2", "decada"});
16
        rules.put("-episodios", new String[]{"nome_ator", "titulo_serie"});
17
        rules.put("-generos", new String[]{"nome_ator"});
18-
        if(!validateArgs(parsedArgs, rules, true, true))
18+
19-
            return;
19+
        if(validateArgs(parsedArgs, rules, false, true, true))
20
            for(Map.Entry<String, List<String>> arg: parsedArgs.entrySet()) {
21-
        for(Map.Entry<String, List<String>> arg: parsedArgs.entrySet()) {
21+
                System.out.println(arg.getKey());
22-
            System.out.println(arg.getKey());
22+
                
23
                if(arg.getValue() != null)
24-
            if(arg.getValue() != null)
24+
                    for(String subArg: arg.getValue())
25-
                for(String subArg: arg.getValue())
25+
                        System.out.println("\t" + subArg);
26-
                    System.out.println("\t" + subArg);
26+
27
    }
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> params = null;
32
        
33
        for(String arg: args) 
34
            if(arg.startsWith("-"))
35
                result.put(arg, params = new ArrayList<String>());
36
            else if(params != null)
37
                params.add(arg);
38
            else 
39
                result.put(arg, null);
40
        
41
        return result;
42
    }
43
    
44
    public static Boolean validateArgs(Map<String, List<String>> args, Map<String, String[]> rules, Boolean anonArgs, Boolean aLeastOne, Boolean sanitize) {
45-
    public static Boolean validateArgs(Map<String, List<String>> args, Map<String, String[]> rules, Boolean anonArgs, Boolean aLeastOne) {
45+
46
        
47
        Iterator<Map.Entry<String, List<String>>> it = args.entrySet().iterator();
48-
        if(aLeastOne && args.isEmpty())
48+
        while(it.hasNext()) {
49
            Map.Entry<String, List<String>> arg = it.next();
50
            
51-
        for(Map.Entry<String, List<String>> arg: args.entrySet()) {
51+
52
            
53
            if(rules.containsKey(arg.getKey())) {
54
                if(params != null && (arg.getValue() == null || arg.getValue().size() != params.length)) {
55
                    String Msg = String.format("O argumento <%s> espera %d parametro(s).\nModo de uso: <%s>"
56
                                              , arg.getKey(), params.length, arg.getKey());
57
                    
58
                    for(String str: params)
59
                        Msg += " <" + str + ">";
60
                    
61
                    System.err.println(Msg);
62
                    result = false || sanitize;
63-
                    result = false;
63+
                    if(sanitize)
64
                        it.remove();
65
                }
66
            } else if(arg.getValue() != null || !anonArgs) {
67-
                result = false;
67+
68
                result = false || sanitize;
69
                if(sanitize)
70
                    it.remove();
71
            }
72
        }
73
        
74
        if(aLeastOne && args.isEmpty()) {
75
            System.err.println("E necessario ao menos um argumento!");
76
            result = false;
77
        }
78
        
79
        return result;
80
    }
81
}