--- /dev/null
+package eu.svjatoslav.commons.commandline.parameterparser;
+
+public enum ArgumentCount {
+
+ NONE, SINGLE, MULTI
+
+}
/**
* Indicates that at least one argument is mandatory for this parameter.
*/
- private final boolean mandatory;
-
- private final boolean enableArguments;
-
- private final boolean enableMultipleArguments;
+ private boolean mandatory;
private final Argument argumentType;
- private final ArrayList<String> aliases;
+ private final ArrayList<String> aliases = new ArrayList<String>();
+
+ public final String description;
- private final String description;
+ protected final ArrayList<String> arguments = new ArrayList<String>();
- private final ArrayList<String> arguments = new ArrayList<String>();
+ ArgumentCount argumentCount;
/**
* If this parameter was present in the commandline, then this boolean will
*/
private boolean parameterSpecified;
- public Parameter(final boolean mandatory, final boolean enableArguments,
- final boolean enableMultipleArguments, final Argument argumentType,
- final String description, final String... aliases) {
+ public Parameter(final Argument argumentType, 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) {
this.mandatory = mandatory;
- this.enableArguments = enableArguments;
- this.enableMultipleArguments = enableMultipleArguments;
this.argumentType = argumentType;
this.description = description;
+ this.argumentCount = argumentCount;
// save aliases
- {
- final ArrayList<String> aliasesList = new ArrayList<String>();
- for (final String alias : aliases)
- aliasesList.add(alias);
- this.aliases = aliasesList;
- }
+ for (final String alias : aliases2)
+ aliases.add(alias);
}
- public Parameter(final String description, final String... aliases) {
- this(false, false, false, null, description, aliases);
+ public Parameter addAliases(final String... aliasArray) {
+
+ // save aliases
+ for (final String alias : aliasArray)
+ aliases.add(alias);
+
+ return this;
}
/**
*/
public boolean addArgument(final String argumentString) {
// check if arguments are allowed for this parameter
- if (!enableArguments) {
+ if (argumentCount.equals(ArgumentCount.NONE)) {
System.out
.println("Error! No arguments are allowed for parameters: "
+ getAliases());
}
// check if multiple arguments are allowed
- if ((arguments.size() > 0) && (!enableMultipleArguments)) {
+ if ((arguments.size() > 0)
+ && (argumentCount.equals(ArgumentCount.SINGLE))) {
System.out
.println("Error! Only single argument is allowed for parameters: "
+ getAliases());
return Integer.parseInt(arguments.get(0));
}
- public String getArgumentAsString() {
- if (arguments.size() != 1)
- throw new RuntimeException("Parameter " + description
- + " shall have exactly 1 argument.");
- return arguments.get(0);
- }
-
public List<File> getArgumentsAsFiles() {
final ArrayList<File> result = new ArrayList<File>();
// first line
buffer.append(getAliases());
- if (enableArguments) {
+ if (!argumentCount.equals(ArgumentCount.NONE)) {
buffer.append(" (" + argumentType.describeFormat() + ")");
- if (enableMultipleArguments)
+ if (argumentCount.equals(ArgumentCount.MULTI))
buffer.append("...");
}
buffer.append("\n");
*/
public boolean noMoreArguments() {
- if (enableArguments && (arguments.isEmpty())) {
+ if ((!argumentCount.equals(ArgumentCount.NONE))
+ && (arguments.isEmpty())) {
System.out.println("Error! " + getAliases()
+ " require at least one following argument.");
return true;
}
+ public Parameter setMandatory() {
+ mandatory = true;
+ return this;
+ }
+
/**
* @param parameterSpecified
* the parameterSpecified to set
import java.util.ArrayList;
+import eu.svjatoslav.commons.commandline.parameterparser.parameter.StringParameter;
+
/**
* <pre>
* Single commandline parameter can have any amount of arguments.
parameters.add(parameter);
}
+ public StringParameter createStringParameter(final String description) {
+ final StringParameter parameter = new StringParameter(description);
+ parameters.add(parameter);
+ return parameter;
+ }
+
/**
* Return parameter by given alias or <code>null</code> if no parameter
* exists for given alias.
+++ /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.arguments;
-
-import java.io.File;
-
-import eu.svjatoslav.commons.commandline.parameterparser.Argument;
-
-public class ExistingDirectory implements Argument {
-
- @Override
- public java.lang.String describeFormat() {
- return "existing directory";
- }
-
- @Override
- public boolean validate(final java.lang.String value) {
- final File file2 = new File(value);
- if (file2.exists() && file2.isDirectory())
- return true;
- 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.arguments;
-
-import java.io.File;
-
-import eu.svjatoslav.commons.commandline.parameterparser.Argument;
-
-public class ExistingFile implements Argument {
-
- @Override
- public java.lang.String describeFormat() {
- return "existing file";
- }
-
- @Override
- public boolean validate(final java.lang.String value) {
- final File file = new File(value);
- if (file.exists() && file.isFile())
- return true;
- 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.arguments;
-
-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.arguments;
-
-import java.io.File;
-
-import eu.svjatoslav.commons.commandline.parameterparser.Argument;
-
-public class NonExistingDirectory implements Argument {
-
- @Override
- public java.lang.String describeFormat() {
- return "non existing directory";
- }
-
- @Override
- public boolean validate(final java.lang.String value) {
- final File file4 = new File(value);
- if (!file4.exists())
- return true;
- 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.arguments;
-
-import java.io.File;
-
-import eu.svjatoslav.commons.commandline.parameterparser.Argument;
-
-public class NonExistingFile implements Argument {
-
- @Override
- public java.lang.String describeFormat() {
- return "non existing file";
- }
-
- @Override
- public boolean validate(final java.lang.String value) {
- final File file3 = new File(value);
- if (!file3.exists())
- return true;
- 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.arguments;
-
-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;
- }
-
-}
--- /dev/null
+package eu.svjatoslav.commons.commandline.parameterparser.parameter;
+
+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);
+ }
+
+ @Override
+ public StringParameter addAliases(final String... aliasArray) {
+ super.addAliases(aliasArray);
+ return this;
+ }
+
+ public String getValue() {
+
+ if (arguments.size() != 1)
+ throw new RuntimeException("Parameter " + description
+ + " shall have exactly 1 argument.");
+
+ return arguments.get(0);
+ }
+}
--- /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 java.io.File;
+
+import eu.svjatoslav.commons.commandline.parameterparser.Argument;
+
+public class ExistingDirectory implements Argument {
+
+ @Override
+ public java.lang.String describeFormat() {
+ return "existing directory";
+ }
+
+ @Override
+ public boolean validate(final java.lang.String value) {
+ final File file2 = new File(value);
+ if (file2.exists() && file2.isDirectory())
+ return true;
+ 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 java.io.File;
+
+import eu.svjatoslav.commons.commandline.parameterparser.Argument;
+
+public class ExistingFile implements Argument {
+
+ @Override
+ public java.lang.String describeFormat() {
+ return "existing file";
+ }
+
+ @Override
+ public boolean validate(final java.lang.String value) {
+ final File file = new File(value);
+ if (file.exists() && file.isFile())
+ return true;
+ 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 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 java.io.File;
+
+import eu.svjatoslav.commons.commandline.parameterparser.Argument;
+
+public class NonExistingDirectory implements Argument {
+
+ @Override
+ public java.lang.String describeFormat() {
+ return "non existing directory";
+ }
+
+ @Override
+ public boolean validate(final java.lang.String value) {
+ final File file4 = new File(value);
+ if (!file4.exists())
+ return true;
+ 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 java.io.File;
+
+import eu.svjatoslav.commons.commandline.parameterparser.Argument;
+
+public class NonExistingFile implements Argument {
+
+ @Override
+ public java.lang.String describeFormat() {
+ return "non existing file";
+ }
+
+ @Override
+ public boolean validate(final java.lang.String value) {
+ final File file3 = new File(value);
+ if (!file3.exists())
+ return true;
+ 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;
+ }
+
+}
import org.junit.Before;
import org.junit.Test;
-import eu.svjatoslav.commons.commandline.parameterparser.arguments.IntegerArgument;
+import eu.svjatoslav.commons.commandline.parameterparser.parameter.StringParameter;
public class ParserTest {
public void testParse() {
// define allowed parameters
- final Parameter helpParameter = new Parameter("Show help screen", "-h",
- "--help");
- parser.addParameter(helpParameter);
+ final StringParameter helpParameter = parser.createStringParameter(
+ "Show help screen").addAliases("--help", "-h");
- final Parameter compileParameter = new Parameter("Compile code", "-c",
- "--compile");
- parser.addParameter(compileParameter);
-
- final Parameter bitrateParameter = new Parameter(false, true, false,
- new IntegerArgument(), "Target bitrate", "-b", "--bitrate");
- parser.addParameter(bitrateParameter);
+ final StringParameter compileParameter = parser.createStringParameter(
+ "Compile code").addAliases("--compile", "-c");
// check help generation
parser.showHelp();
// parse arguments
- parser.parse(new String[] { "--help", "-b", "123" });
+ parser.parse(new String[] { "--help", "section" });
// --help was in the arguments
assertTrue(helpParameter.isParameterSpecified());
// compile was not present
assertFalse(compileParameter.isParameterSpecified());
- // bitrate is given as 123
- assertTrue(bitrateParameter.isParameterSpecified());
+ // validate that help argument was "section"
+ assertEquals("section", helpParameter.getValue());
- assertEquals(123, (int) bitrateParameter.getArgumentsAsIntegers()
- .get(0));
}
-
}