/** * Returns the index of the target in the input line or a -1 if it does not occur. * * @param arg * @return */ public int indexOf(String arg) { int index = 1; // zero-th argument is the name of the method, so getArgs start at index 1. for (String x : getArgs()) { if (x.equals(arg)) return index; index++; } return -1; }
/** * This will find the index for the "key" and return the very next argument. This is a * very, very use idiom for retrieving arguments for options.<br/><br/> * E.g. If the command line were * <pre> * myfunc -x foo -y fnord -blarg * </pre> * Then * <pre> * getNextArgFor("-x"); * </pre> * would return "foo". On the other hand * <pre> * getNextArgFor("-blarg"); * </pre> * would return a null, since there is no possible argument for it. * * @param key * @return */ public String getNextArgFor(String key){ int index = indexOf(key); // NOTE that the indexOf command starts at 1, since the zeroth index is always omitted if(index ==getArgs().size()){ // so it is the last arg in the string and there cannot be another return null; } return getArg(1 + index); // finally, a result! } }