2 * Svjatoslav Commons - shared library of common functionality.
3 * Copyright (C) 2012, 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 private final boolean mandatory;
23 private final boolean enableArguments;
25 private final boolean enableMultipleArguments;
27 private final Argument argumentType;
29 private final ArrayList<String> aliases;
31 private final String description;
33 private final ArrayList<String> arguments = new ArrayList<String>();
36 * If this parameter was present in the commandline, then this boolean will
37 * be set to <code>true</code>.
39 private boolean parameterSpecified;
41 public Parameter(final boolean mandatory, final boolean enableArguments,
42 final boolean enableMultipleArguments, final Argument argumentType,
43 final String description, final String... aliases) {
45 this.mandatory = mandatory;
46 this.enableArguments = enableArguments;
47 this.enableMultipleArguments = enableMultipleArguments;
48 this.argumentType = argumentType;
49 this.description = description;
53 final ArrayList<String> aliasesList = new ArrayList<String>();
54 for (final String alias : aliases) {
55 aliasesList.add(alias);
57 this.aliases = aliasesList;
62 public Parameter(final String description, final String... aliases) {
63 this(false, false, false, null, description, aliases);
67 * @return <code>true</code> if no errors were found. <code>false</code>
70 public boolean addArgument(final String argumentString) {
71 // check if arguments are allowed for this parameter
72 if (!enableArguments) {
74 .println("Error! No arguments are allowed for parameters: "
79 // check if multiple arguments are allowed
80 if ((arguments.size() > 0) && (!enableMultipleArguments)) {
82 .println("Error! Only single argument is allowed for parameters: "
87 if (!argumentType.validate(argumentString)) {
89 System.out.println("Error! Invalid argument \"" + argumentString
90 + "\". It shall be " + argumentType.describeFormat() + ".");
95 arguments.add(argumentString);
100 public String getAliases() {
101 final StringBuffer buffer = new StringBuffer();
103 for (final String alias : aliases) {
105 if (buffer.length() > 0)
108 buffer.append(alias);
111 return buffer.toString();
114 public File getArgumentAsFile() {
115 if (arguments.size() != 1) {
116 throw new RuntimeException("Parameter " + description
117 + " shall have exactly 1 argument.");
119 return new File(arguments.get(0));
122 public int getArgumentAsInteger() {
123 if (arguments.size() != 1) {
124 throw new RuntimeException("Parameter " + description
125 + " shall have exactly 1 argument.");
127 return Integer.parseInt(arguments.get(0));
130 public String getArgumentAsString() {
131 if (arguments.size() != 1) {
132 throw new RuntimeException("Parameter " + description
133 + " shall have exactly 1 argument.");
135 return arguments.get(0);
138 public List<File> getArgumentsAsFiles() {
139 final ArrayList<File> result = new ArrayList<File>();
141 for (final String argument : arguments) {
142 final File file = new File(argument);
149 public List<Integer> getArgumentsAsIntegers() {
150 final ArrayList<Integer> result = new ArrayList<Integer>();
152 for (final String argument : arguments) {
153 result.add(Integer.valueOf(argument));
159 public List<String> getArgumentsAsStrings() {
160 final ArrayList<String> result = new ArrayList<String>(arguments);
164 public String getHelp() {
165 final StringBuffer buffer = new StringBuffer();
168 buffer.append(getAliases());
169 if (enableArguments) {
170 buffer.append(" (" + argumentType.describeFormat() + ")");
172 if (enableMultipleArguments) {
173 buffer.append("...");
179 buffer.append(" " + description + "\n");
181 return buffer.toString();
184 public boolean isMandatory() {
189 * @return the parameterSpecified
191 public boolean isParameterSpecified() {
192 return parameterSpecified;
196 * @return <code>true</code> if given alias is registered for this
199 public boolean matchesAlias(final String alias) {
200 if (aliases.contains(alias))
207 * Notifies this parameter that no more arguments will follow. This gives
208 * parameter chance to verify if this is ok.
210 * @return <code>true</code> if no errors were found. <code>false</code>
213 public boolean noMoreArguments() {
215 if (enableArguments && (arguments.isEmpty())) {
217 System.out.println("Error! " + getAliases()
218 + " require at least one following argument.");
226 * @param parameterSpecified
227 * the parameterSpecified to set
229 public void setParameterSpecified(final boolean parameterSpecified) {
230 this.parameterSpecified = parameterSpecified;