/** * Check if the input line has the given argument. False is returned otherwise. * * @param arg * @return */ public boolean hasArg(String arg) { return -1 != indexOf(arg); }
/** * 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! } }
/** * This will take an input line and search for the arg, returning the next value. * E.g. if the input line is "-file y -id z -v" * then supplying "-file" as the key here will return "y". * If there is no such key, then a null is returned. * * @param inputLine * @param key * @return */ protected String getArgValue(InputLine inputLine, String key) { int index = inputLine.indexOf(key); if (index == -1) return null; // Remember that the input line has the name of the method as the zeroth argument, so // it is always at least 1 in length if (inputLine.size() + 1 == index) { //then this is the final argument and nothing follows return null; } return inputLine.getArg(index + 1); }
public void create_hash(InputLine inputLine) { if (showHelp(inputLine)) { showCreateHashHelp(); return; } String secret = null; if (inputLine.hasArg("-file")) { try { FileReader fis = new FileReader(inputLine.getArg(1 + inputLine.indexOf("-file"))); StringBuffer sb = new StringBuffer(); int i; while ((i = fis.read()) != -1) { sb.append((char) i); } fis.close(); secret = sb.toString(); } catch (Throwable t) { say("Error: could not read file: " + t.getMessage()); return; } } else { secret = inputLine.getLastArg(); } say("creating hash of " + secret); say(DigestUtils.sha1Hex(secret)); }
hasFile = true; } catch (FileNotFoundException e) { say("warning, could not find file at path " + inputLine.getArg(inputLine.indexOf("-file")));