summaryrefslogtreecommitdiff
path: root/BJC-Utils2/src/main/java/bjc/utils/cli/objects/Command.java
blob: a0d44933755d437b323fd9fb7f1fc956472251ba (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
package bjc.utils.cli.objects;

public class Command {
	public final int lineNo;

	public final String fullCommand;
	public final String remnCommand;
	public final String nameCommand;

	public final String ioSource;

	/**
	 * Create a new command.
	 *
	 * @param ln
	 * 	The line 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();

		fullCommand = ln;
		nameCommand = ln.substring(0, idx).trim();
		remnCommand = ln.substring(idx).trim();

		lineNo = lno;

		ioSource = ioSrc;
	}

	public static Command fromString(String ln, int lno, String ioSource) {
		/*
		 * 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, ioSource);
	}

	public String warn(String warning, Object... parms) {
		String msg = String.format(warning, parms);

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

	public String error(String err, Object... parms) {
		String msg = String.format(err, parms);

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