2 * Svjatoslav Commons - shared library of common functionality.
3 * Copyright ©2012-2013, 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 2 of the GNU General Public License
7 * 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 class Parameter {
19 * Indicates that at least one argument is mandatory for this parameter.
21 protected boolean mandatory;
23 private final Argument argumentType;
25 private final ArrayList<String> aliases = new ArrayList<String>();
27 public final String description;
29 protected final ArrayList<String> arguments = new ArrayList<String>();
31 ArgumentCount argumentCount;
34 * If this parameter was present in the commandline, then this boolean will
35 * be set to <code>true</code>.
37 private boolean parameterSpecified;
39 public Parameter(final Argument argumentType, final String description,
40 final ArgumentCount argumentCount) {
42 this.description = description;
43 this.argumentType = argumentType;
44 this.argumentCount = argumentCount;
47 public Parameter(final boolean mandatory,
48 final ArgumentCount argumentCount, final Argument argumentType,
49 final String description, final String... aliases2) {
51 this.mandatory = mandatory;
52 this.argumentType = argumentType;
53 this.description = description;
54 this.argumentCount = argumentCount;
57 for (final String alias : aliases2)
62 public Parameter addAliases(final String... aliasArray) {
65 for (final String alias : aliasArray)
72 * @return <code>true</code> if no errors were found. <code>false</code>
75 public boolean addArgument(final String argumentString) {
76 // check if arguments are allowed for this parameter
77 if (argumentCount.equals(ArgumentCount.NONE)) {
79 .println("Error! No arguments are allowed for parameters: "
84 // check if multiple arguments are allowed
85 if ((arguments.size() > 0)
86 && (argumentCount.equals(ArgumentCount.SINGLE))) {
88 .println("Error! Only single argument is allowed for parameters: "
93 if (!argumentType.validate(argumentString)) {
95 System.out.println("Error! Invalid argument \"" + argumentString
96 + "\". It shall be " + argumentType.describeFormat() + ".");
101 arguments.add(argumentString);
106 public String getAliases() {
107 final StringBuffer buffer = new StringBuffer();
109 for (final String alias : aliases) {
111 if (buffer.length() > 0)
114 buffer.append(alias);
117 return buffer.toString();
120 public File getArgumentAsFile() {
121 if (arguments.size() != 1)
122 throw new RuntimeException("Parameter " + description
123 + " shall have exactly 1 argument.");
124 return new File(arguments.get(0));
127 public List<File> getArgumentsAsFiles() {
128 final ArrayList<File> result = new ArrayList<File>();
130 for (final String argument : arguments) {
131 final File file = new File(argument);
138 public List<Integer> getArgumentsAsIntegers() {
139 final ArrayList<Integer> result = new ArrayList<Integer>();
141 for (final String argument : arguments)
142 result.add(Integer.valueOf(argument));
147 public List<String> getArgumentsAsStrings() {
148 final ArrayList<String> result = new ArrayList<String>(arguments);
152 public String getHelp() {
153 final StringBuffer buffer = new StringBuffer();
156 buffer.append(getAliases());
157 if (!argumentCount.equals(ArgumentCount.NONE)) {
158 buffer.append(" (" + argumentType.describeFormat() + ")");
160 if (argumentCount.equals(ArgumentCount.MULTI))
161 buffer.append("...");
166 buffer.append(" " + description + "\n");
168 return buffer.toString();
171 public boolean isMandatory() {
176 * @return the parameterSpecified
178 public boolean isParameterSpecified() {
179 return parameterSpecified;
183 * @return <code>true</code> if given alias is registered for this
186 public boolean matchesAlias(final String alias) {
187 if (aliases.contains(alias))
194 * Notifies this parameter that no more arguments will follow. This gives
195 * parameter chance to verify if this is ok.
197 * @return <code>true</code> if no errors were found. <code>false</code>
200 public boolean noMoreArguments() {
202 if ((!argumentCount.equals(ArgumentCount.NONE))
203 && (arguments.isEmpty())) {
205 System.out.println("Error! " + getAliases()
206 + " require at least one following argument.");
213 public Parameter setMandatory() {
219 * @param parameterSpecified
220 * the parameterSpecified to set
222 public void setParameterSpecified(final boolean parameterSpecified) {
223 this.parameterSpecified = parameterSpecified;