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