More foolproof code.
/*
* Svjatoslav Commons - shared library of common functionality.
* Copyright ©2012-2014, Svjatoslav Agejenko, svjatoslav@svjatoslav.eu
- *
+ *
* This program is free software; you can redistribute it and/or
* modify it under the terms of version 3 of the GNU Lesser General Public License
* or later as published by the Free Software Foundation.
package eu.svjatoslav.commons.commandline.parameterparser;
-import java.io.File;
import java.util.ArrayList;
-import java.util.List;
public abstract class Parameter {
protected final ArrayList<String> arguments = new ArrayList<String>();
- ArgumentCount argumentCount;
+ final ArgumentCount argumentCount;
/**
* If this parameter was present in the commandline, then this boolean will
this.argumentCount = argumentCount;
}
- public Parameter addAliases(final String... aliasArray) {
+ public abstract Object addAliases(final String... aliasArray);
+
+ protected Parameter addAliasesProtected(final String... aliasArray) {
// save aliases
for (final String alias : aliasArray)
return buffer.toString();
}
- public List<File> getArgumentsAsFiles() {
- final ArrayList<File> result = new ArrayList<File>();
-
- for (final String argument : arguments) {
- final File file = new File(argument);
- result.add(file);
- }
-
- return result;
- }
-
- public List<Integer> getArgumentsAsIntegers() {
- final ArrayList<Integer> result = new ArrayList<Integer>();
-
- for (final String argument : arguments)
- result.add(Integer.valueOf(argument));
-
- return result;
- }
-
- public List<String> getArgumentsAsStrings() {
- final ArrayList<String> result = new ArrayList<String>(arguments);
- return result;
- }
+ // public List<File> getArgumentsAsFiles() {
+ // final ArrayList<File> result = new ArrayList<File>();
+ //
+ // for (final String argument : arguments) {
+ // final File file = new File(argument);
+ // result.add(file);
+ // }
+ //
+ // return result;
+ // }
+ //
+ // public List<Integer> getArgumentsAsIntegers() {
+ // final ArrayList<Integer> result = new ArrayList<Integer>();
+ //
+ // for (final String argument : arguments)
+ // result.add(Integer.valueOf(argument));
+ //
+ // return result;
+ // }
+ //
+ // public List<String> getArgumentsAsStrings() {
+ // final ArrayList<String> result = new ArrayList<String>(arguments);
+ // return result;
+ // }
public String getHelp() {
final StringBuffer buffer = new StringBuffer();
return buffer.toString();
}
+ public abstract Object getValue();
+
public boolean isMandatory() {
return mandatory;
}
/**
* Notifies this parameter that no more arguments will follow. This gives
* parameter chance to verify if this is ok.
- *
+ *
* @return <code>true</code> if no errors were found. <code>false</code>
* otherwise.
*/
return true;
}
- public Parameter setMandatory() {
+ public abstract Parameter setMandatory();
+
+ protected Parameter setMandatoryProtected() {
mandatory = true;
return this;
}
/*
* Svjatoslav Commons - shared library of common functionality.
* Copyright ©2012-2014, Svjatoslav Agejenko, svjatoslav@svjatoslav.eu
- *
+ *
* This program is free software; you can redistribute it and/or
* modify it under the terms of version 3 of the GNU Lesser General Public License
* or later as published by the Free Software Foundation.
import eu.svjatoslav.commons.commandline.parameterparser.parameter.IntegerParameter;
import eu.svjatoslav.commons.commandline.parameterparser.parameter.NullParameter;
import eu.svjatoslav.commons.commandline.parameterparser.parameter.StringParameter;
+import eu.svjatoslav.commons.commandline.parameterparser.parameter.StringParameters;
/**
* <pre>
return parameter;
}
+ public StringParameters createStringParameters(final String description) {
+ final StringParameters stingParameters = new StringParameters(
+ description);
+ parameters.add(stingParameters);
+ return stingParameters;
+ }
+
/**
* Return parameter by given alias or <code>null</code> if no parameter
* exists for given alias.
/*
* Svjatoslav Commons - shared library of common functionality.
* Copyright ©2012-2014, Svjatoslav Agejenko, svjatoslav@svjatoslav.eu
- *
+ *
* This program is free software; you can redistribute it and/or
* modify it under the terms of version 3 of the GNU Lesser General Public License
* or later as published by the Free Software Foundation.
@Override
public DirectoryParameter addAliases(final String... aliasArray) {
- super.addAliases(aliasArray);
+ super.addAliasesProtected(aliasArray);
return this;
}
return existanceType.description + "directory";
}
+ @Override
public File getValue() {
if (arguments.size() != 1)
@Override
public DirectoryParameter setMandatory() {
- mandatory = true;
+ setMandatoryProtected();
return this;
}
/*
* Svjatoslav Commons - shared library of common functionality.
* Copyright ©2012-2014, Svjatoslav Agejenko, svjatoslav@svjatoslav.eu
- *
+ *
* This program is free software; you can redistribute it and/or
* modify it under the terms of version 3 of the GNU Lesser General Public License
* or later as published by the Free Software Foundation.
public enum ExistanceType {
- MUST_EXIST("existing "), MUST_NOT_EXIST("not existing "), DOES_NOT_MATTER(
- "");
+ MUST_EXIST("existing"), MUST_NOT_EXIST("not existing"), DOES_NOT_MATTER("");
public final String description;
/*
* Svjatoslav Commons - shared library of common functionality.
* Copyright ©2012-2014, Svjatoslav Agejenko, svjatoslav@svjatoslav.eu
- *
+ *
* This program is free software; you can redistribute it and/or
* modify it under the terms of version 3 of the GNU Lesser General Public License
* or later as published by the Free Software Foundation.
@Override
public FileParameter addAliases(final String... aliasArray) {
- super.addAliases(aliasArray);
+ super.addAliasesProtected(aliasArray);
return this;
}
@Override
public java.lang.String describeFormat() {
- return existanceType.description + "file";
+ return existanceType.description + " file";
}
+ @Override
public File getValue() {
if (arguments.size() != 1)
@Override
public FileParameter setMandatory() {
- mandatory = true;
+ setMandatoryProtected();
return this;
}
/*
* Svjatoslav Commons - shared library of common functionality.
* Copyright ©2012-2014, Svjatoslav Agejenko, svjatoslav@svjatoslav.eu
- *
+ *
* This program is free software; you can redistribute it and/or
* modify it under the terms of version 3 of the GNU Lesser General Public License
* or later as published by the Free Software Foundation.
@Override
public IntegerParameter addAliases(final String... aliasArray) {
- super.addAliases(aliasArray);
+ super.addAliasesProtected(aliasArray);
return this;
}
return "integer";
}
- public int getValue() {
+ @Override
+ public Integer getValue() {
if (arguments.size() != 1)
throw new RuntimeException("Parameter " + description
+ " shall have exactly 1 argument.");
@Override
public IntegerParameter setMandatory() {
- mandatory = true;
+ setMandatoryProtected();
return this;
}
return false;
}
}
-
}
/*
* Svjatoslav Commons - shared library of common functionality.
* Copyright ©2012-2014, Svjatoslav Agejenko, svjatoslav@svjatoslav.eu
- *
+ *
* This program is free software; you can redistribute it and/or
* modify it under the terms of version 3 of the GNU Lesser General Public License
* or later as published by the Free Software Foundation.
@Override
public NullParameter addAliases(final String... aliasArray) {
- super.addAliases(aliasArray);
+ super.addAliasesProtected(aliasArray);
return this;
}
return "<none>";
}
- public boolean getValue() {
+ @Override
+ public Boolean getValue() {
return isParameterSpecified();
}
@Override
public NullParameter setMandatory() {
- mandatory = true;
+ setMandatoryProtected();
return this;
}
/*
* Svjatoslav Commons - shared library of common functionality.
* Copyright ©2012-2014, Svjatoslav Agejenko, svjatoslav@svjatoslav.eu
- *
+ *
* This program is free software; you can redistribute it and/or
* modify it under the terms of version 3 of the GNU Lesser General Public License
* or later as published by the Free Software Foundation.
@Override
public StringParameter addAliases(final String... aliasArray) {
- super.addAliases(aliasArray);
+ super.addAliasesProtected(aliasArray);
return this;
}
return "string";
}
+ @Override
public String getValue() {
if (arguments.size() != 1)
@Override
public StringParameter setMandatory() {
- mandatory = true;
+ setMandatoryProtected();
return this;
}
--- /dev/null
+/*
+ * Svjatoslav Commons - shared library of common functionality.
+ * Copyright ©2012-2014, Svjatoslav Agejenko, svjatoslav@svjatoslav.eu
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of version 3 of the GNU Lesser General Public License
+ * or later as published by the Free Software Foundation.
+ */
+
+package eu.svjatoslav.commons.commandline.parameterparser.parameter;
+
+import java.util.List;
+
+import eu.svjatoslav.commons.commandline.parameterparser.ArgumentCount;
+import eu.svjatoslav.commons.commandline.parameterparser.Parameter;
+
+public class StringParameters extends Parameter {
+
+ public StringParameters(final String description) {
+ super(description, ArgumentCount.MULTI);
+ }
+
+ @Override
+ public StringParameters addAliases(final String... aliasArray) {
+ super.addAliasesProtected(aliasArray);
+ return this;
+ }
+
+ @Override
+ public java.lang.String describeFormat() {
+ return "one to many strings";
+ }
+
+ @Override
+ public List<String> getValue() {
+ return arguments;
+ }
+
+ @Override
+ public StringParameters setMandatory() {
+ setMandatoryProtected();
+ return this;
+ }
+
+ @Override
+ public boolean validate(final java.lang.String value) {
+ return true;
+ }
+
+}
/*
* Svjatoslav Commons - shared library of common functionality.
* Copyright ©2012-2014, Svjatoslav Agejenko, svjatoslav@svjatoslav.eu
- *
+ *
* This program is free software; you can redistribute it and/or
* modify it under the terms of version 3 of the GNU Lesser General Public License
* or later as published by the Free Software Foundation.