+++ /dev/null
-/*
- * Svjatoslav Commons - shared library of common functionality.
- * Copyright ©2012-2013, Svjatoslav Agejenko, svjatoslav@svjatoslav.eu
- *
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of version 2 of the GNU General Public License
- * as published by the Free Software Foundation.
- */
-
-package eu.svjatoslav.commons.commandline.parameterparser;
-
-public interface Argument {
-
- /**
- * @return Single line argument type description.
- */
- public String describeFormat();
-
- /**
- * @return <code>true</code> if value is correct, <code>false</code>
- * otherwise.
- */
- public boolean validate(String value);
-
-}
import java.util.ArrayList;
import java.util.List;
-public class Parameter {
+public abstract class Parameter {
/**
* Indicates that at least one argument is mandatory for this parameter.
*/
protected boolean mandatory;
- private final Argument argumentType;
-
private final ArrayList<String> aliases = new ArrayList<String>();
public final String description;
*/
private boolean parameterSpecified;
- public Parameter(final Argument argumentType, final String description,
- final ArgumentCount argumentCount) {
-
+ public Parameter(final String description, final ArgumentCount argumentCount) {
this.description = description;
- this.argumentType = argumentType;
this.argumentCount = argumentCount;
};
public Parameter(final boolean mandatory,
- final ArgumentCount argumentCount, final Argument argumentType,
- final String description, final String... aliases2) {
+ final ArgumentCount argumentCount, final String description,
+ final String... aliases2) {
this.mandatory = mandatory;
- this.argumentType = argumentType;
this.description = description;
this.argumentCount = argumentCount;
return false;
}
- if (!argumentType.validate(argumentString)) {
+ if (!validate(argumentString)) {
System.out.println("Error! Invalid argument \"" + argumentString
- + "\". It shall be " + argumentType.describeFormat() + ".");
+ + "\". It shall be " + describeFormat() + ".");
return false;
}
// first line
buffer.append(getAliases());
if (!argumentCount.equals(ArgumentCount.NONE)) {
- buffer.append(" (" + argumentType.describeFormat() + ")");
+ buffer.append(" (" + describeFormat() + ")");
if (argumentCount.equals(ArgumentCount.MULTI))
buffer.append("...");
this.parameterSpecified = parameterSpecified;
}
+ /**
+ * @return Single line argument type description.
+ */
+ public abstract String describeFormat();
+
+ /**
+ * @return <code>true</code> if value is correct, <code>false</code>
+ * otherwise.
+ */
+ public abstract boolean validate(String value);
+
}
parameters.add(parameter);
}
- public Parameter createCustomParameter(final Argument paramaterType,
- final String description) {
- final Parameter parameter = new Parameter(paramaterType, description,
- ArgumentCount.SINGLE);
- parameters.add(parameter);
- return parameter;
- }
-
public DirectoryParameter createDirectoryParameter(final String description) {
final DirectoryParameter parameter = new DirectoryParameter(description);
parameters.add(parameter);
import java.io.File;
-import eu.svjatoslav.commons.commandline.parameterparser.Argument;
import eu.svjatoslav.commons.commandline.parameterparser.ArgumentCount;
import eu.svjatoslav.commons.commandline.parameterparser.Parameter;
-import eu.svjatoslav.commons.commandline.parameterparser.type.StringArgument;
-public class DirectoryParameter extends Parameter implements Argument {
+public class DirectoryParameter extends Parameter {
private ExistanceType existanceType = ExistanceType.DOES_NOT_MATTER;
public DirectoryParameter(final String description) {
- super(new StringArgument(), description, ArgumentCount.SINGLE);
+ super(description, ArgumentCount.SINGLE);
}
@Override
import java.io.File;
-import eu.svjatoslav.commons.commandline.parameterparser.Argument;
import eu.svjatoslav.commons.commandline.parameterparser.ArgumentCount;
import eu.svjatoslav.commons.commandline.parameterparser.Parameter;
-import eu.svjatoslav.commons.commandline.parameterparser.type.StringArgument;
-public class FileParameter extends Parameter implements Argument {
+public class FileParameter extends Parameter {
private ExistanceType existanceType = ExistanceType.DOES_NOT_MATTER;
public FileParameter(final String description) {
- super(new StringArgument(), description, ArgumentCount.SINGLE);
+ super(description, ArgumentCount.SINGLE);
}
@Override
import eu.svjatoslav.commons.commandline.parameterparser.ArgumentCount;
import eu.svjatoslav.commons.commandline.parameterparser.Parameter;
-import eu.svjatoslav.commons.commandline.parameterparser.type.IntegerArgument;
public class IntegerParameter extends Parameter {
public IntegerParameter(final String description) {
- super(new IntegerArgument(), description, ArgumentCount.SINGLE);
+ super(description, ArgumentCount.SINGLE);
}
@Override
mandatory = true;
return this;
}
+
+ @Override
+ public java.lang.String describeFormat() {
+ return "integer";
+ }
+
+ @Override
+ public boolean validate(final java.lang.String value) {
+ try {
+ java.lang.Integer.valueOf(value);
+ return true;
+ } catch (final NumberFormatException e) {
+ return false;
+ }
+ }
+
}
public class NullParameter extends Parameter {
public NullParameter(final String description) {
- super(null, description, ArgumentCount.NONE);
+ super(description, ArgumentCount.NONE);
}
@Override
mandatory = true;
return this;
}
+
+ @Override
+ public java.lang.String describeFormat() {
+ return "<none>";
+ }
+
+ @Override
+ public boolean validate(final java.lang.String value) {
+ return true;
+ }
+
}
import eu.svjatoslav.commons.commandline.parameterparser.ArgumentCount;
import eu.svjatoslav.commons.commandline.parameterparser.Parameter;
-import eu.svjatoslav.commons.commandline.parameterparser.type.StringArgument;
public class StringParameter extends Parameter {
public StringParameter(final String description) {
- super(new StringArgument(), description, ArgumentCount.SINGLE);
+ super(description, ArgumentCount.SINGLE);
}
@Override
return this;
}
+ @Override
+ public java.lang.String describeFormat() {
+ return "string";
+ }
+
+ @Override
+ public boolean validate(final java.lang.String value) {
+ return true;
+ }
+
}
+++ /dev/null
-/*
- * Svjatoslav Commons - shared library of common functionality.
- * Copyright ©2012-2013, Svjatoslav Agejenko, svjatoslav@svjatoslav.eu
- *
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of version 2 of the GNU General Public License
- * as published by the Free Software Foundation.
- */
-
-package eu.svjatoslav.commons.commandline.parameterparser.type;
-
-import eu.svjatoslav.commons.commandline.parameterparser.Argument;
-
-public class IntegerArgument implements Argument {
-
- @Override
- public java.lang.String describeFormat() {
- return "integer";
- }
-
- @Override
- public boolean validate(final java.lang.String value) {
- try {
- java.lang.Integer.valueOf(value);
- return true;
- } catch (final NumberFormatException e) {
- return false;
- }
- }
-
-}
+++ /dev/null
-/*
- * Svjatoslav Commons - shared library of common functionality.
- * Copyright ©2012-2013, Svjatoslav Agejenko, svjatoslav@svjatoslav.eu
- *
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of version 2 of the GNU General Public License
- * as published by the Free Software Foundation.
- */
-
-package eu.svjatoslav.commons.commandline.parameterparser.type;
-
-import eu.svjatoslav.commons.commandline.parameterparser.Argument;
-
-public class StringArgument implements Argument {
-
- @Override
- public java.lang.String describeFormat() {
- return "string";
- }
-
- @Override
- public boolean validate(final java.lang.String value) {
- return true;
- }
-
-}