simplified commandline helper API
authorSvjatoslav Agejenko <svjatoslav@svjatoslav.eu>
Tue, 29 Oct 2013 16:36:19 +0000 (18:36 +0200)
committerSvjatoslav Agejenko <svjatoslav@svjatoslav.eu>
Tue, 29 Oct 2013 16:36:19 +0000 (18:36 +0200)
src/main/java/eu/svjatoslav/commons/commandline/parameterparser/Argument.java [deleted file]
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/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/type/IntegerArgument.java [deleted file]
src/main/java/eu/svjatoslav/commons/commandline/parameterparser/type/StringArgument.java [deleted file]

diff --git a/src/main/java/eu/svjatoslav/commons/commandline/parameterparser/Argument.java b/src/main/java/eu/svjatoslav/commons/commandline/parameterparser/Argument.java
deleted file mode 100755 (executable)
index 36d2c92..0000000
+++ /dev/null
@@ -1,25 +0,0 @@
-/*
- * 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;
-
-public interface Argument {
-
-       /**
-        * @return Single line argument type description.
-        */
-       public String describeFormat();
-
-       /**
-        * @return <code>true</code> if value is correct, <code>false</code>
-        *         otherwise.
-        */
-       public boolean validate(String value);
-
-}
index d9e78da..58282c5 100755 (executable)
@@ -13,15 +13,13 @@ import java.io.File;
 import java.util.ArrayList;
 import java.util.List;
 
-public class Parameter {
+public abstract class Parameter {
 
        /**
         * Indicates that at least one argument is mandatory for this parameter.
         */
        protected boolean mandatory;
 
-       private final Argument argumentType;
-
        private final ArrayList<String> aliases = new ArrayList<String>();
 
        public final String description;
@@ -36,20 +34,16 @@ public class Parameter {
         */
        private boolean parameterSpecified;
 
-       public Parameter(final Argument argumentType, final String description,
-                       final ArgumentCount argumentCount) {
-
+       public Parameter(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) {
+                       final ArgumentCount argumentCount, final String description,
+                       final String... aliases2) {
 
                this.mandatory = mandatory;
-               this.argumentType = argumentType;
                this.description = description;
                this.argumentCount = argumentCount;
 
@@ -90,10 +84,10 @@ public class Parameter {
                        return false;
                }
 
-               if (!argumentType.validate(argumentString)) {
+               if (!validate(argumentString)) {
 
                        System.out.println("Error! Invalid argument \"" + argumentString
-                                       + "\". It shall be " + argumentType.describeFormat() + ".");
+                                       + "\". It shall be " + describeFormat() + ".");
                        return false;
 
                }
@@ -148,7 +142,7 @@ public class Parameter {
                // first line
                buffer.append(getAliases());
                if (!argumentCount.equals(ArgumentCount.NONE)) {
-                       buffer.append(" (" + argumentType.describeFormat() + ")");
+                       buffer.append(" (" + describeFormat() + ")");
 
                        if (argumentCount.equals(ArgumentCount.MULTI))
                                buffer.append("...");
@@ -216,4 +210,15 @@ public class Parameter {
                this.parameterSpecified = parameterSpecified;
        }
 
+       /**
+        * @return Single line argument type description.
+        */
+       public abstract String describeFormat();
+
+       /**
+        * @return <code>true</code> if value is correct, <code>false</code>
+        *         otherwise.
+        */
+       public abstract boolean validate(String value);
+
 }
index f5ecf72..2646bfb 100755 (executable)
@@ -30,14 +30,6 @@ public class Parser {
                parameters.add(parameter);
        }
 
-       public Parameter createCustomParameter(final Argument paramaterType,
-                       final String description) {
-               final Parameter parameter = new Parameter(paramaterType, description,
-                               ArgumentCount.SINGLE);
-               parameters.add(parameter);
-               return parameter;
-       }
-
        public DirectoryParameter createDirectoryParameter(final String description) {
                final DirectoryParameter parameter = new DirectoryParameter(description);
                parameters.add(parameter);
index 8607234..2886dbf 100644 (file)
@@ -2,17 +2,15 @@ package eu.svjatoslav.commons.commandline.parameterparser.parameter;
 
 import java.io.File;
 
-import eu.svjatoslav.commons.commandline.parameterparser.Argument;
 import eu.svjatoslav.commons.commandline.parameterparser.ArgumentCount;
 import eu.svjatoslav.commons.commandline.parameterparser.Parameter;
-import eu.svjatoslav.commons.commandline.parameterparser.type.StringArgument;
 
-public class DirectoryParameter extends Parameter implements Argument {
+public class DirectoryParameter extends Parameter {
 
        private ExistanceType existanceType = ExistanceType.DOES_NOT_MATTER;
 
        public DirectoryParameter(final String description) {
-               super(new StringArgument(), description, ArgumentCount.SINGLE);
+               super(description, ArgumentCount.SINGLE);
        }
 
        @Override
index 7a48e2c..7980255 100644 (file)
@@ -2,17 +2,15 @@ package eu.svjatoslav.commons.commandline.parameterparser.parameter;
 
 import java.io.File;
 
-import eu.svjatoslav.commons.commandline.parameterparser.Argument;
 import eu.svjatoslav.commons.commandline.parameterparser.ArgumentCount;
 import eu.svjatoslav.commons.commandline.parameterparser.Parameter;
-import eu.svjatoslav.commons.commandline.parameterparser.type.StringArgument;
 
-public class FileParameter extends Parameter implements Argument {
+public class FileParameter extends Parameter {
 
        private ExistanceType existanceType = ExistanceType.DOES_NOT_MATTER;
 
        public FileParameter(final String description) {
-               super(new StringArgument(), description, ArgumentCount.SINGLE);
+               super(description, ArgumentCount.SINGLE);
        }
 
        @Override
index 7af305b..25755e4 100644 (file)
@@ -2,12 +2,11 @@ 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.IntegerArgument;
 
 public class IntegerParameter extends Parameter {
 
        public IntegerParameter(final String description) {
-               super(new IntegerArgument(), description, ArgumentCount.SINGLE);
+               super(description, ArgumentCount.SINGLE);
        }
 
        @Override
@@ -28,4 +27,20 @@ public class IntegerParameter extends Parameter {
                mandatory = true;
                return this;
        }
+
+       @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;
+               }
+       }
+
 }
index 81ed2c0..acac388 100644 (file)
@@ -6,7 +6,7 @@ import eu.svjatoslav.commons.commandline.parameterparser.Parameter;
 public class NullParameter extends Parameter {
 
        public NullParameter(final String description) {
-               super(null, description, ArgumentCount.NONE);
+               super(description, ArgumentCount.NONE);
        }
 
        @Override
@@ -24,4 +24,15 @@ public class NullParameter extends Parameter {
                mandatory = true;
                return this;
        }
+
+       @Override
+       public java.lang.String describeFormat() {
+               return "<none>";
+       }
+
+       @Override
+       public boolean validate(final java.lang.String value) {
+               return true;
+       }
+
 }
index 29967bc..dc70c26 100644 (file)
@@ -2,12 +2,11 @@ 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);
+               super(description, ArgumentCount.SINGLE);
        }
 
        @Override
@@ -31,4 +30,14 @@ public class StringParameter extends Parameter {
                return this;
        }
 
+       @Override
+       public java.lang.String describeFormat() {
+               return "string";
+       }
+
+       @Override
+       public boolean validate(final java.lang.String value) {
+               return true;
+       }
+
 }
diff --git a/src/main/java/eu/svjatoslav/commons/commandline/parameterparser/type/IntegerArgument.java b/src/main/java/eu/svjatoslav/commons/commandline/parameterparser/type/IntegerArgument.java
deleted file mode 100755 (executable)
index 2dfd300..0000000
+++ /dev/null
@@ -1,31 +0,0 @@
-/*
- * 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;
-               }
-       }
-
-}
diff --git a/src/main/java/eu/svjatoslav/commons/commandline/parameterparser/type/StringArgument.java b/src/main/java/eu/svjatoslav/commons/commandline/parameterparser/type/StringArgument.java
deleted file mode 100755 (executable)
index 1ba894a..0000000
+++ /dev/null
@@ -1,26 +0,0 @@
-/*
- * 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;
-       }
-
-}