Advertisement
HwapX

Java Argument Parser V3

Dec 17th, 2013
126
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))
  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.  
  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 if(subArgs != null)
  37.                 subArgs.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 aLeastOne) {
  45.         Boolean result = true;
  46.        
  47.         if(aLeastOne && args.isEmpty())
  48.             System.err.println("E necessario ao menos um parametro!");
  49.        
  50.         for(Map.Entry<String, List<String>> arg: args.entrySet()) {
  51.             if(rules.containsKey(arg.getKey())) {
  52.                 String[] subArgs = rules.get(arg.getKey());
  53.                
  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. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement