Advertisement
Guest User

Untitled

a guest
Jul 20th, 2017
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.19 KB | None | 0 0
  1. package org.ossnipes.snipes.bot;
  2.  
  3. public class ArgumentParser
  4. {
  5.     private static final ArgumentParser inst = new ArgumentParser();
  6.  
  7.     public static ArgumentParser getParser()
  8.     {
  9.         return inst;
  10.     }
  11.  
  12.     public static ArgumentParser getParser(boolean debugCreateNew)
  13.     {
  14.         return debugCreateNew ? new ArgumentParser() : inst;
  15.     }
  16.  
  17.     public void parseArgs(Configuration c, String[] args)
  18.     {
  19.         int i = 0, j;
  20.         String arg;
  21.  
  22.         while (i < args.length && args[i].startsWith("-"))
  23.         {
  24.             arg = args[i++];
  25.  
  26.             if (arg.equals("--verbose"))
  27.             {
  28.                 this.onVerbose(c);
  29.             }
  30.  
  31.             else if (arg.equals("--define"))
  32.             {
  33.                 if (i < args.length)
  34.                 {
  35.                     this.onDefine(c, arg, args[i++]);
  36.                 }
  37.                 else
  38.                 {
  39.                     System.err
  40.                             .println("--define requires a property=value argument.");
  41.                     break;
  42.                 }
  43.  
  44.             }
  45.  
  46.             // Check for small flag args.
  47.             else
  48.             {
  49.                 for (j = 0; j < arg.length(); j++)
  50.                 {
  51.                     char flag = arg.charAt(j);
  52.                     switch (flag)
  53.                     {
  54.                         case '-':
  55.                         {
  56.                             // The initial -
  57.                             break;
  58.                         }
  59.                         case 'v':
  60.                         {
  61.                             // Verbose on
  62.                             this.onVerbose(c);
  63.                             break;
  64.                         }
  65.                         case 'D':
  66.                         {
  67.                             if (i < args.length)
  68.                             {
  69.                                 this.onDefine(c, arg, args[i++]);
  70.                             }
  71.                             else
  72.                             {
  73.                                 System.err
  74.                                         .println("-D requires a property=value argument.");
  75.                                 break;
  76.                             }
  77.                             break;
  78.                         }
  79.                         default:
  80.                             System.err.println("Illegal option " + flag);
  81.                         break;
  82.                     }
  83.                 }
  84.             }
  85.         }
  86.         if (i != args.length)
  87.         {
  88.             System.exit(Exit.EXIT_INCORRECTARGSSYN.ordinal());
  89.         }
  90.     }
  91.  
  92.     private void define(Configuration c, String key, String value)
  93.     {
  94.         c.setTempProperty(key, value);
  95.     }
  96.  
  97.     private void onVerbose(Configuration c)
  98.     {
  99.         this.define(c, "verbose", "TRUE");
  100.     }
  101.  
  102.     private boolean onDefine(Configuration c, String arg, String arg2)
  103.     {
  104.         String[] eqSplit = arg2.split("=");
  105.         if (eqSplit.length != 2)
  106.         {
  107.             System.err
  108.                     .println("Define directive: Argument invalid. Argument should look like: \""
  109.                             + arg + " myproperty=myvalue\"");
  110.             return false;
  111.         }
  112.         else
  113.         {
  114.             this.define(c, eqSplit[0], eqSplit[1]);
  115.             return true;
  116.         }
  117.     }
  118. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement