From 5fecd6b03f114cab04e0389bd1900b27726c73ea Mon Sep 17 00:00:00 2001 From: Svjatoslav Agejenko Date: Sun, 20 Oct 2013 11:15:14 +0300 Subject: [PATCH] refactored commandline parser API for simpler usage --- .../parameterparser/ArgumentCount.java | 7 ++ .../parameterparser/Parameter.java | 72 ++++++++++--------- .../commandline/parameterparser/Parser.java | 8 +++ .../parameter/StringParameter.java | 27 +++++++ .../ExistingDirectory.java | 2 +- .../{arguments => type}/ExistingFile.java | 2 +- .../{arguments => type}/IntegerArgument.java | 2 +- .../NonExistingDirectory.java | 2 +- .../{arguments => type}/NonExistingFile.java | 2 +- .../{arguments => type}/StringArgument.java | 2 +- .../parameterparser/ParserTest.java | 25 +++---- 11 files changed, 95 insertions(+), 56 deletions(-) create mode 100644 src/main/java/eu/svjatoslav/commons/commandline/parameterparser/ArgumentCount.java create mode 100644 src/main/java/eu/svjatoslav/commons/commandline/parameterparser/parameter/StringParameter.java rename src/main/java/eu/svjatoslav/commons/commandline/parameterparser/{arguments => type}/ExistingDirectory.java (91%) rename src/main/java/eu/svjatoslav/commons/commandline/parameterparser/{arguments => type}/ExistingFile.java (91%) rename src/main/java/eu/svjatoslav/commons/commandline/parameterparser/{arguments => type}/IntegerArgument.java (91%) rename src/main/java/eu/svjatoslav/commons/commandline/parameterparser/{arguments => type}/NonExistingDirectory.java (91%) rename src/main/java/eu/svjatoslav/commons/commandline/parameterparser/{arguments => type}/NonExistingFile.java (91%) rename src/main/java/eu/svjatoslav/commons/commandline/parameterparser/{arguments => type}/StringArgument.java (90%) diff --git a/src/main/java/eu/svjatoslav/commons/commandline/parameterparser/ArgumentCount.java b/src/main/java/eu/svjatoslav/commons/commandline/parameterparser/ArgumentCount.java new file mode 100644 index 0000000..c87c829 --- /dev/null +++ b/src/main/java/eu/svjatoslav/commons/commandline/parameterparser/ArgumentCount.java @@ -0,0 +1,7 @@ +package eu.svjatoslav.commons.commandline.parameterparser; + +public enum ArgumentCount { + + NONE, SINGLE, MULTI + +} diff --git a/src/main/java/eu/svjatoslav/commons/commandline/parameterparser/Parameter.java b/src/main/java/eu/svjatoslav/commons/commandline/parameterparser/Parameter.java index 1e738a5..831806d 100755 --- a/src/main/java/eu/svjatoslav/commons/commandline/parameterparser/Parameter.java +++ b/src/main/java/eu/svjatoslav/commons/commandline/parameterparser/Parameter.java @@ -18,19 +18,17 @@ public class Parameter { /** * 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 aliases; + private final ArrayList aliases = new ArrayList(); + + public final String description; - private final String description; + protected final ArrayList arguments = new ArrayList(); - private final ArrayList arguments = new ArrayList(); + ArgumentCount argumentCount; /** * If this parameter was present in the commandline, then this boolean will @@ -38,28 +36,36 @@ public class Parameter { */ 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 aliasesList = new ArrayList(); - 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; } /** @@ -68,7 +74,7 @@ public class Parameter { */ 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()); @@ -76,7 +82,8 @@ public class Parameter { } // 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()); @@ -124,13 +131,6 @@ public class Parameter { 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 getArgumentsAsFiles() { final ArrayList result = new ArrayList(); @@ -161,10 +161,10 @@ public class Parameter { // 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"); @@ -206,7 +206,8 @@ public class Parameter { */ 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."); @@ -216,6 +217,11 @@ public class Parameter { return true; } + public Parameter setMandatory() { + mandatory = true; + return this; + } + /** * @param parameterSpecified * the parameterSpecified to set diff --git a/src/main/java/eu/svjatoslav/commons/commandline/parameterparser/Parser.java b/src/main/java/eu/svjatoslav/commons/commandline/parameterparser/Parser.java index a72984f..4ab529d 100755 --- a/src/main/java/eu/svjatoslav/commons/commandline/parameterparser/Parser.java +++ b/src/main/java/eu/svjatoslav/commons/commandline/parameterparser/Parser.java @@ -11,6 +11,8 @@ package eu.svjatoslav.commons.commandline.parameterparser; import java.util.ArrayList; +import eu.svjatoslav.commons.commandline.parameterparser.parameter.StringParameter; + /** *
  * Single commandline parameter can have any amount of arguments.
@@ -24,6 +26,12 @@ public class Parser {
 		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 null if no parameter
 	 * exists for given alias.
diff --git a/src/main/java/eu/svjatoslav/commons/commandline/parameterparser/parameter/StringParameter.java b/src/main/java/eu/svjatoslav/commons/commandline/parameterparser/parameter/StringParameter.java
new file mode 100644
index 0000000..e04b0f3
--- /dev/null
+++ b/src/main/java/eu/svjatoslav/commons/commandline/parameterparser/parameter/StringParameter.java
@@ -0,0 +1,27 @@
+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);
+	}
+}
diff --git a/src/main/java/eu/svjatoslav/commons/commandline/parameterparser/arguments/ExistingDirectory.java b/src/main/java/eu/svjatoslav/commons/commandline/parameterparser/type/ExistingDirectory.java
similarity index 91%
rename from src/main/java/eu/svjatoslav/commons/commandline/parameterparser/arguments/ExistingDirectory.java
rename to src/main/java/eu/svjatoslav/commons/commandline/parameterparser/type/ExistingDirectory.java
index 07341dd..2e66255 100755
--- a/src/main/java/eu/svjatoslav/commons/commandline/parameterparser/arguments/ExistingDirectory.java
+++ b/src/main/java/eu/svjatoslav/commons/commandline/parameterparser/type/ExistingDirectory.java
@@ -7,7 +7,7 @@
  * as published by the Free Software Foundation.
  */
 
-package eu.svjatoslav.commons.commandline.parameterparser.arguments;
+package eu.svjatoslav.commons.commandline.parameterparser.type;
 
 import java.io.File;
 
diff --git a/src/main/java/eu/svjatoslav/commons/commandline/parameterparser/arguments/ExistingFile.java b/src/main/java/eu/svjatoslav/commons/commandline/parameterparser/type/ExistingFile.java
similarity index 91%
rename from src/main/java/eu/svjatoslav/commons/commandline/parameterparser/arguments/ExistingFile.java
rename to src/main/java/eu/svjatoslav/commons/commandline/parameterparser/type/ExistingFile.java
index 676663f..d2ce6f4 100755
--- a/src/main/java/eu/svjatoslav/commons/commandline/parameterparser/arguments/ExistingFile.java
+++ b/src/main/java/eu/svjatoslav/commons/commandline/parameterparser/type/ExistingFile.java
@@ -7,7 +7,7 @@
  * as published by the Free Software Foundation.
  */
 
-package eu.svjatoslav.commons.commandline.parameterparser.arguments;
+package eu.svjatoslav.commons.commandline.parameterparser.type;
 
 import java.io.File;
 
diff --git a/src/main/java/eu/svjatoslav/commons/commandline/parameterparser/arguments/IntegerArgument.java b/src/main/java/eu/svjatoslav/commons/commandline/parameterparser/type/IntegerArgument.java
similarity index 91%
rename from src/main/java/eu/svjatoslav/commons/commandline/parameterparser/arguments/IntegerArgument.java
rename to src/main/java/eu/svjatoslav/commons/commandline/parameterparser/type/IntegerArgument.java
index 1d29046..2dfd300 100755
--- a/src/main/java/eu/svjatoslav/commons/commandline/parameterparser/arguments/IntegerArgument.java
+++ b/src/main/java/eu/svjatoslav/commons/commandline/parameterparser/type/IntegerArgument.java
@@ -7,7 +7,7 @@
  * as published by the Free Software Foundation.
  */
 
-package eu.svjatoslav.commons.commandline.parameterparser.arguments;
+package eu.svjatoslav.commons.commandline.parameterparser.type;
 
 import eu.svjatoslav.commons.commandline.parameterparser.Argument;
 
diff --git a/src/main/java/eu/svjatoslav/commons/commandline/parameterparser/arguments/NonExistingDirectory.java b/src/main/java/eu/svjatoslav/commons/commandline/parameterparser/type/NonExistingDirectory.java
similarity index 91%
rename from src/main/java/eu/svjatoslav/commons/commandline/parameterparser/arguments/NonExistingDirectory.java
rename to src/main/java/eu/svjatoslav/commons/commandline/parameterparser/type/NonExistingDirectory.java
index 005077b..d8a02bb 100755
--- a/src/main/java/eu/svjatoslav/commons/commandline/parameterparser/arguments/NonExistingDirectory.java
+++ b/src/main/java/eu/svjatoslav/commons/commandline/parameterparser/type/NonExistingDirectory.java
@@ -7,7 +7,7 @@
  * as published by the Free Software Foundation.
  */
 
-package eu.svjatoslav.commons.commandline.parameterparser.arguments;
+package eu.svjatoslav.commons.commandline.parameterparser.type;
 
 import java.io.File;
 
diff --git a/src/main/java/eu/svjatoslav/commons/commandline/parameterparser/arguments/NonExistingFile.java b/src/main/java/eu/svjatoslav/commons/commandline/parameterparser/type/NonExistingFile.java
similarity index 91%
rename from src/main/java/eu/svjatoslav/commons/commandline/parameterparser/arguments/NonExistingFile.java
rename to src/main/java/eu/svjatoslav/commons/commandline/parameterparser/type/NonExistingFile.java
index e4ddeb4..f3e441e 100755
--- a/src/main/java/eu/svjatoslav/commons/commandline/parameterparser/arguments/NonExistingFile.java
+++ b/src/main/java/eu/svjatoslav/commons/commandline/parameterparser/type/NonExistingFile.java
@@ -7,7 +7,7 @@
  * as published by the Free Software Foundation.
  */
 
-package eu.svjatoslav.commons.commandline.parameterparser.arguments;
+package eu.svjatoslav.commons.commandline.parameterparser.type;
 
 import java.io.File;
 
diff --git a/src/main/java/eu/svjatoslav/commons/commandline/parameterparser/arguments/StringArgument.java b/src/main/java/eu/svjatoslav/commons/commandline/parameterparser/type/StringArgument.java
similarity index 90%
rename from src/main/java/eu/svjatoslav/commons/commandline/parameterparser/arguments/StringArgument.java
rename to src/main/java/eu/svjatoslav/commons/commandline/parameterparser/type/StringArgument.java
index dd9f588..1ba894a 100755
--- a/src/main/java/eu/svjatoslav/commons/commandline/parameterparser/arguments/StringArgument.java
+++ b/src/main/java/eu/svjatoslav/commons/commandline/parameterparser/type/StringArgument.java
@@ -7,7 +7,7 @@
  * as published by the Free Software Foundation.
  */
 
-package eu.svjatoslav.commons.commandline.parameterparser.arguments;
+package eu.svjatoslav.commons.commandline.parameterparser.type;
 
 import eu.svjatoslav.commons.commandline.parameterparser.Argument;
 
diff --git a/src/test/java/eu/svjatoslav/commons/commandline/parameterparser/ParserTest.java b/src/test/java/eu/svjatoslav/commons/commandline/parameterparser/ParserTest.java
index 0e654b4..e8d1078 100755
--- a/src/test/java/eu/svjatoslav/commons/commandline/parameterparser/ParserTest.java
+++ b/src/test/java/eu/svjatoslav/commons/commandline/parameterparser/ParserTest.java
@@ -16,7 +16,7 @@ import static org.junit.Assert.assertTrue;
 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 {
 
@@ -31,23 +31,17 @@ 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());
@@ -55,11 +49,8 @@ public class ParserTest {
 		// 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));
 	}
-
 }
-- 
2.20.1