3b53068ff73871f04049520520c34e8cefde57ef
[vamsas.git] / src / uk / ac / vamsas / test / simpleclient / CommandProcessor.java
1 package uk.ac.vamsas.test.simpleclient;
2
3 import java.util.Iterator;
4 import java.util.Vector;
5
6 public class CommandProcessor {
7   /**
8    * this is not getOPT!!!! - processes a *series* of space separated commands - some of which take arguments.
9    */
10   private Vector commands;
11   /* static {
12     ClientsFileTest.commands=new Vector();
13     ClientsFileTest.commands.add(new String("add"));
14     ClientsFileTest.commands.add(new String("remove"));
15     ClientsFileTest.commands.add(new String("list"));    
16     ClientsFileTest.commands.add(new String("clear"));
17     ClientsFileTest.commands.add(new String("watch"));    
18     ClientsFileTest.commands.add(new String("monitor"));    
19   } */
20
21   public int addCommand(String cmd, int argneed, String complainString) {
22     int cnum=0;
23     if (commands==null)
24       commands = new Vector();
25     else 
26       cnum = commands.size();
27     Vector cv = new Vector();
28     cv.add(new String(cmd));
29     cv.add(new Integer(argneed));
30     cv.add(new String(complainString));
31     commands.add(cv);
32     return cnum;
33   }
34  
35   /**
36    * Integer argl, Integer argpos, String cmd, Integer argneed, String msg in vector
37    */
38   public void complainArgs(int argl, int argpos, Vector ca) {
39     int argneed = ((Integer) ca.get(1)).intValue();
40     if (argl-argpos<argneed)
41       throw new Error(((String) ca.get(0))+" at position "+argpos+" needs "+argneed+" arguments : "+(String) ca.get(2));
42   }
43   /**
44    * find and verify a command
45    * @param args argstring
46    * @param argpos position to check for command
47    * @return matching command or -1
48    */
49   public int getCommand(String[]args, int argpos) {
50     Iterator coms = commands.iterator();
51     int com=-1, argc;
52     argc=argpos;
53     while ((coms!=null) && coms.hasNext()) {
54       com++;
55       Vector comnext = (Vector) coms.next();
56       if (args[argc].toLowerCase().equals((String) comnext.get(0))) {
57         if (comnext.size()>2)
58           complainArgs(args.length, argc+1, comnext);
59         return com;
60       }
61     }
62     return -1;
63         
64   }
65 }