From: Svjatoslav Agejenko Date: Tue, 29 Oct 2013 16:36:19 +0000 (+0200) Subject: simplified commandline helper API X-Git-Tag: svjatoslavcommons-1.8~127 X-Git-Url: http://www2.svjatoslav.eu/gitweb/?a=commitdiff_plain;h=9e18e609a44f9392aee651cfaf193b4141aef6b4;p=svjatoslav_commons.git simplified commandline helper API --- 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 index 36d2c92..0000000 --- a/src/main/java/eu/svjatoslav/commons/commandline/parameterparser/Argument.java +++ /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 true if value is correct, false - * otherwise. - */ - public boolean validate(String value); - -} 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 d9e78da..58282c5 100755 --- a/src/main/java/eu/svjatoslav/commons/commandline/parameterparser/Parameter.java +++ b/src/main/java/eu/svjatoslav/commons/commandline/parameterparser/Parameter.java @@ -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 aliases = new ArrayList(); 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 true if value is correct, false + * otherwise. + */ + public abstract boolean validate(String value); + } 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 f5ecf72..2646bfb 100755 --- a/src/main/java/eu/svjatoslav/commons/commandline/parameterparser/Parser.java +++ b/src/main/java/eu/svjatoslav/commons/commandline/parameterparser/Parser.java @@ -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); 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 8607234..2886dbf 100644 --- a/src/main/java/eu/svjatoslav/commons/commandline/parameterparser/parameter/DirectoryParameter.java +++ b/src/main/java/eu/svjatoslav/commons/commandline/parameterparser/parameter/DirectoryParameter.java @@ -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 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 7a48e2c..7980255 100644 --- a/src/main/java/eu/svjatoslav/commons/commandline/parameterparser/parameter/FileParameter.java +++ b/src/main/java/eu/svjatoslav/commons/commandline/parameterparser/parameter/FileParameter.java @@ -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 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 7af305b..25755e4 100644 --- a/src/main/java/eu/svjatoslav/commons/commandline/parameterparser/parameter/IntegerParameter.java +++ b/src/main/java/eu/svjatoslav/commons/commandline/parameterparser/parameter/IntegerParameter.java @@ -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; + } + } + } 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 81ed2c0..acac388 100644 --- a/src/main/java/eu/svjatoslav/commons/commandline/parameterparser/parameter/NullParameter.java +++ b/src/main/java/eu/svjatoslav/commons/commandline/parameterparser/parameter/NullParameter.java @@ -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 ""; + } + + @Override + public boolean validate(final java.lang.String value) { + return true; + } + } 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 29967bc..dc70c26 100644 --- a/src/main/java/eu/svjatoslav/commons/commandline/parameterparser/parameter/StringParameter.java +++ b/src/main/java/eu/svjatoslav/commons/commandline/parameterparser/parameter/StringParameter.java @@ -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 index 2dfd300..0000000 --- a/src/main/java/eu/svjatoslav/commons/commandline/parameterparser/type/IntegerArgument.java +++ /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 index 1ba894a..0000000 --- a/src/main/java/eu/svjatoslav/commons/commandline/parameterparser/type/StringArgument.java +++ /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; - } - -}