2 * Svjatoslav Commons - shared library of common functionality.
3 * Copyright ©2012-2014, Svjatoslav Agejenko, svjatoslav@svjatoslav.eu
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of version 3 of the GNU Lesser General Public License
7 * or later as published by the Free Software Foundation.
10 package eu.svjatoslav.commons.commandline.parameterparser;
12 import java.util.ArrayList;
14 public abstract class Parameter<T> {
17 * Indicates that at least one argument is mandatory for this parameter.
19 protected boolean mandatory;
21 private final ArrayList<String> aliases = new ArrayList<String>();
23 public final String description;
25 public final ArrayList<String> arguments = new ArrayList<String>();
27 final ArgumentCount argumentCount;
30 * If this parameter was present in the commandline, then this boolean will
31 * be set to <code>true</code>.
33 private boolean parameterSpecified;
35 public Parameter(final boolean mandatory,
36 final ArgumentCount argumentCount, final String description,
37 final String... aliases2) {
39 this.mandatory = mandatory;
40 this.description = description;
41 this.argumentCount = argumentCount;
44 for (final String alias : aliases2)
49 public Parameter(final String description, final ArgumentCount argumentCount) {
50 this.description = description;
51 this.argumentCount = argumentCount;
54 @SuppressWarnings("unchecked")
55 public T addAliases(final String... aliasArray) {
58 for (final String alias : aliasArray)
65 * @param argumentString
67 * @return <code>true</code> if no errors were found. <code>false</code>
70 public boolean addArgument(final String argumentString) {
71 // check if arguments are allowed for this parameter
72 if (argumentCount.equals(ArgumentCount.NONE)) {
74 .println("Error! No arguments are allowed for parameters: "
79 // check if multiple arguments are allowed
80 if ((arguments.size() > 0)
81 && (argumentCount.equals(ArgumentCount.SINGLE))) {
83 .println("Error! Only single argument is allowed for parameters: "
88 if (!validate(argumentString)) {
90 System.out.println("Error! Invalid argument \"" + argumentString
91 + "\". It shall be " + describeFormat() + ".");
96 arguments.add(argumentString);
102 * @return Single line argument type description.
104 public abstract String describeFormat();
106 public String getAliases() {
107 final StringBuffer buffer = new StringBuffer();
109 for (final String alias : aliases) {
111 if (buffer.length() > 0)
114 buffer.append(alias);
117 return buffer.toString();
120 public String getHelp() {
121 final StringBuffer buffer = new StringBuffer();
124 buffer.append(getAliases());
125 if (!argumentCount.equals(ArgumentCount.NONE)) {
126 buffer.append(" (" + describeFormat() + ")");
128 if (argumentCount.equals(ArgumentCount.MULTI))
129 buffer.append("...");
134 buffer.append(" " + description + "\n");
136 return buffer.toString();
139 public abstract Object getValue();
141 public boolean isMandatory() {
146 * @return the parameterSpecified
148 public boolean isParameterSpecified() {
149 return parameterSpecified;
154 * alias to check against
155 * @return <code>true</code> if given alias is registered for this
158 public boolean matchesAlias(final String alias) {
159 if (aliases.contains(alias))
166 * Notifies this parameter that no more arguments will follow. This gives
167 * parameter chance to verify if this is ok.
169 * @return <code>true</code> if no errors were found. <code>false</code>
172 public boolean noMoreArguments() {
174 if ((!argumentCount.equals(ArgumentCount.NONE))
175 && (arguments.isEmpty())) {
177 System.out.println("Error! " + getAliases()
178 + " require at least one following argument.");
185 @SuppressWarnings("unchecked")
186 public T setMandatory() {
192 * @param parameterSpecified
193 * the parameterSpecified to set
195 public void setParameterSpecified(final boolean parameterSpecified) {
196 this.parameterSpecified = parameterSpecified;
202 * @return <code>true</code> if value is correct, <code>false</code>
205 public abstract boolean validate(String value);