fixed copyright headers
[svjatoslav_commons.git] / src / main / java / eu / svjatoslav / commons / commandline / parameterparser / Parameter.java
1 /*
2  * Svjatoslav Commons - shared library of common functionality.
3  * Copyright ©2012-2013, Svjatoslav Agejenko, svjatoslav@svjatoslav.eu
4  * 
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.
8  */
9
10 package eu.svjatoslav.commons.commandline.parameterparser;
11
12 import java.io.File;
13 import java.util.ArrayList;
14 import java.util.List;
15
16 public class Parameter {
17
18         /**
19          * Indicates that at least one argument is mandatory for this parameter.
20          */
21         private final boolean mandatory;
22
23         private final boolean enableArguments;
24
25         private final boolean enableMultipleArguments;
26
27         private final Argument argumentType;
28
29         private final ArrayList<String> aliases;
30
31         private final String description;
32
33         private final ArrayList<String> arguments = new ArrayList<String>();
34
35         /**
36          * If this parameter was present in the commandline, then this boolean will
37          * be set to <code>true</code>.
38          */
39         private boolean parameterSpecified;
40
41         public Parameter(final boolean mandatory, final boolean enableArguments,
42                         final boolean enableMultipleArguments, final Argument argumentType,
43                         final String description, final String... aliases) {
44
45                 this.mandatory = mandatory;
46                 this.enableArguments = enableArguments;
47                 this.enableMultipleArguments = enableMultipleArguments;
48                 this.argumentType = argumentType;
49                 this.description = description;
50
51                 // save aliases
52                 {
53                         final ArrayList<String> aliasesList = new ArrayList<String>();
54                         for (final String alias : aliases)
55                                 aliasesList.add(alias);
56                         this.aliases = aliasesList;
57                 }
58
59         }
60
61         public Parameter(final String description, final String... aliases) {
62                 this(false, false, false, null, description, aliases);
63         }
64
65         /**
66          * @return <code>true</code> if no errors were found. <code>false</code>
67          *         otherwise.
68          */
69         public boolean addArgument(final String argumentString) {
70                 // check if arguments are allowed for this parameter
71                 if (!enableArguments) {
72                         System.out
73                                         .println("Error! No arguments are allowed for parameters: "
74                                                         + getAliases());
75                         return false;
76                 }
77
78                 // check if multiple arguments are allowed
79                 if ((arguments.size() > 0) && (!enableMultipleArguments)) {
80                         System.out
81                                         .println("Error! Only single argument is allowed for parameters: "
82                                                         + getAliases());
83                         return false;
84                 }
85
86                 if (!argumentType.validate(argumentString)) {
87
88                         System.out.println("Error! Invalid argument \"" + argumentString
89                                         + "\". It shall be " + argumentType.describeFormat() + ".");
90                         return false;
91
92                 }
93
94                 arguments.add(argumentString);
95
96                 return true;
97         }
98
99         public String getAliases() {
100                 final StringBuffer buffer = new StringBuffer();
101
102                 for (final String alias : aliases) {
103
104                         if (buffer.length() > 0)
105                                 buffer.append(", ");
106
107                         buffer.append(alias);
108                 }
109
110                 return buffer.toString();
111         }
112
113         public File getArgumentAsFile() {
114                 if (arguments.size() != 1)
115                         throw new RuntimeException("Parameter " + description
116                                         + " shall have exactly 1 argument.");
117                 return new File(arguments.get(0));
118         }
119
120         public int getArgumentAsInteger() {
121                 if (arguments.size() != 1)
122                         throw new RuntimeException("Parameter " + description
123                                         + " shall have exactly 1 argument.");
124                 return Integer.parseInt(arguments.get(0));
125         }
126
127         public String getArgumentAsString() {
128                 if (arguments.size() != 1)
129                         throw new RuntimeException("Parameter " + description
130                                         + " shall have exactly 1 argument.");
131                 return arguments.get(0);
132         }
133
134         public List<File> getArgumentsAsFiles() {
135                 final ArrayList<File> result = new ArrayList<File>();
136
137                 for (final String argument : arguments) {
138                         final File file = new File(argument);
139                         result.add(file);
140                 }
141
142                 return result;
143         }
144
145         public List<Integer> getArgumentsAsIntegers() {
146                 final ArrayList<Integer> result = new ArrayList<Integer>();
147
148                 for (final String argument : arguments)
149                         result.add(Integer.valueOf(argument));
150
151                 return result;
152         }
153
154         public List<String> getArgumentsAsStrings() {
155                 final ArrayList<String> result = new ArrayList<String>(arguments);
156                 return result;
157         }
158
159         public String getHelp() {
160                 final StringBuffer buffer = new StringBuffer();
161
162                 // first line
163                 buffer.append(getAliases());
164                 if (enableArguments) {
165                         buffer.append(" (" + argumentType.describeFormat() + ")");
166
167                         if (enableMultipleArguments)
168                                 buffer.append("...");
169                 }
170                 buffer.append("\n");
171
172                 // second line
173                 buffer.append("    " + description + "\n");
174
175                 return buffer.toString();
176         }
177
178         public boolean isMandatory() {
179                 return mandatory;
180         }
181
182         /**
183          * @return the parameterSpecified
184          */
185         public boolean isParameterSpecified() {
186                 return parameterSpecified;
187         }
188
189         /**
190          * @return <code>true</code> if given alias is registered for this
191          *         parameter.
192          */
193         public boolean matchesAlias(final String alias) {
194                 if (aliases.contains(alias))
195                         return true;
196
197                 return false;
198         }
199
200         /**
201          * Notifies this parameter that no more arguments will follow. This gives
202          * parameter chance to verify if this is ok.
203          * 
204          * @return <code>true</code> if no errors were found. <code>false</code>
205          *         otherwise.
206          */
207         public boolean noMoreArguments() {
208
209                 if (enableArguments && (arguments.isEmpty())) {
210
211                         System.out.println("Error! " + getAliases()
212                                         + " require at least one following argument.");
213
214                         return false;
215                 }
216                 return true;
217         }
218
219         /**
220          * @param parameterSpecified
221          *            the parameterSpecified to set
222          */
223         public void setParameterSpecified(final boolean parameterSpecified) {
224                 this.parameterSpecified = parameterSpecified;
225         }
226
227 }