91 lines
2.6 KiB
Java
91 lines
2.6 KiB
Java
package net.knarcraft.ffmpegconverter.parser;
|
|
|
|
import net.knarcraft.ffmpegconverter.utility.ListUtil;
|
|
|
|
/**
|
|
* A class representing a command argument
|
|
*/
|
|
public class ConverterArgument {
|
|
|
|
private final String name;
|
|
private final char shorthand;
|
|
private final boolean valueRequired;
|
|
private final ConverterArgumentValueType valueType;
|
|
|
|
/**
|
|
* Instantiates a converter argument
|
|
*
|
|
* @param name <p>The name of the argument which users has to type.</p>
|
|
* @param shorthand <p>A single character value for using the command.</p>
|
|
* @param valueRequired <p>Whether the argument must be followed by a valid value.</p>
|
|
* @param valueType <p>The type of value the argument requires.</p>
|
|
*/
|
|
public ConverterArgument(String name, char shorthand, boolean valueRequired, ConverterArgumentValueType valueType) {
|
|
this.name = name;
|
|
this.shorthand = shorthand;
|
|
this.valueRequired = valueRequired;
|
|
this.valueType = valueType;
|
|
}
|
|
|
|
/**
|
|
* Gets the argument name
|
|
*
|
|
* @return <p>The argument name.</p>
|
|
*/
|
|
public String getName() {
|
|
return name;
|
|
}
|
|
|
|
/**
|
|
* Gets the argument shorthand
|
|
*
|
|
* @return <p>The argument shorthand</p>
|
|
*/
|
|
public char getShorthand() {
|
|
return shorthand;
|
|
}
|
|
|
|
/**
|
|
* Gets whether the argument requires a value
|
|
*
|
|
* @return <p>Whether the argument requires a value.</p>
|
|
*/
|
|
public boolean isValueRequired() {
|
|
return valueRequired;
|
|
}
|
|
|
|
/**
|
|
* Tests whether the given value is valid for this converter argument
|
|
*
|
|
* @param value <p>The value to test.</p>
|
|
* @return <p>True if the argument is valid. False otherwise.</p>
|
|
*/
|
|
public boolean testArgumentValue(String value) {
|
|
if (value.isEmpty()) {
|
|
return !valueRequired;
|
|
}
|
|
if (valueRequired && value.startsWith("-")) {
|
|
return false;
|
|
}
|
|
switch (valueType) {
|
|
case BOOLEAN:
|
|
String lower = value.toLowerCase();
|
|
return lower.equals("true") || lower.equals("false");
|
|
case COMMA_SEPARATED_LIST:
|
|
ListUtil.getListFromCommaSeparatedString(value);
|
|
return true;
|
|
case STRING:
|
|
return true;
|
|
case INT:
|
|
try {
|
|
Integer.parseInt(value);
|
|
return true;
|
|
} catch (NumberFormatException exception) {
|
|
return false;
|
|
}
|
|
}
|
|
return false;
|
|
}
|
|
|
|
}
|