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.commandline.parameterparser;
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;
20 public final List<String> arguments = new ArrayList<>();
21 final ArgumentCount argumentCount;
22 private final List<String> aliases = new ArrayList<>();
24 * Indicates that at least one argument is mandatory for this parameter.
26 protected boolean mandatory;
28 * If this parameter was present in the commandline, then this boolean will
29 * be set to <code>true</code>.
31 private boolean specified;
33 public Parameter(final boolean mandatory,
34 final ArgumentCount argumentCount, final String description,
35 final String... aliases2) {
37 this.mandatory = mandatory;
38 this.description = description;
39 this.argumentCount = argumentCount;
42 Collections.addAll(aliases, aliases2);
46 public Parameter(final String description, final ArgumentCount argumentCount) {
47 this.description = description;
48 this.argumentCount = argumentCount;
51 @SuppressWarnings("unchecked")
52 public I addAliases(final String... aliasArray) {
55 Collections.addAll(aliases, aliasArray);
61 * @param argumentString argument to add
62 * @return <code>true</code> if no errors were found. <code>false</code>
65 public boolean addArgument(final String argumentString) {
66 // check if arguments are allowed for this parameter
67 if (argumentCount.equals(ArgumentCount.NONE)) {
69 .println("Error! No arguments are allowed for parameters: "
74 // check if multiple arguments are allowed
75 if ((arguments.size() > 0)
76 && (argumentCount.equals(ArgumentCount.SINGLE))) {
78 .println("Error! Only single argument is allowed for parameters: "
83 if (!validate(argumentString)) {
85 System.out.println("Error! Invalid argument \"" + argumentString
86 + "\". It shall be " + describeFormat() + ".");
91 arguments.add(argumentString);
97 * @return Single line argument type description. If argument type should be file,
98 * date, integer, regular expression, etc..
100 * Note: for argument purpose description there is {@link #description}
102 public abstract String describeFormat();
104 public String getAliases() {
105 final StringBuilder buffer = new StringBuilder();
107 for (final String alias : aliases) {
109 if (buffer.length() > 0)
112 buffer.append(alias);
115 return buffer.toString();
118 public String getHelp() {
119 final StringBuilder buffer = new StringBuilder();
122 buffer.append(getAliases());
123 if (!argumentCount.equals(ArgumentCount.NONE)) {
126 .append(isMandatory() ? "mandatory, " : "")
127 .append(describeFormat())
130 if (argumentCount.equals(ArgumentCount.MULTI))
131 buffer.append("...");
136 buffer.append(" " + description + "\n");
138 return buffer.toString();
141 public abstract T getValue();
143 public boolean isMandatory() {
148 * @return the parameterSpecified
150 public boolean isSpecified() {
155 * @param specified the parameterSpecified to set
157 protected void setSpecified(final boolean specified) {
158 this.specified = specified;
162 * @param alias alias to check against
163 * @return <code>true</code> if given alias is registered for this
166 public boolean matchesAlias(final String alias) {
167 return aliases.contains(alias);
172 * Notifies this parameter that no more arguments will follow. This gives
173 * parameter chance to verify if this is ok.
175 * @return <code>true</code> if no errors were found. <code>false</code>
178 public boolean noMoreArguments() {
180 if ((!argumentCount.equals(ArgumentCount.NONE))
181 && (arguments.isEmpty())) {
183 System.out.println("Error! " + getAliases()
184 + " require at least one following argument.");
191 @SuppressWarnings("unchecked")
192 public I setMandatory() {
198 * @param value value to validate
199 * @return <code>true</code> if value is correct, <code>false</code>
202 public abstract boolean validate(String value);