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)) {
119 buffer.append(" (" + describeFormat() + ")");
121 if (argumentCount.equals(ArgumentCount.MULTI))
122 buffer.append("...");
127 buffer.append(" " + description + "\n");
129 return buffer.toString();
132 public abstract T getValue();
134 public boolean isMandatory() {
139 * @return the parameterSpecified
141 public boolean isSpecified() {
146 * @param specified the parameterSpecified to set
148 protected void setSpecified(final boolean specified) {
149 this.specified = specified;
153 * @param alias alias to check against
154 * @return <code>true</code> if given alias is registered for this
157 public boolean matchesAlias(final String alias) {
158 return aliases.contains(alias);
163 * Notifies this parameter that no more arguments will follow. This gives
164 * parameter chance to verify if this is ok.
166 * @return <code>true</code> if no errors were found. <code>false</code>
169 public boolean noMoreArguments() {
171 if ((!argumentCount.equals(ArgumentCount.NONE))
172 && (arguments.isEmpty())) {
174 System.out.println("Error! " + getAliases()
175 + " require at least one following argument.");
182 @SuppressWarnings("unchecked")
183 public I setMandatory() {
189 * @param value value to validate
190 * @return <code>true</code> if value is correct, <code>false</code>
193 public abstract boolean validate(String value);