All Downloads are FREE. Search and download functionalities are using the official Maven repository.

edu.uiuc.ncsa.security.util.cli.InputLine Maven / Gradle / Ivy

package edu.uiuc.ncsa.security.util.cli;

import java.util.LinkedList;
import java.util.List;
import java.util.Vector;

/**
 * A utility to take an input line and turn it into a command line. The zero-th index is
 * always the command and is returned in lower case. The remaining arguments may be gotten
 * as a String (default from {@link #getArg(int)}) or as an integer (from {@link #getIntArg(int)}.
 * Supplying an empty command line will throw a {@link CommandNotFoundException} if you try to get it,
 * so check that the command line is not empty.
 * 

Created by Jeff Gaynor
* on 5/17/13 at 11:10 AM */ public class InputLine { public static final String COMMAND_DELIMITER = ""; public InputLine(Vector v) { parsedInput = v; } /** * This returns this as a string * * @return */ public String[] argsToStringArray() { String[] out = null; if (parsedInput != null && !parsedInput.isEmpty()) { out = new String[parsedInput.size() - 1]; // first pass tells whether to put a blank between arguments. The zero-th // element of the vector is the function, so omit that. for (int i =1; i< parsedInput.size(); i++) { out[i-1] = parsedInput.get(i); } } return out; } public List getArgs() { if (parsedInput.isEmpty() || parsedInput.size() == 1) { return new LinkedList(); } return parsedInput.subList(1, parsedInput.size()); } List parsedInput; public String getCommand() { if (parsedInput.isEmpty()) throw new CommandNotFoundException(); return parsedInput.get(0).toLowerCase(); } public String getLastArg(){ if(size() == 0){ throw new ArgumentNotFoundException(); } return getArg(size()-1); } public String getArg(int index) { if (0 <= parsedInput.size() && index + 1 <= parsedInput.size()) { return parsedInput.get(index); } throw new ArgumentNotFoundException(); } public int getIntArg(int index) { try { return Integer.parseInt(getArg(index)); } catch (NumberFormatException nfx) { throw new ArgumentNotFoundException("Error: the argument /" + getArg(index) + "/ cannot be parsed as an integer"); } } /** * Returns true if this command line was created with an empty string. * * @return */ public boolean isEmpty() { return parsedInput.isEmpty(); } public int size() { return parsedInput.size(); } /** * 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); } /** * If this command line has arguments at all. * * @return */ public boolean hasArgs() { return 1 < size(); } /** * 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 = 0; for (String x : getArgs()) { if (x.equals(arg)) return index; index++; } return -1; } }





© 2015 - 2025 Weber Informatics LLC | Privacy Policy