2 * Svjatoslav Commons - shared library of common functionality.
3 * Copyright ©2012-2014, 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;
13 import java.util.ArrayList;
14 import java.util.List;
16 public abstract class Parameter {
19 * Indicates that at least one argument is mandatory for this parameter.
21 protected boolean mandatory;
23 private final ArrayList<String> aliases = new ArrayList<String>();
25 public final String description;
27 protected final ArrayList<String> arguments = new ArrayList<String>();
29 ArgumentCount argumentCount;
32 * If this parameter was present in the commandline, then this boolean will
33 * be set to <code>true</code>.
35 private boolean parameterSpecified;
37 public Parameter(final boolean mandatory,
38 final ArgumentCount argumentCount, final String description,
39 final String... aliases2) {
41 this.mandatory = mandatory;
42 this.description = description;
43 this.argumentCount = argumentCount;
46 for (final String alias : aliases2)
51 public Parameter(final String description, final ArgumentCount argumentCount) {
52 this.description = description;
53 this.argumentCount = argumentCount;
56 public Parameter addAliases(final String... aliasArray) {
59 for (final String alias : aliasArray)
66 * @return <code>true</code> if no errors were found. <code>false</code>
69 public boolean addArgument(final String argumentString) {
70 // check if arguments are allowed for this parameter
71 if (argumentCount.equals(ArgumentCount.NONE)) {
73 .println("Error! No arguments are allowed for parameters: "
78 // check if multiple arguments are allowed
79 if ((arguments.size() > 0)
80 && (argumentCount.equals(ArgumentCount.SINGLE))) {
82 .println("Error! Only single argument is allowed for parameters: "
87 if (!validate(argumentString)) {
89 System.out.println("Error! Invalid argument \"" + argumentString
90 + "\". It shall be " + describeFormat() + ".");
95 arguments.add(argumentString);
101 * @return Single line argument type description.
103 public abstract String describeFormat();
105 public String getAliases() {
106 final StringBuffer buffer = new StringBuffer();
108 for (final String alias : aliases) {
110 if (buffer.length() > 0)
113 buffer.append(alias);
116 return buffer.toString();
119 public List<File> getArgumentsAsFiles() {
120 final ArrayList<File> result = new ArrayList<File>();
122 for (final String argument : arguments) {
123 final File file = new File(argument);
130 public List<Integer> getArgumentsAsIntegers() {
131 final ArrayList<Integer> result = new ArrayList<Integer>();
133 for (final String argument : arguments)
134 result.add(Integer.valueOf(argument));
139 public List<String> getArgumentsAsStrings() {
140 final ArrayList<String> result = new ArrayList<String>(arguments);
144 public String getHelp() {
145 final StringBuffer buffer = new StringBuffer();
148 buffer.append(getAliases());
149 if (!argumentCount.equals(ArgumentCount.NONE)) {
150 buffer.append(" (" + describeFormat() + ")");
152 if (argumentCount.equals(ArgumentCount.MULTI))
153 buffer.append("...");
158 buffer.append(" " + description + "\n");
160 return buffer.toString();
163 public boolean isMandatory() {
168 * @return the parameterSpecified
170 public boolean isParameterSpecified() {
171 return parameterSpecified;
175 * @return <code>true</code> if given alias is registered for this
178 public boolean matchesAlias(final String alias) {
179 if (aliases.contains(alias))
186 * Notifies this parameter that no more arguments will follow. This gives
187 * parameter chance to verify if this is ok.
189 * @return <code>true</code> if no errors were found. <code>false</code>
192 public boolean noMoreArguments() {
194 if ((!argumentCount.equals(ArgumentCount.NONE))
195 && (arguments.isEmpty())) {
197 System.out.println("Error! " + getAliases()
198 + " require at least one following argument.");
205 public Parameter setMandatory() {
211 * @param parameterSpecified
212 * the parameterSpecified to set
214 public void setParameterSpecified(final boolean parameterSpecified) {
215 this.parameterSpecified = parameterSpecified;
219 * @return <code>true</code> if value is correct, <code>false</code>
222 public abstract boolean validate(String value);