Subversion Repositories HomeAutomation

Rev

Blame | Last modification | View Log | SVN | RSS feed

  1. package utils;
  2.  
  3. import java.util.*;
  4.  
  5.  
  6. /**
  7.  * This class parses arguments from the command line and
  8.  * make them easely accessable.
  9.  *
  10.  * @author      Mattias Runge
  11.  * @author      Kristian Holmberg
  12.  * @version     1.1
  13.  */
  14. public class CommandLineParse
  15. {
  16.     private ArrayList<Flag> myFlags = new ArrayList<Flag>();
  17.  
  18.     /**
  19.      * Constructor for this class
  20.      *
  21.      * @param args  The arguments specified at the command line
  22.      * @throws CommandLineParseException    Exception thrown if the input was bad
  23.      */
  24.     public CommandLineParse(String[] args, ArrayList<String> validFlags) throws CommandLineParseException
  25.     {
  26.         int c = 0;
  27.        
  28.         Flag lastFlag = null;
  29.        
  30.         while (c < args.length)
  31.         {
  32.             if (args[c].substring(0, 1).compareTo("-") == 0)
  33.             {
  34.                 lastFlag = new Flag(args[c].substring(1));
  35.                
  36.                 if (!validFlags.contains(lastFlag.getName()))
  37.                 {
  38.                     throw new CommandLineParseException("Invalid flag: " + lastFlag.getName());
  39.                 }
  40.                
  41.                 //System.out.println("DEBUG: flags.add(" + args[c].substring(1) + ")");
  42.                 myFlags.add(lastFlag);
  43.             }
  44.             else if (lastFlag != null)
  45.             {
  46.                 lastFlag.addArgument(args[c]);
  47.                 //System.out.println("DEBUG: lastFlag.addArgument(" + args[c] + ") - " + lastFlag.getName());
  48.             }
  49.             else
  50.             {
  51.                 throw new CommandLineParseException("Unknown flag: " + args[c]);
  52.             }
  53.            
  54.             c++;
  55.         }
  56.     }
  57.    
  58.     /**
  59.      * Method for checking if a certain flag was set
  60.      *
  61.      * @param flagName  The name of the flag to check
  62.      * @return Returns true if the flag was set otherwise false
  63.      */
  64.     public boolean isFlagSet(String flagName)
  65.     {
  66.         //System.out.println("DEBUG: isFlagSet(" + flagName + ")");
  67.         return myFlags.contains(new Flag(flagName));
  68.     }
  69.    
  70.     /**
  71.      * Method for getting the arguments passed to a certain flag
  72.      *
  73.      * @param flagName  The name of the flag to get the arguments for
  74.      * @throws CommandLineParseException    Throws an exception if the flag was not found
  75.      * @return Returns a ArrayList<String> with the arguments
  76.      */
  77.     public ArrayList<String> getFlagArguments(String flagName) throws CommandLineParseException
  78.     {
  79.         int index = myFlags.indexOf(new Flag(flagName));
  80.        
  81.         if (index == -1)
  82.         {
  83.             throw new CommandLineParseException("Flag name not found");
  84.         }
  85.        
  86.         return myFlags.get(index).getArguments();
  87.     }
  88.    
  89.     /**
  90.      * Private class a flag
  91.      *
  92.      */
  93.     private class Flag
  94.     {
  95.         private String myName = "";
  96.         private ArrayList<String> myArguments = new ArrayList<String>();
  97.        
  98.         /**
  99.          * Constructor for the Flag class
  100.          *
  101.          * @param flagName  This is the name of this flag
  102.          */
  103.         public Flag(String flagName)
  104.         {
  105.             myName = flagName;
  106.         }
  107.        
  108.         /**
  109.          * Returns the name of this flag
  110.          *
  111.          * @return The name of this flag
  112.          */
  113.         public String getName()
  114.         {
  115.             return myName;
  116.         }
  117.        
  118.         /**
  119.          * Method for adding an argument to this flag
  120.          *
  121.          * @param argumentName  The name of the argument to add
  122.          */
  123.         public void addArgument(String argumentName)
  124.         {
  125.             myArguments.add(argumentName);
  126.         }
  127.        
  128.         /**
  129.          * Returns the arguments for this flag
  130.          *
  131.          * @return The arguments for this flag
  132.          */
  133.         public ArrayList<String> getArguments()
  134.         {
  135.             return myArguments;
  136.         }
  137.        
  138.         /**
  139.          * Method checking if two flags are equal
  140.          *
  141.          * @param flag  Object of the type flag to check against
  142.          */
  143.         public boolean equals(Object flag)
  144.         {
  145.             String flagName = ((Flag)flag).getName();
  146.             //System.out.println("DEBUG: equals - " + flagName + " == " + name);
  147.             return (myName.compareTo(flagName) == 0);
  148.         }
  149.     }
  150. }
  151.