Last active
January 15, 2020 13:53
-
-
Save Swedz/ffb1c9dabd439629ae6e8d3cc67a360e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package net.swedz.lib; | |
import java.util.ArrayList; | |
import java.util.Arrays; | |
import java.util.List; | |
public class ArgumentReader { | |
private final String[] args; | |
private int readIndex; | |
/** | |
* Create a new argument reader. | |
* | |
* @param args the array of arguments | |
*/ | |
public ArgumentReader(String[] args) { | |
this.args = args; | |
this.readIndex = 0; | |
} | |
/** | |
* Get the arguments in the reader. | |
* | |
* @return the array of arguments | |
*/ | |
public String[] getArguments() { | |
return this.args; | |
} | |
/** | |
* Get the amount of arguments in the reader. | |
* | |
* @return the amount of arguments | |
*/ | |
public int size() { | |
return this.args.length; | |
} | |
/** | |
* Get the maximum index that can be read onto. | |
* | |
* @return the maximum index | |
*/ | |
public int getMaxReadIndex() { | |
return this.size() - 1; | |
} | |
/** | |
* Read the next argument as a string. | |
* | |
* @return the string argument | |
*/ | |
public String readString() { | |
String arg = this.args[this.readIndex]; | |
this.readIndex++; | |
return arg; | |
} | |
/** | |
* Read the next argument as an integer. | |
* | |
* @return the integer argument | |
*/ | |
public Integer readInt() { | |
try { | |
return Integer.parseInt(this.readString()); | |
} catch (NumberFormatException ex) { | |
return null; | |
} | |
} | |
/** | |
* Read the next argument as a double. | |
* | |
* @return the double argument | |
*/ | |
public Double readDouble() { | |
try { | |
return Double.parseDouble(this.readString()); | |
} catch (NumberFormatException ex) { | |
return null; | |
} | |
} | |
/** | |
* Read the next argument as a long. | |
* | |
* @return the long argument | |
*/ | |
public Long readLong() { | |
try { | |
return Long.parseLong(this.readString()); | |
} catch (NumberFormatException ex) { | |
return null; | |
} | |
} | |
/** | |
* Read the next argument as a boolean. | |
* | |
* @return the boolean argument | |
*/ | |
public boolean readBoolean() { | |
return Boolean.parseBoolean(this.readString()); | |
} | |
/** | |
* Read all of the arguments that start with a hyphen. | |
* | |
* @return the list of tags | |
*/ | |
public List<String> readTags() { | |
List<String> tags = new ArrayList<>(); | |
String tag; | |
while(this.readIndex < this.size() && (tag = this.args[this.readIndex]).startsWith("-")) { | |
tags.add(tag.substring(1)); | |
this.readIndex++; | |
} | |
return tags; | |
} | |
/** | |
* Read the remaining string arguments and join them together. | |
* | |
* @param delimiter the delimiter that separates each element | |
* @return the fully joined string | |
*/ | |
public String readRemaining(String delimiter) { | |
String arg = String.join(delimiter, Arrays.copyOfRange(this.args, this.readIndex, this.size())); | |
this.readIndex = this.size(); | |
return arg; | |
} | |
/** | |
* Read the remaining string arguments and join them together. | |
* | |
* @return the fully joined string | |
*/ | |
public String readRemaining() { | |
return this.readRemaining(" "); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This makes it very easy to read arguments (typically used for commands) as you do not need to handle casting and formatting your arguments to each of their respective types. The read methods will return null (or false in the case of boolean) if the value in the arguments doesn't match that type. The constructor takes an array of arguments (excluding the command label), typically equal to your full command line string split by spaces (note bukkit commands already do this for you).
Here is an example: