2 * Svjatoslav Commons - shared library of common functionality.
3 * Copyright ©2012-2020, 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> {
19 * Purpose of this argument, like: input image path, compression level, etc...
21 * Note: for describing argument type (file, integer, ...) there is {@link #describeFormat()}.
23 public final String description;
25 public final List<String> arguments = new ArrayList<>();
26 final ArgumentCount argumentCount;
27 private final List<String> aliases = new ArrayList<>();
29 * Indicates that at least one argument is mandatory for this parameter.
31 protected boolean mandatory;
33 * If this parameter was present in the commandline, then this boolean will
34 * be set to <code>true</code>.
36 private boolean specified;
38 public Parameter(final boolean mandatory,
39 final ArgumentCount argumentCount, final String description,
40 final String... aliases2) {
42 this.mandatory = mandatory;
43 this.description = description;
44 this.argumentCount = argumentCount;
47 Collections.addAll(aliases, aliases2);
51 public Parameter(final String description, final ArgumentCount argumentCount) {
52 this.description = description;
53 this.argumentCount = argumentCount;
56 @SuppressWarnings("unchecked")
57 public I addAliases(final String... aliasArray) {
60 Collections.addAll(aliases, aliasArray);
66 * @param argumentString argument to add
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. If argument type should be file,
103 * date, integer, regular expression, etc..
105 * Note: for argument purpose description there is {@link #description}
107 public abstract String describeFormat();
109 public String getAliases() {
110 final StringBuilder buffer = new StringBuilder();
112 for (final String alias : aliases) {
114 if (buffer.length() > 0)
117 buffer.append(alias);
120 return buffer.toString();
123 public String getHelp() {
124 final StringBuilder buffer = new StringBuilder();
127 buffer.append(getAliases());
128 if (!argumentCount.equals(ArgumentCount.NONE)) {
131 .append(isMandatory() ? "mandatory, " : "")
132 .append(describeFormat())
135 if (argumentCount.equals(ArgumentCount.MULTI))
136 buffer.append("...");
141 buffer.append(" " + description + "\n");
143 return buffer.toString();
146 public abstract T getValue();
148 public boolean isMandatory() {
153 * @return the parameterSpecified
155 public boolean isSpecified() {
160 * @param specified the parameterSpecified to set
162 protected void setSpecified(final boolean specified) {
163 this.specified = specified;
167 * @param alias alias to check against
168 * @return <code>true</code> if given alias is registered for this
171 public boolean matchesAlias(final String alias) {
172 return aliases.contains(alias);
177 * Notifies this parameter that no more arguments will follow. This gives
178 * parameter chance to verify if this is ok.
180 * @return <code>true</code> if no errors were found. <code>false</code>
183 public boolean noMoreArguments() {
185 if ((!argumentCount.equals(ArgumentCount.NONE))
186 && (arguments.isEmpty())) {
188 System.out.println("Error! " + getAliases()
189 + " require at least one following argument.");
196 @SuppressWarnings("unchecked")
197 public I setMandatory() {
203 * @param value value to validate
204 * @return <code>true</code> if value is correct, <code>false</code>
207 public abstract boolean validate(String value);