further improvements to commandline parser API
[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         protected boolean mandatory;
22
23         private final Argument argumentType;
24
25         private final ArrayList<String> aliases = new ArrayList<String>();
26
27         public final String description;
28
29         protected final ArrayList<String> arguments = new ArrayList<String>();
30
31         ArgumentCount argumentCount;
32
33         /**
34          * If this parameter was present in the commandline, then this boolean will
35          * be set to <code>true</code>.
36          */
37         private boolean parameterSpecified;
38
39         public Parameter(final Argument argumentType, final String description,
40                         final ArgumentCount argumentCount) {
41
42                 this.description = description;
43                 this.argumentType = argumentType;
44                 this.argumentCount = argumentCount;
45         };
46
47         public Parameter(final boolean mandatory,
48                         final ArgumentCount argumentCount, final Argument argumentType,
49                         final String description, final String... aliases2) {
50
51                 this.mandatory = mandatory;
52                 this.argumentType = argumentType;
53                 this.description = description;
54                 this.argumentCount = argumentCount;
55
56                 // save aliases
57                 for (final String alias : aliases2)
58                         aliases.add(alias);
59
60         }
61
62         public Parameter addAliases(final String... aliasArray) {
63
64                 // save aliases
65                 for (final String alias : aliasArray)
66                         aliases.add(alias);
67
68                 return this;
69         }
70
71         /**
72          * @return <code>true</code> if no errors were found. <code>false</code>
73          *         otherwise.
74          */
75         public boolean addArgument(final String argumentString) {
76                 // check if arguments are allowed for this parameter
77                 if (argumentCount.equals(ArgumentCount.NONE)) {
78                         System.out
79                                         .println("Error! No arguments are allowed for parameters: "
80                                                         + getAliases());
81                         return false;
82                 }
83
84                 // check if multiple arguments are allowed
85                 if ((arguments.size() > 0)
86                                 && (argumentCount.equals(ArgumentCount.SINGLE))) {
87                         System.out
88                                         .println("Error! Only single argument is allowed for parameters: "
89                                                         + getAliases());
90                         return false;
91                 }
92
93                 if (!argumentType.validate(argumentString)) {
94
95                         System.out.println("Error! Invalid argument \"" + argumentString
96                                         + "\". It shall be " + argumentType.describeFormat() + ".");
97                         return false;
98
99                 }
100
101                 arguments.add(argumentString);
102
103                 return true;
104         }
105
106         public String getAliases() {
107                 final StringBuffer buffer = new StringBuffer();
108
109                 for (final String alias : aliases) {
110
111                         if (buffer.length() > 0)
112                                 buffer.append(", ");
113
114                         buffer.append(alias);
115                 }
116
117                 return buffer.toString();
118         }
119
120         public List<File> getArgumentsAsFiles() {
121                 final ArrayList<File> result = new ArrayList<File>();
122
123                 for (final String argument : arguments) {
124                         final File file = new File(argument);
125                         result.add(file);
126                 }
127
128                 return result;
129         }
130
131         public List<Integer> getArgumentsAsIntegers() {
132                 final ArrayList<Integer> result = new ArrayList<Integer>();
133
134                 for (final String argument : arguments)
135                         result.add(Integer.valueOf(argument));
136
137                 return result;
138         }
139
140         public List<String> getArgumentsAsStrings() {
141                 final ArrayList<String> result = new ArrayList<String>(arguments);
142                 return result;
143         }
144
145         public String getHelp() {
146                 final StringBuffer buffer = new StringBuffer();
147
148                 // first line
149                 buffer.append(getAliases());
150                 if (!argumentCount.equals(ArgumentCount.NONE)) {
151                         buffer.append(" (" + argumentType.describeFormat() + ")");
152
153                         if (argumentCount.equals(ArgumentCount.MULTI))
154                                 buffer.append("...");
155                 }
156                 buffer.append("\n");
157
158                 // second line
159                 buffer.append("    " + description + "\n");
160
161                 return buffer.toString();
162         }
163
164         public boolean isMandatory() {
165                 return mandatory;
166         }
167
168         /**
169          * @return the parameterSpecified
170          */
171         public boolean isParameterSpecified() {
172                 return parameterSpecified;
173         }
174
175         /**
176          * @return <code>true</code> if given alias is registered for this
177          *         parameter.
178          */
179         public boolean matchesAlias(final String alias) {
180                 if (aliases.contains(alias))
181                         return true;
182
183                 return false;
184         }
185
186         /**
187          * Notifies this parameter that no more arguments will follow. This gives
188          * parameter chance to verify if this is ok.
189          * 
190          * @return <code>true</code> if no errors were found. <code>false</code>
191          *         otherwise.
192          */
193         public boolean noMoreArguments() {
194
195                 if ((!argumentCount.equals(ArgumentCount.NONE))
196                                 && (arguments.isEmpty())) {
197
198                         System.out.println("Error! " + getAliases()
199                                         + " require at least one following argument.");
200
201                         return false;
202                 }
203                 return true;
204         }
205
206         public Parameter setMandatory() {
207                 mandatory = true;
208                 return this;
209         }
210
211         /**
212          * @param parameterSpecified
213          *            the parameterSpecified to set
214          */
215         public void setParameterSpecified(final boolean parameterSpecified) {
216                 this.parameterSpecified = parameterSpecified;
217         }
218
219 }