X-Git-Url: http://www2.svjatoslav.eu/gitweb/?a=blobdiff_plain;f=src%2Fmain%2Fjava%2Feu%2Fsvjatoslav%2Fcommons%2Fcommandline%2Fparameterparser%2FParameter.java;h=2e1c03ab06db5cf2020540c6cc74ae39946ba6dd;hb=b34ba4499cfbca09bc794a810e460bf1c86dcd34;hp=22cc92701e41b0fb21d20b4cb9b9f8c7cc7cd13e;hpb=f05caf52a82cdd174ded6bc6d8042200221b18d6;p=svjatoslav_commons.git 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 22cc927..2e1c03a 100755 --- a/src/main/java/eu/svjatoslav/commons/commandline/parameterparser/Parameter.java +++ b/src/main/java/eu/svjatoslav/commons/commandline/parameterparser/Parameter.java @@ -1,10 +1,10 @@ /* * Svjatoslav Commons - shared library of common functionality. - * Copyright ©2012-2013, Svjatoslav Agejenko, svjatoslav@svjatoslav.eu + * 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 2 of the GNU General Public License - * as published by the Free Software Foundation. + * 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; @@ -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,11 @@ public class Parameter { */ private boolean parameterSpecified; - 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) { + final ArgumentCount argumentCount, final String description, + final String... aliases2) { this.mandatory = mandatory; - this.argumentType = argumentType; this.description = description; this.argumentCount = argumentCount; @@ -57,6 +46,11 @@ public class Parameter { for (final String alias : aliases2) aliases.add(alias); + }; + + public Parameter(final String description, final ArgumentCount argumentCount) { + this.description = description; + this.argumentCount = argumentCount; } public Parameter addAliases(final String... aliasArray) { @@ -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; } @@ -103,6 +97,11 @@ public class Parameter { return true; } + /** + * @return Single line argument type description. + */ + public abstract String describeFormat(); + public String getAliases() { final StringBuffer buffer = new StringBuffer(); @@ -117,13 +116,6 @@ public class Parameter { return buffer.toString(); } - public File getArgumentAsFile() { - if (arguments.size() != 1) - throw new RuntimeException("Parameter " + description - + " shall have exactly 1 argument."); - return new File(arguments.get(0)); - } - public List getArgumentsAsFiles() { final ArrayList result = new ArrayList(); @@ -155,7 +147,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("..."); @@ -223,4 +215,10 @@ public class Parameter { this.parameterSpecified = parameterSpecified; } + /** + * @return true if value is correct, false + * otherwise. + */ + public abstract boolean validate(String value); + }