summaryrefslogtreecommitdiff
path: root/base/src/main/java/bjc/utils/cli/objects/Command.java
blob: e31839c9b9a152e5205288fea580cad4f594df92 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
package bjc.utils.cli.objects;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * A single-line command read from the user.
 *
 * @author Ben Culkin
 */
public class Command {
	/**
	 * Command status values.
	 *
	 * @author Ben Culkin
	 */
	public static enum CommandStatus {
		/**
		 * The command succeeded.
		 */
		SUCCESS,
		/**
		 * The command failed non-fatally.
		 */
		FAIL,
		/**
		 * The command failed fatally.
		 */
		ERROR,
		/**
		 * The command was the last one.
		 */
		FINISH,
	}

	/**
	 * The line number of this command.
	 */
	public final int lno;

	/**
	 * The full text of this command.
	 */
	public final String full;
	/**
	 * The text of this command without its name.
	 */
	public String remn;
	/**
	 * The name of this command.
	 */
	public final String name;

	/**
	 * The name of the I/O source this command was read from.
	 */
	public final String src;

	/**
	 * Create a new command.
	 *
	 * @param ln
	 *                The string to get the command from.
	 *
	 * @param lno
	 *                The number of the line the command came from.
	 *
	 * @param ioSrc
	 *                The name of where the I/O came from.
	 */
	public Command(String ln, int lno, String ioSrc) {
		int idx = ln.indexOf(' ');

		if (idx == -1) idx = ln.length();

		/* Grab command parts. */
		full = ln;
		name = ln.substring(0, idx).trim();
		remn = ln.substring(idx).trim();

		this.lno = lno;

		src = ioSrc;
	}

	/**
	 * Removes up until the first occurrence of a particular string for the
	 * remaining command, and returns the removed string.
	 * 
	 * By default, both the substring and the remaining text are trimmed
	 * (leading/trailing spaces removed).
	 * 
	 * @param delm
	 *                The delimiter to stop substringing at.
	 * 
	 * @return The substring, or null if there is no occurrence of the
	 *         delimiter.
	 */
	public String trimTo(String delm) {
		return trimTo(delm, true);
	}

	/**
	 * Removes up until the first occurrence of a particular string for the
	 * remaining command, and returns the removed string.
	 * 
	 * @param delm
	 *                The delimiter to stop substringing at.
	 * @param doTrim
	 *                Whether or not to trim the substring and remaining
	 *                command (Remove leading/trailing spaces).
	 * 
	 * @return The substring, or null if there is no occurrence of the
	 *         delimiter.
	 */
	public String trimTo(String delm, boolean doTrim) {
		int idx = remn.indexOf(delm);
		if (idx == -1) return null;

		String tmp = remn.substring(0, idx);
		remn = remn.substring(idx);

		if (doTrim) {
			tmp = tmp.trim();
			remn = remn.trim();
		}

		return tmp;
	}

	/**
	 * Removes up until the first occurrence of a particular regex for the
	 * remaining command, and returns the removed string.
	 * 
	 * By default, both the substring and the remaining text are trimmed
	 * (leading/trailing spaces removed).
	 * 
	 * @param rDelm
	 *                The regex to stop substringing at.
	 * 
	 * @return The first capturing group (the entire match otherwise), or
	 *         null if there is no occurrence of the delimiter.
	 */
	public String trimToRX(String rDelm) {
		return trimToRX(Pattern.compile(rDelm), true);
	}

	/**
	 * Removes up until the first occurrence of a particular regex for the
	 * remaining command, and returns the removed string.
	 * 
	 * By default, both the substring and the remaining text are trimmed
	 * (leading/trailing spaces removed).
	 * 
	 * @param delm
	 *                The regex to stop substringing at.
	 * 
	 * @return The first capturing group (the entire match otherwise), or
	 *         null if there is no occurrence of the delimiter.
	 */
	public String trimToRX(Pattern delm) {
		return trimToRX(delm, true);
	}

	/**
	 * Removes up until the first occurrence of a particular regex for the
	 * remaining command, and returns the removed string.
	 * 
	 * @param rDelm
	 *                The regex to stop substringing at.
	 * @param doTrim
	 *                Whether or not to trim the substring and remaining
	 *                command (Remove leading/trailing spaces).
	 * 
	 * @return The first capturing group (the entire match otherwise), or
	 *         null if there is no occurrence of the delimiter.
	 */
	public String trimToRX(String rDelm, boolean doTrim) {
		return trimToRX(Pattern.compile(rDelm), doTrim);
	}

	/**
	 * Removes up until the first occurrence of a particular regex for the
	 * remaining command, and returns the removed string.
	 *
	 * @param delm
	 *                The regex to stop substringing at.
	 * @param doTrim
	 *                Whether or not to trim the substring and remaining
	 *                command (Remove leading/trailing spaces).
	 * 
	 * @return The first capturing group (the entire match otherwise), or
	 *         null if there is no occurrence of the delimiter.
	 */
	public String trimToRX(Pattern delm, boolean doTrim) {
		Matcher mat = delm.matcher(remn);
		if (!mat.find()) return null;

		String tmp;

		if (mat.groupCount() > 0) {
			tmp = mat.group(1);
			remn = remn.substring(mat.end());
		} else {
			tmp = mat.group();
			remn = remn.substring(mat.end());
		}

		if (doTrim) {
			tmp = tmp.trim();
			remn = remn.trim();
		}

		return tmp;
	}

	/**
	 * Removes up until the first occurrence of a particular string for the
	 * remaining command, and returns the removed string.
	 * 
	 * By default, both the substring and the remaining text are trimmed
	 * (leading/trailing spaces removed).
	 * 
	 * @param delm
	 *                The delimiter to stop substringing at.
	 * 
	 * @return The substring, or null if there is no occurrence of the
	 *         delimiter.
	 */
	public String trimTo(char delm) {
		return trimTo(delm, true);
	}

	/**
	 * Removes up until the first occurrence of a particular string for the
	 * remaining command, and returns the removed string.
	 * 
	 * @param delm
	 *                The delimiter to stop substringing at.
	 * @param doTrim
	 *                Whether or not to trim the substring and remaining
	 *                command (Remove leading/trailing spaces).
	 * 
	 * @return The substring, or null if there is no occurrence of the
	 *         delimiter.
	 */
	public String trimTo(char delm, boolean doTrim) {
		int idx = remn.indexOf(delm);
		if (idx == -1) return null;

		String tmp = remn.substring(0, idx);
		remn = remn.substring(idx);

		if (doTrim) {
			tmp = tmp.trim();
			remn = remn.trim();
		}

		return tmp;
	}

	/**
	 * Check if this command has text after its name.
	 * 
	 * @return Whether or not this command has text after its name.
	 */
	public boolean hasRemaining() {
		return !remn.equals("");
	}

	/**
	 * Parse a command from a string.
	 *
	 * The main thing this does is ignore blank lines, as well as comments
	 * marked by #'s either at the start of the line or part of the way
	 * through the line.
	 *
	 * @param ln
	 *                The string to get the command from.
	 *
	 * @param lno
	 *                The line number of the command.
	 *
	 * @param srcName
	 *                The name of where the I/O came from.
	 * @return The parsed command
	 */
	public static Command fromString(String ln, int lno, String srcName) {
		/* Ignore blank lines and comments. */
		if (ln.equals("")) return null;
		if (ln.startsWith("#")) return null;

		/* Trim off comments part-way through the line. */
		int idxHash = ln.indexOf('#');
		if (idxHash != -1) {
			ln = ln.substring(0, idxHash).trim();
		}

		return new Command(ln, lno, srcName);
	}

	/**
	 * Give an informational message about something in relation to this
	 * command.
	 *
	 * @param info
	 *                The informational message.
	 *
	 * @param parms
	 *                The parameters for the informational message.
	 * @return The information message.
	 */
	public String info(String info, Object... parms) {
		String msg = String.format(info, parms);

		return String.format("INFO (%s:%d): %s", src, lno, msg);
	}

	/**
	 * Warn about something in relation to this command.
	 *
	 * @param warning
	 *                The warning message.
	 *
	 * @param parms
	 *                The parameters for the warning message.
	 * 
	 * @return The formatted warning.
	 */
	public String warn(String warning, Object... parms) {
		String msg = String.format(warning, parms);

		return String.format("WARNING (%s:%d): %s", src, lno, msg);
	}

	/**
	 * Give an error about something in relation to this command.
	 *
	 * @param err
	 *                The error message.
	 *
	 * @param parms
	 *                The parameters for the error message.
	 * 
	 * @return The formatted error
	 */
	public String error(String err, Object... parms) {
		String msg = String.format(err, parms);

		return String.format("ERROR (%s:%d): %s", src, lno, msg);
	}
}