From: Svjatoslav Agejenko Date: Sat, 23 Aug 2014 21:04:27 +0000 (+0300) Subject: Added special commandline argument type to support multiple strings. X-Git-Tag: svjatoslavcommons-1.8~96 X-Git-Url: http://www2.svjatoslav.eu/gitweb/?a=commitdiff_plain;h=ab4cc64cf105d4f03b8a0b94ab58d9b973820c8a;p=svjatoslav_commons.git Added special commandline argument type to support multiple strings. More foolproof code. --- 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 2e1c03a..004a48a 100755 --- a/src/main/java/eu/svjatoslav/commons/commandline/parameterparser/Parameter.java +++ b/src/main/java/eu/svjatoslav/commons/commandline/parameterparser/Parameter.java @@ -1,7 +1,7 @@ /* * 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. @@ -9,9 +9,7 @@ package eu.svjatoslav.commons.commandline.parameterparser; -import java.io.File; import java.util.ArrayList; -import java.util.List; public abstract class Parameter { @@ -26,7 +24,7 @@ public abstract class Parameter { protected final ArrayList arguments = new ArrayList(); - ArgumentCount argumentCount; + final ArgumentCount argumentCount; /** * If this parameter was present in the commandline, then this boolean will @@ -53,7 +51,9 @@ public abstract class Parameter { 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) @@ -116,30 +116,30 @@ public abstract class Parameter { return buffer.toString(); } - public List getArgumentsAsFiles() { - final ArrayList result = new ArrayList(); - - for (final String argument : arguments) { - final File file = new File(argument); - result.add(file); - } - - return result; - } - - public List getArgumentsAsIntegers() { - final ArrayList result = new ArrayList(); - - for (final String argument : arguments) - result.add(Integer.valueOf(argument)); - - return result; - } - - public List getArgumentsAsStrings() { - final ArrayList result = new ArrayList(arguments); - return result; - } + // public List getArgumentsAsFiles() { + // final ArrayList result = new ArrayList(); + // + // for (final String argument : arguments) { + // final File file = new File(argument); + // result.add(file); + // } + // + // return result; + // } + // + // public List getArgumentsAsIntegers() { + // final ArrayList result = new ArrayList(); + // + // for (final String argument : arguments) + // result.add(Integer.valueOf(argument)); + // + // return result; + // } + // + // public List getArgumentsAsStrings() { + // final ArrayList result = new ArrayList(arguments); + // return result; + // } public String getHelp() { final StringBuffer buffer = new StringBuffer(); @@ -160,6 +160,8 @@ public abstract class Parameter { return buffer.toString(); } + public abstract Object getValue(); + public boolean isMandatory() { return mandatory; } @@ -185,7 +187,7 @@ public abstract class Parameter { /** * Notifies this parameter that no more arguments will follow. This gives * parameter chance to verify if this is ok. - * + * * @return true if no errors were found. false * otherwise. */ @@ -202,7 +204,9 @@ public abstract class Parameter { return true; } - public Parameter setMandatory() { + public abstract Parameter setMandatory(); + + protected Parameter setMandatoryProtected() { mandatory = true; return this; } 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 47b13d7..bd4cb17 100755 --- a/src/main/java/eu/svjatoslav/commons/commandline/parameterparser/Parser.java +++ b/src/main/java/eu/svjatoslav/commons/commandline/parameterparser/Parser.java @@ -1,7 +1,7 @@ /* * 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. @@ -16,6 +16,7 @@ import eu.svjatoslav.commons.commandline.parameterparser.parameter.FileParameter 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; /** *
@@ -60,6 +61,13 @@ public class Parser {
 		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 null if no parameter
 	 * exists for given alias.
diff --git a/src/main/java/eu/svjatoslav/commons/commandline/parameterparser/parameter/DirectoryParameter.java b/src/main/java/eu/svjatoslav/commons/commandline/parameterparser/parameter/DirectoryParameter.java
index 35ab439..fc03263 100755
--- a/src/main/java/eu/svjatoslav/commons/commandline/parameterparser/parameter/DirectoryParameter.java
+++ b/src/main/java/eu/svjatoslav/commons/commandline/parameterparser/parameter/DirectoryParameter.java
@@ -1,7 +1,7 @@
 /*
  * 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.
@@ -24,7 +24,7 @@ public class DirectoryParameter extends Parameter {
 
 	@Override
 	public DirectoryParameter addAliases(final String... aliasArray) {
-		super.addAliases(aliasArray);
+		super.addAliasesProtected(aliasArray);
 		return this;
 	}
 
@@ -33,6 +33,7 @@ public class DirectoryParameter extends Parameter {
 		return existanceType.description + "directory";
 	}
 
+	@Override
 	public File getValue() {
 
 		if (arguments.size() != 1)
@@ -54,7 +55,7 @@ public class DirectoryParameter extends Parameter {
 
 	@Override
 	public DirectoryParameter setMandatory() {
-		mandatory = true;
+		setMandatoryProtected();
 		return this;
 	}
 
diff --git a/src/main/java/eu/svjatoslav/commons/commandline/parameterparser/parameter/ExistanceType.java b/src/main/java/eu/svjatoslav/commons/commandline/parameterparser/parameter/ExistanceType.java
index 3ff1a03..2b30d46 100755
--- a/src/main/java/eu/svjatoslav/commons/commandline/parameterparser/parameter/ExistanceType.java
+++ b/src/main/java/eu/svjatoslav/commons/commandline/parameterparser/parameter/ExistanceType.java
@@ -1,7 +1,7 @@
 /*
  * 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.
@@ -11,8 +11,7 @@ package eu.svjatoslav.commons.commandline.parameterparser.parameter;
 
 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;
 
diff --git a/src/main/java/eu/svjatoslav/commons/commandline/parameterparser/parameter/FileParameter.java b/src/main/java/eu/svjatoslav/commons/commandline/parameterparser/parameter/FileParameter.java
index 5fa357c..98a9b42 100755
--- a/src/main/java/eu/svjatoslav/commons/commandline/parameterparser/parameter/FileParameter.java
+++ b/src/main/java/eu/svjatoslav/commons/commandline/parameterparser/parameter/FileParameter.java
@@ -1,7 +1,7 @@
 /*
  * 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.
@@ -24,15 +24,16 @@ public class FileParameter extends Parameter {
 
 	@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)
@@ -54,7 +55,7 @@ public class FileParameter extends Parameter {
 
 	@Override
 	public FileParameter setMandatory() {
-		mandatory = true;
+		setMandatoryProtected();
 		return this;
 	}
 
diff --git a/src/main/java/eu/svjatoslav/commons/commandline/parameterparser/parameter/IntegerParameter.java b/src/main/java/eu/svjatoslav/commons/commandline/parameterparser/parameter/IntegerParameter.java
index 6972149..7c75151 100755
--- a/src/main/java/eu/svjatoslav/commons/commandline/parameterparser/parameter/IntegerParameter.java
+++ b/src/main/java/eu/svjatoslav/commons/commandline/parameterparser/parameter/IntegerParameter.java
@@ -1,7 +1,7 @@
 /*
  * 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.
@@ -20,7 +20,7 @@ public class IntegerParameter extends Parameter {
 
 	@Override
 	public IntegerParameter addAliases(final String... aliasArray) {
-		super.addAliases(aliasArray);
+		super.addAliasesProtected(aliasArray);
 		return this;
 	}
 
@@ -29,7 +29,8 @@ public class IntegerParameter extends Parameter {
 		return "integer";
 	}
 
-	public int getValue() {
+	@Override
+	public Integer getValue() {
 		if (arguments.size() != 1)
 			throw new RuntimeException("Parameter " + description
 					+ " shall have exactly 1 argument.");
@@ -38,7 +39,7 @@ public class IntegerParameter extends Parameter {
 
 	@Override
 	public IntegerParameter setMandatory() {
-		mandatory = true;
+		setMandatoryProtected();
 		return this;
 	}
 
@@ -51,5 +52,4 @@ public class IntegerParameter extends Parameter {
 			return false;
 		}
 	}
-
 }
diff --git a/src/main/java/eu/svjatoslav/commons/commandline/parameterparser/parameter/NullParameter.java b/src/main/java/eu/svjatoslav/commons/commandline/parameterparser/parameter/NullParameter.java
index 7e55d57..276ecdb 100755
--- a/src/main/java/eu/svjatoslav/commons/commandline/parameterparser/parameter/NullParameter.java
+++ b/src/main/java/eu/svjatoslav/commons/commandline/parameterparser/parameter/NullParameter.java
@@ -1,7 +1,7 @@
 /*
  * 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.
@@ -20,7 +20,7 @@ public class NullParameter extends Parameter {
 
 	@Override
 	public NullParameter addAliases(final String... aliasArray) {
-		super.addAliases(aliasArray);
+		super.addAliasesProtected(aliasArray);
 		return this;
 	}
 
@@ -29,13 +29,14 @@ public class NullParameter extends Parameter {
 		return "";
 	}
 
-	public boolean getValue() {
+	@Override
+	public Boolean getValue() {
 		return isParameterSpecified();
 	}
 
 	@Override
 	public NullParameter setMandatory() {
-		mandatory = true;
+		setMandatoryProtected();
 		return this;
 	}
 
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
index e108417..533763d 100755
--- a/src/main/java/eu/svjatoslav/commons/commandline/parameterparser/parameter/StringParameter.java
+++ b/src/main/java/eu/svjatoslav/commons/commandline/parameterparser/parameter/StringParameter.java
@@ -1,7 +1,7 @@
 /*
  * 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.
@@ -20,7 +20,7 @@ public class StringParameter extends Parameter {
 
 	@Override
 	public StringParameter addAliases(final String... aliasArray) {
-		super.addAliases(aliasArray);
+		super.addAliasesProtected(aliasArray);
 		return this;
 	}
 
@@ -29,6 +29,7 @@ public class StringParameter extends Parameter {
 		return "string";
 	}
 
+	@Override
 	public String getValue() {
 
 		if (arguments.size() != 1)
@@ -40,7 +41,7 @@ public class StringParameter extends Parameter {
 
 	@Override
 	public StringParameter setMandatory() {
-		mandatory = true;
+		setMandatoryProtected();
 		return this;
 	}
 
diff --git a/src/main/java/eu/svjatoslav/commons/commandline/parameterparser/parameter/StringParameters.java b/src/main/java/eu/svjatoslav/commons/commandline/parameterparser/parameter/StringParameters.java
new file mode 100644
index 0000000..3de1b03
--- /dev/null
+++ b/src/main/java/eu/svjatoslav/commons/commandline/parameterparser/parameter/StringParameters.java
@@ -0,0 +1,50 @@
+/*
+ * 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 getValue() {
+		return arguments;
+	}
+
+	@Override
+	public StringParameters setMandatory() {
+		setMandatoryProtected();
+		return this;
+	}
+
+	@Override
+	public boolean validate(final java.lang.String value) {
+		return true;
+	}
+
+}
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 e84a012..7917a77 100755
--- a/src/test/java/eu/svjatoslav/commons/commandline/parameterparser/ParserTest.java
+++ b/src/test/java/eu/svjatoslav/commons/commandline/parameterparser/ParserTest.java
@@ -1,7 +1,7 @@
 /*
  * 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.