Advertisement
HwapX

Java Argument Parser V4

Dec 17th, 2013
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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.        
  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.             if(arg.getValue() != null)
  25.                 for(String subArg: arg.getValue())
  26.                     System.out.println("\t" + subArg);
  27.         }
  28.     }
  29.  
  30.     public static Map<String, List<String>> parseArgs(String[] args) {
  31.         Map<String, List<String>> result = new LinkedHashMap<String, List<String>>();
  32.         List<String> params = null;
  33.        
  34.         for(String arg: args)
  35.             if(arg.startsWith("-"))
  36.                 result.put(arg, params = new ArrayList<String>());
  37.             else if(params != null)
  38.                 params.add(arg);
  39.             else
  40.                 result.put(arg, null);
  41.        
  42.         return result;
  43.     }
  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.         if(aLeastOne && args.isEmpty())
  49.             System.err.println("E necessario ao menos um argumento!");
  50.        
  51.         for(Map.Entry<String, List<String>> arg: args.entrySet()) {
  52.             String[] params = rules.get(arg.getKey());
  53.            
  54.             if(rules.containsKey(arg.getKey())) {
  55.                 if(params != null && (arg.getValue() == null || arg.getValue().size() != params.length)) {
  56.                     String Msg = String.format("O argumento <%s> espera %d parametro(s).\nModo de uso: <%s>"
  57.                                               , arg.getKey(), params.length, arg.getKey());
  58.                    
  59.                     for(String str: params)
  60.                         Msg += " <" + str + ">";
  61.                    
  62.                     System.err.println(Msg);
  63.                     result = false;
  64.                 }
  65.             } else if(arg.getValue() != null || !anonArgs) {
  66.                 System.err.println(String.format("Argumento desconhecido <%s>", arg.getKey()));
  67.                 result = false;
  68.             }
  69.         }
  70.        
  71.         return result;
  72.     }
  73. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement