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;
14 import java.util.List;
16 public abstract class Parameter<T, I extends Parameter> {
18 public final String description;
19 public final List<String> arguments = new ArrayList<>();
20 final ArgumentCount argumentCount;
21 private final List<String> aliases = new ArrayList<>();
23 * Indicates that at least one argument is mandatory for this parameter.
25 protected boolean mandatory;
27 * If this parameter was present in the commandline, then this boolean will
28 * be set to <code>true</code>.
30 private boolean specified;
32 public Parameter(final boolean mandatory,
33 final ArgumentCount argumentCount, final String description,
34 final String... aliases2) {
36 this.mandatory = mandatory;
37 this.description = description;
38 this.argumentCount = argumentCount;
41 Collections.addAll(aliases, aliases2);
45 public Parameter(final String description, final ArgumentCount argumentCount) {
46 this.description = description;
47 this.argumentCount = argumentCount;
50 @SuppressWarnings("unchecked")
51 public I addAliases(final String... aliasArray) {
54 Collections.addAll(aliases, aliasArray);
60 * @param argumentString argument to add
61 * @return <code>true</code> if no errors were found. <code>false</code>
64 public boolean addArgument(final String argumentString) {
65 // check if arguments are allowed for this parameter
66 if (argumentCount.equals(ArgumentCount.NONE)) {
68 .println("Error! No arguments are allowed for parameters: "
73 // check if multiple arguments are allowed
74 if ((arguments.size() > 0)
75 && (argumentCount.equals(ArgumentCount.SINGLE))) {
77 .println("Error! Only single argument is allowed for parameters: "
82 if (!validate(argumentString)) {
84 System.out.println("Error! Invalid argument \"" + argumentString
85 + "\". It shall be " + describeFormat() + ".");
90 arguments.add(argumentString);
96 * @return Single line argument type description.
98 public abstract String describeFormat();
100 public String getAliases() {
101 final StringBuilder buffer = new StringBuilder();
103 for (final String alias : aliases) {
105 if (buffer.length() > 0)
108 buffer.append(alias);
111 return buffer.toString();
114 public String getHelp() {
115 final StringBuilder buffer = new StringBuilder();
118 buffer.append(getAliases());
119 if (!argumentCount.equals(ArgumentCount.NONE)) {
122 .append(isMandatory() ? "mandatory, " : "")
123 .append(describeFormat())
126 if (argumentCount.equals(ArgumentCount.MULTI))
127 buffer.append("...");
132 buffer.append(" " + description + "\n");
134 return buffer.toString();
137 public abstract T getValue();
139 public boolean isMandatory() {
144 * @return the parameterSpecified
146 public boolean isSpecified() {
151 * @param specified the parameterSpecified to set
153 protected void setSpecified(final boolean specified) {
154 this.specified = specified;
158 * @param alias alias to check against
159 * @return <code>true</code> if given alias is registered for this
162 public boolean matchesAlias(final String alias) {
163 return aliases.contains(alias);
168 * Notifies this parameter that no more arguments will follow. This gives
169 * parameter chance to verify if this is ok.
171 * @return <code>true</code> if no errors were found. <code>false</code>
174 public boolean noMoreArguments() {
176 if ((!argumentCount.equals(ArgumentCount.NONE))
177 && (arguments.isEmpty())) {
179 System.out.println("Error! " + getAliases()
180 + " require at least one following argument.");
187 @SuppressWarnings("unchecked")
188 public I setMandatory() {
194 * @param value value to validate
195 * @return <code>true</code> if value is correct, <code>false</code>
198 public abstract boolean validate(String value);