2 * Svjatoslav Commons - shared library of common functionality. Author: Svjatoslav Agejenko.
3 * This project is released under Creative Commons Zero (CC0) license.
5 package eu.svjatoslav.commons.cli_helper.parameter_parser;
7 import java.util.ArrayList;
8 import java.util.Collections;
11 public abstract class Parameter<T, I extends Parameter> {
14 * Purpose of this argument, like: input image path, compression level, etc...
16 * Note: for describing argument type (file, integer, ...) there is {@link #describeFormat()}.
18 public final String description;
21 * List of arguments for this parameter.
23 public final List<String> arguments = new ArrayList<>();
24 final ArgumentCount argumentCount;
25 private final List<String> aliases = new ArrayList<>();
27 * Indicates that at least one argument is mandatory for this parameter.
29 protected boolean mandatory;
31 * If this parameter was present in the commandline, then this boolean will
32 * be set to <code>true</code>.
34 private boolean specified;
36 public Parameter(final boolean mandatory,
37 final ArgumentCount argumentCount, final String description,
38 final String... aliases2) {
40 this.mandatory = mandatory;
41 this.description = description;
42 this.argumentCount = argumentCount;
45 Collections.addAll(aliases, aliases2);
49 public Parameter(final String description, final ArgumentCount argumentCount) {
50 this.description = description;
51 this.argumentCount = argumentCount;
54 @SuppressWarnings("unchecked")
55 public I addAliases(final String... aliasArray) {
58 Collections.addAll(aliases, aliasArray);
64 * @param argumentString argument to add
65 * @return <code>true</code> if no errors were found. <code>false</code>
68 public boolean addArgument(final String argumentString) {
69 // check if arguments are allowed for this parameter
70 if (argumentCount.equals(ArgumentCount.NONE)) {
72 .println("Error! No arguments are allowed for parameters: "
77 // check if multiple arguments are allowed
78 if ((arguments.size() > 0)
79 && (argumentCount.equals(ArgumentCount.SINGLE))) {
81 .println("Error! Only single argument is allowed for parameters: "
86 if (!validate(argumentString)) {
88 System.out.println("Error! Invalid argument \"" + argumentString
89 + "\". It shall be " + describeFormat() + ".");
94 arguments.add(argumentString);
100 * @return Single line argument type description. If argument type should be file,
101 * date, integer, regular expression, etc..
103 * Note: for argument purpose description there is {@link #description}
105 public abstract String describeFormat();
107 public String getAliases() {
108 final StringBuilder buffer = new StringBuilder();
110 for (final String alias : aliases) {
112 if (buffer.length() > 0)
115 buffer.append(alias);
118 return buffer.toString();
121 public String getHelp() {
122 final StringBuilder buffer = new StringBuilder();
125 buffer.append(getAliases());
126 if (!argumentCount.equals(ArgumentCount.NONE)) {
129 .append(isMandatory() ? "mandatory, " : "")
130 .append(describeFormat())
133 if (argumentCount.equals(ArgumentCount.MULTI))
134 buffer.append("...");
139 buffer.append(" " + description + "\n");
141 return buffer.toString();
144 public abstract T getValue();
146 public boolean isMandatory() {
151 * @return the parameterSpecified
153 public boolean isSpecified() {
158 * @param specified the parameterSpecified to set
160 protected void setSpecified(final boolean specified) {
161 this.specified = specified;
165 * @param alias alias to check against
166 * @return <code>true</code> if given alias is registered for this
169 public boolean matchesAlias(final String alias) {
170 return aliases.contains(alias);
175 * Notifies this parameter that no more arguments will follow. This gives
176 * parameter chance to verify if this is ok.
178 * @return <code>true</code> if no errors were found. <code>false</code>
181 public boolean noMoreArguments() {
183 if ((!argumentCount.equals(ArgumentCount.NONE))
184 && (arguments.isEmpty())) {
186 System.out.println("Error! " + getAliases()
187 + " require at least one following argument.");
194 @SuppressWarnings("unchecked")
195 public I setMandatory() {
201 * @param value value to validate
202 * @return <code>true</code> if value is correct, <code>false</code>
205 public abstract boolean validate(String value);