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;
10 import static eu.svjatoslav.commons.cli_helper.parameter_parser.ParameterCount.NONE;
11 import static java.lang.String.join;
12 import static java.util.Collections.addAll;
13 import static java.util.stream.Collectors.joining;
15 public abstract class Option<T, I extends Option> {
18 * Purpose of this option, like: input image path, compression level, etc...
20 * Note: for describing option type there is {@link #describeFormat()}.
22 public final String description;
25 * List of arguments for this parameter.
27 public final List<String> parameters = new ArrayList<>();
28 final ParameterCount parameterCount;
31 * List of aliases for this option.
33 private final List<String> aliases = new ArrayList<>();
36 * Indicates that at least one parameter is mandatory for this option.
38 protected boolean mandatory;
41 * If this parameter was present in the commandline, then this boolean will
42 * be set to <code>true</code>.
44 private boolean isPresent;
46 public Option(final boolean mandatory,
47 final ParameterCount parameterCount, final String description,
48 final String... aliases2) {
50 this.mandatory = mandatory;
51 this.description = description;
52 this.parameterCount = parameterCount;
55 addAll(aliases, aliases2);
58 public Option(final String description, final ParameterCount parameterCount) {
59 this.description = description;
60 this.parameterCount = parameterCount;
63 @SuppressWarnings("unchecked")
64 public I addAliases(final String... aliasArray) {
67 addAll(aliases, aliasArray);
73 * @param parameterString parameter to add
74 * @return <code>true</code> if no errors were found. <code>false</code>
77 public boolean addParameter(final String parameterString) {
78 // check if arguments are allowed for this parameter
79 if (parameterCount.equals(NONE)) {
81 .println("Error! No parameters are allowed for option(s): "
82 + getAliasesAsString());
86 // check if multiple arguments are allowed
87 if ((!parameters.isEmpty())
88 && (parameterCount.equals(ParameterCount.ONE))) {
90 .println("Error! Only single parameter is allowed for argument(s): "
91 + getAliasesAsString());
95 if (isValid(parameterString)) {
96 parameters.add(parameterString);
99 System.out.println("Error! Invalid parameter type for \"" + parameterString
100 + "\". It shall be " + describeFormat() + ".");
106 * @return Single line parameter type description. For example: "file", "date", "integer", "regular expression", etc..
108 * Note: for argument purpose description there is {@link #description}
110 public abstract String describeFormat();
113 * @return aliases for this parameter as string.
115 public String getAliasesAsString() {
116 return join(", ", aliases);
120 * @return help for this option.
122 public String getHelp() {
123 final StringBuilder result = new StringBuilder();
126 result.append(getAliasesAsString());
127 if (!NONE.equals(parameterCount)) {
130 .append(isMandatory() ? "mandatory, " : "")
131 .append(describeFormat())
134 if (parameterCount.equals(ParameterCount.ONE_OR_MORE))
135 result.append("...");
140 result.append(" " + description + "\n");
142 return result.toString();
145 public abstract T getValue();
147 public boolean isMandatory() {
152 * @return the parameterSpecified
154 public boolean isPresent() {
159 * @param present the parameterSpecified to set
161 protected void setPresent(final boolean present) {
162 this.isPresent = present;
166 * @param alias alias to check against
167 * @return <code>true</code> if given alias is registered for this
170 public boolean matchesAlias(final String alias) {
171 return aliases.contains(alias);
176 * Notifies this option that no more parameters will follow.
177 * This gives option chance to verify if this is ok.
179 * @return <code>true</code> if no errors were found. <code>false</code>
182 public boolean noMoreArguments() {
184 if ((!parameterCount.equals(NONE))
185 && (parameters.isEmpty())) {
187 System.out.println("Error! " + getAliasesAsString()
188 + " require at least one parameter.");
195 @SuppressWarnings("unchecked")
196 public I setMandatory() {
202 * @param value value to validate
203 * @return <code>true</code> if value is correct, <code>false</code>
206 public abstract boolean isValid(String value);