2 * Svjatoslav Commons - shared library of common functionality.
3 * Copyright ©2012-2017, 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;
13 import java.util.Collections;
15 public abstract class Parameter<T, I extends Parameter> {
17 public final String description;
18 public final ArrayList<String> arguments = new ArrayList<>();
19 final ArgumentCount argumentCount;
20 private final ArrayList<String> aliases = new ArrayList<>();
22 * Indicates that at least one argument is mandatory for this parameter.
24 protected boolean mandatory;
26 * If this parameter was present in the commandline, then this boolean will
27 * be set to <code>true</code>.
29 private boolean specified;
31 public Parameter(final boolean mandatory,
32 final ArgumentCount argumentCount, final String description,
33 final String... aliases2) {
35 this.mandatory = mandatory;
36 this.description = description;
37 this.argumentCount = argumentCount;
40 Collections.addAll(aliases, aliases2);
44 public Parameter(final String description, final ArgumentCount argumentCount) {
45 this.description = description;
46 this.argumentCount = argumentCount;
49 @SuppressWarnings("unchecked")
50 public I addAliases(final String... aliasArray) {
53 Collections.addAll(aliases, aliasArray);
59 * @param argumentString argument to add
60 * @return <code>true</code> if no errors were found. <code>false</code>
63 public boolean addArgument(final String argumentString) {
64 // check if arguments are allowed for this parameter
65 if (argumentCount.equals(ArgumentCount.NONE)) {
67 .println("Error! No arguments are allowed for parameters: "
72 // check if multiple arguments are allowed
73 if ((arguments.size() > 0)
74 && (argumentCount.equals(ArgumentCount.SINGLE))) {
76 .println("Error! Only single argument is allowed for parameters: "
81 if (!validate(argumentString)) {
83 System.out.println("Error! Invalid argument \"" + argumentString
84 + "\". It shall be " + describeFormat() + ".");
89 arguments.add(argumentString);
95 * @return Single line argument type description.
97 public abstract String describeFormat();
99 public String getAliases() {
100 final StringBuilder buffer = new StringBuilder();
102 for (final String alias : aliases) {
104 if (buffer.length() > 0)
107 buffer.append(alias);
110 return buffer.toString();
113 public String getHelp() {
114 final StringBuilder buffer = new StringBuilder();
117 buffer.append(getAliases());
118 if (!argumentCount.equals(ArgumentCount.NONE)) {
121 .append(isMandatory() ? "mandatory, " : "")
122 .append(describeFormat())
125 if (argumentCount.equals(ArgumentCount.MULTI))
126 buffer.append("...");
131 buffer.append(" " + description + "\n");
133 return buffer.toString();
136 public abstract T getValue();
138 public boolean isMandatory() {
143 * @return the parameterSpecified
145 public boolean isSpecified() {
150 * @param specified the parameterSpecified to set
152 protected void setSpecified(final boolean specified) {
153 this.specified = specified;
157 * @param alias alias to check against
158 * @return <code>true</code> if given alias is registered for this
161 public boolean matchesAlias(final String alias) {
162 return aliases.contains(alias);
167 * Notifies this parameter that no more arguments will follow. This gives
168 * parameter chance to verify if this is ok.
170 * @return <code>true</code> if no errors were found. <code>false</code>
173 public boolean noMoreArguments() {
175 if ((!argumentCount.equals(ArgumentCount.NONE))
176 && (arguments.isEmpty())) {
178 System.out.println("Error! " + getAliases()
179 + " require at least one following argument.");
186 @SuppressWarnings("unchecked")
187 public I setMandatory() {
193 * @param value value to validate
194 * @return <code>true</code> if value is correct, <code>false</code>
197 public abstract boolean validate(String value);