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 public T addAliases(final String... aliasArray) {
57 for (final String alias : aliasArray)
64 * @return <code>true</code> if no errors were found. <code>false</code>
67 public boolean addArgument(final String argumentString) {
68 // check if arguments are allowed for this parameter
69 if (argumentCount.equals(ArgumentCount.NONE)) {
71 .println("Error! No arguments are allowed for parameters: "
76 // check if multiple arguments are allowed
77 if ((arguments.size() > 0)
78 && (argumentCount.equals(ArgumentCount.SINGLE))) {
80 .println("Error! Only single argument is allowed for parameters: "
85 if (!validate(argumentString)) {
87 System.out.println("Error! Invalid argument \"" + argumentString
88 + "\". It shall be " + describeFormat() + ".");
93 arguments.add(argumentString);
99 * @return Single line argument type description.
101 public abstract String describeFormat();
103 public String getAliases() {
104 final StringBuffer buffer = new StringBuffer();
106 for (final String alias : aliases) {
108 if (buffer.length() > 0)
111 buffer.append(alias);
114 return buffer.toString();
117 public String getHelp() {
118 final StringBuffer buffer = new StringBuffer();
121 buffer.append(getAliases());
122 if (!argumentCount.equals(ArgumentCount.NONE)) {
123 buffer.append(" (" + describeFormat() + ")");
125 if (argumentCount.equals(ArgumentCount.MULTI))
126 buffer.append("...");
131 buffer.append(" " + description + "\n");
133 return buffer.toString();
136 public abstract Object getValue();
138 public boolean isMandatory() {
143 * @return the parameterSpecified
145 public boolean isParameterSpecified() {
146 return parameterSpecified;
150 * @return <code>true</code> if given alias is registered for this
153 public boolean matchesAlias(final String alias) {
154 if (aliases.contains(alias))
161 * Notifies this parameter that no more arguments will follow. This gives
162 * parameter chance to verify if this is ok.
164 * @return <code>true</code> if no errors were found. <code>false</code>
167 public boolean noMoreArguments() {
169 if ((!argumentCount.equals(ArgumentCount.NONE))
170 && (arguments.isEmpty())) {
172 System.out.println("Error! " + getAliases()
173 + " require at least one following argument.");
180 public T setMandatory() {
186 * @param parameterSpecified
187 * the parameterSpecified to set
189 public void setParameterSpecified(final boolean parameterSpecified) {
190 this.parameterSpecified = parameterSpecified;
194 * @return <code>true</code> if value is correct, <code>false</code>
197 public abstract boolean validate(String value);