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
|
package bjc.utils.cli.objects;
/**
* 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 succeded.
*/
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 lineNo;
/**
* The full text of this command.
*/
public final String fullCommand;
/**
* The text of this command without its name.
*/
public final String remnCommand;
/**
* The name of this command.
*/
public final String nameCommand;
/**
* The name of the I/O source this command was read from.
*/
public final String ioSource;
/**
* 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();
fullCommand = ln;
nameCommand = ln.substring(0, idx).trim();
remnCommand = ln.substring(idx).trim();
lineNo = lno;
ioSource = ioSrc;
}
/**
* 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 ioSource
* The name of where the I/O came from.
*/
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);
}
/**
* Give an informational message about something in relation to this
* command.
*
* @param info
* The informational message.
*
* @param parms
* The parameters for the informational message.
*/
public String info(String info, Object... parms) {
String msg = String.format(info, parms);
return String.format("INFO (%s:%d): %s", ioSource, lineNo, msg);
}
/**
* Warn about something in relation to this command.
*
* @param warning
* The warning message.
*
* @param parms
* The parameters for the warning message.
*/
public String warn(String warning, Object... parms) {
String msg = String.format(warning, parms);
return String.format("WARNING (%s:%d): %s", ioSource, lineNo, msg);
}
/**
* Give an error about something in relation to this command.
*
* @param error
* The error message.
*
* @param parms
* The parameters for the error message.
*/
public String error(String err, Object... parms) {
String msg = String.format(err, parms);
return String.format("ERROR (%s:%d): %s", ioSource, lineNo, msg);
}
}
|