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