import java.util.ArrayList;
import java.util.Collections;
-public abstract class Parameter<T> {
+public abstract class Parameter<T, I extends Parameter> {
- public final String description;
+ public String description;
public final ArrayList<String> arguments = new ArrayList<>();
- final ArgumentCount argumentCount;
+ ArgumentCount argumentCount;
private final ArrayList<String> aliases = new ArrayList<>();
/**
* Indicates that at least one argument is mandatory for this parameter.
* If this parameter was present in the commandline, then this boolean will
* be set to <code>true</code>.
*/
- private boolean parameterSpecified;
+ private boolean specified;
public Parameter(final boolean mandatory,
final ArgumentCount argumentCount, final String description,
}
@SuppressWarnings("unchecked")
- public T addAliases(final String... aliasArray) {
+ public I addAliases(final String... aliasArray) {
// save aliases
Collections.addAll(aliases, aliasArray);
- return (T) this;
+ return (I)this;
}
/**
return buffer.toString();
}
- public abstract Object getValue();
+ public abstract T getValue();
public boolean isMandatory() {
return mandatory;
/**
* @return the parameterSpecified
*/
- public boolean isParameterSpecified() {
- return parameterSpecified;
+ public boolean isSpecified() {
+ return specified;
}
/**
- * @param parameterSpecified the parameterSpecified to set
+ * @param specified the parameterSpecified to set
*/
- public void setParameterSpecified(final boolean parameterSpecified) {
- this.parameterSpecified = parameterSpecified;
+ protected void setSpecified(final boolean specified) {
+ this.specified = specified;
}
/**
}
@SuppressWarnings("unchecked")
- public T setMandatory() {
+ public I setMandatory() {
mandatory = true;
- return (T) this;
+ return (I)this;
}
/**
*/
public class Parser {
- private final ArrayList<Parameter<?>> parameters = new ArrayList<>();
+ private final ArrayList<Parameter<?, ? extends Parameter>> parameters = new ArrayList<>();
- public <E extends Parameter<?>> E add(final E parameter) {
+ public <E extends Parameter> E add(final E parameter) {
parameters.add(parameter);
return parameter;
}
*/
private boolean checkMandatoryArgumentsPresent() {
- for (final Parameter<?> parameter : parameters)
- if (parameter.isMandatory() && (!parameter.isParameterSpecified())) {
+ for (final Parameter parameter : parameters)
+ if (parameter.isMandatory() && (!parameter.isSpecified())) {
System.out.println("Error! Mandatory parameter ("
+ parameter.getAliases() + ") is not specified.");
return false;
* @param alias parameter alias
* @return found parameter or <code>null</code> if parameter was not found.
*/
- public Parameter<?> findParameterByAlias(final String alias) {
+ public Parameter findParameterByAlias(final String alias) {
- for (final Parameter<?> parameter : parameters)
+ for (final Parameter parameter : parameters)
if (parameter.matchesAlias(alias))
return parameter;
*/
public boolean parse(final String[] args) {
- Parameter<?> currentParameter = null;
+ Parameter currentParameter = null;
for (final String argument : args) {
- final Parameter<?> parameterForAlias = findParameterByAlias(argument);
+ final Parameter parameterForAlias = findParameterByAlias(argument);
if (parameterForAlias == null) {
if (currentParameter == null) {
System.out.println("Unknown commandline parameter: "
if (!currentParameter.noMoreArguments())
return false;
- parameterForAlias.setParameterSpecified(true);
+ parameterForAlias.setSpecified(true);
currentParameter = parameterForAlias;
}
}
public void showHelp() {
- for (final Parameter<?> parameter : parameters)
+ for (final Parameter parameter : parameters)
System.out.println(parameter.getHelp());
}
import java.io.File;
-public class DirectoryParameter extends Parameter<DirectoryParameter> {
+public class DirectoryParameter extends Parameter<File, DirectoryParameter> {
private ExistenceType existenceType = ExistenceType.DOES_NOT_MATTER;
import java.io.File;
-public class FileParameter extends Parameter<FileParameter> {
+public class FileParameter extends Parameter<File, FileParameter> {
private ExistenceType existenceType = ExistenceType.DOES_NOT_MATTER;
import eu.svjatoslav.commons.commandline.parameterparser.ArgumentCount;
import eu.svjatoslav.commons.commandline.parameterparser.Parameter;
-public class IntegerParameter extends Parameter<IntegerParameter> {
+public class IntegerParameter extends Parameter<Integer, IntegerParameter> {
public IntegerParameter(final String description) {
super(description, ArgumentCount.SINGLE);
import eu.svjatoslav.commons.commandline.parameterparser.ArgumentCount;
import eu.svjatoslav.commons.commandline.parameterparser.Parameter;
-public class NullParameter extends Parameter<NullParameter> {
+public class NullParameter extends Parameter<Boolean, NullParameter> {
public NullParameter(final String description) {
super(description, ArgumentCount.NONE);
@Override
public Boolean getValue() {
- return isParameterSpecified();
+ return isSpecified();
}
@Override
import eu.svjatoslav.commons.commandline.parameterparser.ArgumentCount;
import eu.svjatoslav.commons.commandline.parameterparser.Parameter;
-public class StringParameter extends Parameter<StringParameter> {
+public class StringParameter extends Parameter<String, StringParameter> {
public StringParameter(final String description) {
super(description, ArgumentCount.SINGLE);
import java.util.List;
-public class StringParameters extends Parameter<StringParameters> {
+public class StringParameters extends Parameter<List<String>, StringParameters> {
public StringParameters(final String description) {
super(description, ArgumentCount.MULTI);
package eu.svjatoslav.commons.commandline.parameterparser;
+import eu.svjatoslav.commons.commandline.parameterparser.parameter.FileParameter;
import eu.svjatoslav.commons.commandline.parameterparser.parameter.StringParameter;
import org.junit.Before;
import org.junit.Test;
+import java.io.File;
+import java.io.FileWriter;
+import java.io.IOException;
+
import static org.junit.Assert.*;
public class ParserTest {
}
@Test
- public void testParse() {
+ public void testParse() throws IOException {
// define allowed parameters
- final StringParameter helpParameter = parser
- .add(new StringParameter("Show help screen"))
- .addAliases("--help", "-h").setMandatory();
+ final StringParameter helpParameter = parser.add(new StringParameter("Show help screen")
+ .addAliases("--help", "-h").setMandatory());
+
+ final StringParameter compileParameter = parser.add(new StringParameter("Compile code"))
+ .addAliases("--compile", "-c");
+
+ FileParameter fileParameter = parser.add(new FileParameter("Input file")
+ .addAliases("-i").mustExist());
- final StringParameter compileParameter = parser.add(
- new StringParameter("Compile code")).addAliases("--compile",
- "-c");
+ createTemporaryFile();
// check help generation
parser.showHelp();
// parse arguments
- parser.parse(new String[]{"--help", "section"});
+ parser.parse(new String[]{"--help", "section", "-i", "/tmp/file with spaces"});
// --help was in the arguments
- assertTrue(helpParameter.isParameterSpecified());
+ assertTrue(helpParameter.isSpecified());
// compile was not present
- assertFalse(compileParameter.isParameterSpecified());
+ assertFalse(compileParameter.isSpecified());
// validate that help argument was "section"
assertEquals("section", helpParameter.getValue());
+ assertTrue(fileParameter.isSpecified());
+ assertEquals("/tmp/file with spaces", fileParameter.getValue().getAbsolutePath());
+
+ }
+
+ private void createTemporaryFile() throws IOException {
+ File fileWithSpaces = new File("/tmp/file with spaces");
+ FileWriter writer = new FileWriter(fileWithSpaces);
+ writer.write("test");
+ writer.close();
}
}