Added special commandline argument type to support multiple strings.
authorSvjatoslav Agejenko <svjatoslav@svjatoslav.eu>
Sat, 23 Aug 2014 21:04:27 +0000 (00:04 +0300)
committerSvjatoslav Agejenko <svjatoslav@svjatoslav.eu>
Sat, 23 Aug 2014 21:04:27 +0000 (00:04 +0300)
More foolproof code.

src/main/java/eu/svjatoslav/commons/commandline/parameterparser/Parameter.java
src/main/java/eu/svjatoslav/commons/commandline/parameterparser/Parser.java
src/main/java/eu/svjatoslav/commons/commandline/parameterparser/parameter/DirectoryParameter.java
src/main/java/eu/svjatoslav/commons/commandline/parameterparser/parameter/ExistanceType.java
src/main/java/eu/svjatoslav/commons/commandline/parameterparser/parameter/FileParameter.java
src/main/java/eu/svjatoslav/commons/commandline/parameterparser/parameter/IntegerParameter.java
src/main/java/eu/svjatoslav/commons/commandline/parameterparser/parameter/NullParameter.java
src/main/java/eu/svjatoslav/commons/commandline/parameterparser/parameter/StringParameter.java
src/main/java/eu/svjatoslav/commons/commandline/parameterparser/parameter/StringParameters.java [new file with mode: 0644]
src/test/java/eu/svjatoslav/commons/commandline/parameterparser/ParserTest.java

index 2e1c03a..004a48a 100755 (executable)
@@ -1,7 +1,7 @@
 /*
  * Svjatoslav Commons - shared library of common functionality.
  * Copyright ©2012-2014, Svjatoslav Agejenko, svjatoslav@svjatoslav.eu
 /*
  * 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.
  * 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;
 
 
 package eu.svjatoslav.commons.commandline.parameterparser;
 
-import java.io.File;
 import java.util.ArrayList;
 import java.util.ArrayList;
-import java.util.List;
 
 public abstract class Parameter {
 
 
 public abstract class Parameter {
 
@@ -26,7 +24,7 @@ public abstract class Parameter {
 
        protected final ArrayList<String> arguments = new ArrayList<String>();
 
 
        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
 
        /**
         * If this parameter was present in the commandline, then this boolean will
@@ -53,7 +51,9 @@ public abstract class Parameter {
                this.argumentCount = argumentCount;
        }
 
                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)
 
                // save aliases
                for (final String alias : aliasArray)
@@ -116,30 +116,30 @@ public abstract class Parameter {
                return buffer.toString();
        }
 
                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();
 
        public String getHelp() {
                final StringBuffer buffer = new StringBuffer();
@@ -160,6 +160,8 @@ public abstract class Parameter {
                return buffer.toString();
        }
 
                return buffer.toString();
        }
 
+       public abstract Object getValue();
+
        public boolean isMandatory() {
                return mandatory;
        }
        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.
        /**
         * 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 <code>true</code> if no errors were found. <code>false</code>
         *         otherwise.
         */
@@ -202,7 +204,9 @@ public abstract class Parameter {
                return true;
        }
 
                return true;
        }
 
-       public Parameter setMandatory() {
+       public abstract Parameter setMandatory();
+
+       protected Parameter setMandatoryProtected() {
                mandatory = true;
                return this;
        }
                mandatory = true;
                return this;
        }
index 47b13d7..bd4cb17 100755 (executable)
@@ -1,7 +1,7 @@
 /*
  * Svjatoslav Commons - shared library of common functionality.
  * Copyright ©2012-2014, Svjatoslav Agejenko, svjatoslav@svjatoslav.eu
 /*
  * 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.
  * 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.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>
 
 /**
  * <pre>
@@ -60,6 +61,13 @@ public class Parser {
                return parameter;
        }
 
                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.
        /**
         * Return parameter by given alias or <code>null</code> if no parameter
         * exists for given alias.
index 35ab439..fc03263 100755 (executable)
@@ -1,7 +1,7 @@
 /*
  * Svjatoslav Commons - shared library of common functionality.
  * Copyright ©2012-2014, Svjatoslav Agejenko, svjatoslav@svjatoslav.eu
 /*
  * 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.
  * 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) {
 
        @Override
        public DirectoryParameter addAliases(final String... aliasArray) {
-               super.addAliases(aliasArray);
+               super.addAliasesProtected(aliasArray);
                return this;
        }
 
                return this;
        }
 
@@ -33,6 +33,7 @@ public class DirectoryParameter extends Parameter {
                return existanceType.description + "directory";
        }
 
                return existanceType.description + "directory";
        }
 
+       @Override
        public File getValue() {
 
                if (arguments.size() != 1)
        public File getValue() {
 
                if (arguments.size() != 1)
@@ -54,7 +55,7 @@ public class DirectoryParameter extends Parameter {
 
        @Override
        public DirectoryParameter setMandatory() {
 
        @Override
        public DirectoryParameter setMandatory() {
-               mandatory = true;
+               setMandatoryProtected();
                return this;
        }
 
                return this;
        }
 
index 3ff1a03..2b30d46 100755 (executable)
@@ -1,7 +1,7 @@
 /*
  * Svjatoslav Commons - shared library of common functionality.
  * Copyright ©2012-2014, Svjatoslav Agejenko, svjatoslav@svjatoslav.eu
 /*
  * 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.
  * 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 {
 
 
 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;
 
 
        public final String description;
 
index 5fa357c..98a9b42 100755 (executable)
@@ -1,7 +1,7 @@
 /*
  * Svjatoslav Commons - shared library of common functionality.
  * Copyright ©2012-2014, Svjatoslav Agejenko, svjatoslav@svjatoslav.eu
 /*
  * 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.
  * 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) {
 
        @Override
        public FileParameter addAliases(final String... aliasArray) {
-               super.addAliases(aliasArray);
+               super.addAliasesProtected(aliasArray);
                return this;
        }
 
        @Override
        public java.lang.String describeFormat() {
                return this;
        }
 
        @Override
        public java.lang.String describeFormat() {
-               return existanceType.description + "file";
+               return existanceType.description + " file";
        }
 
        }
 
+       @Override
        public File getValue() {
 
                if (arguments.size() != 1)
        public File getValue() {
 
                if (arguments.size() != 1)
@@ -54,7 +55,7 @@ public class FileParameter extends Parameter {
 
        @Override
        public FileParameter setMandatory() {
 
        @Override
        public FileParameter setMandatory() {
-               mandatory = true;
+               setMandatoryProtected();
                return this;
        }
 
                return this;
        }
 
index 6972149..7c75151 100755 (executable)
@@ -1,7 +1,7 @@
 /*
  * Svjatoslav Commons - shared library of common functionality.
  * Copyright ©2012-2014, Svjatoslav Agejenko, svjatoslav@svjatoslav.eu
 /*
  * 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.
  * 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) {
 
        @Override
        public IntegerParameter addAliases(final String... aliasArray) {
-               super.addAliases(aliasArray);
+               super.addAliasesProtected(aliasArray);
                return this;
        }
 
                return this;
        }
 
@@ -29,7 +29,8 @@ public class IntegerParameter extends Parameter {
                return "integer";
        }
 
                return "integer";
        }
 
-       public int getValue() {
+       @Override
+       public Integer getValue() {
                if (arguments.size() != 1)
                        throw new RuntimeException("Parameter " + description
                                        + " shall have exactly 1 argument.");
                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() {
 
        @Override
        public IntegerParameter setMandatory() {
-               mandatory = true;
+               setMandatoryProtected();
                return this;
        }
 
                return this;
        }
 
@@ -51,5 +52,4 @@ public class IntegerParameter extends Parameter {
                        return false;
                }
        }
                        return false;
                }
        }
-
 }
 }
index 7e55d57..276ecdb 100755 (executable)
@@ -1,7 +1,7 @@
 /*
  * Svjatoslav Commons - shared library of common functionality.
  * Copyright ©2012-2014, Svjatoslav Agejenko, svjatoslav@svjatoslav.eu
 /*
  * 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.
  * 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) {
 
        @Override
        public NullParameter addAliases(final String... aliasArray) {
-               super.addAliases(aliasArray);
+               super.addAliasesProtected(aliasArray);
                return this;
        }
 
                return this;
        }
 
@@ -29,13 +29,14 @@ public class NullParameter extends Parameter {
                return "<none>";
        }
 
                return "<none>";
        }
 
-       public boolean getValue() {
+       @Override
+       public Boolean getValue() {
                return isParameterSpecified();
        }
 
        @Override
        public NullParameter setMandatory() {
                return isParameterSpecified();
        }
 
        @Override
        public NullParameter setMandatory() {
-               mandatory = true;
+               setMandatoryProtected();
                return this;
        }
 
                return this;
        }
 
index e108417..533763d 100755 (executable)
@@ -1,7 +1,7 @@
 /*
  * Svjatoslav Commons - shared library of common functionality.
  * Copyright ©2012-2014, Svjatoslav Agejenko, svjatoslav@svjatoslav.eu
 /*
  * 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.
  * 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) {
 
        @Override
        public StringParameter addAliases(final String... aliasArray) {
-               super.addAliases(aliasArray);
+               super.addAliasesProtected(aliasArray);
                return this;
        }
 
                return this;
        }
 
@@ -29,6 +29,7 @@ public class StringParameter extends Parameter {
                return "string";
        }
 
                return "string";
        }
 
+       @Override
        public String getValue() {
 
                if (arguments.size() != 1)
        public String getValue() {
 
                if (arguments.size() != 1)
@@ -40,7 +41,7 @@ public class StringParameter extends Parameter {
 
        @Override
        public StringParameter setMandatory() {
 
        @Override
        public StringParameter setMandatory() {
-               mandatory = true;
+               setMandatoryProtected();
                return this;
        }
 
                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 (file)
index 0000000..3de1b03
--- /dev/null
@@ -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<String> getValue() {
+               return arguments;
+       }
+
+       @Override
+       public StringParameters setMandatory() {
+               setMandatoryProtected();
+               return this;
+       }
+
+       @Override
+       public boolean validate(final java.lang.String value) {
+               return true;
+       }
+
+}
index e84a012..7917a77 100755 (executable)
@@ -1,7 +1,7 @@
 /*
  * Svjatoslav Commons - shared library of common functionality.
  * Copyright ©2012-2014, Svjatoslav Agejenko, svjatoslav@svjatoslav.eu
 /*
  * 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.
  * 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.