summaryrefslogtreecommitdiff
path: root/base/src/main/java/bjc/utils/cli/CommandMode.java
blob: c145c1c99c2ec0b24f57f4ed85c3f284012d30e3 (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
package bjc.utils.cli;

/**
 * A mode for determining the commands that are valid to enter, and then
 * handling those commands.
 *
 * @author ben
 */
public interface CommandMode extends Comparable<CommandMode> {
	/**
	 * Check to see if this mode can handle the specified command.
	 *
	 * @param command
	 *                The command to check.
	 *
	 * @return Whether or not this mode can handle the command. It is assumed not by
	 *         default.
	 */
	default boolean canHandle(final String command) {
		return false;
	}

	/**
	 * Get the custom prompt for this mode.
	 *
	 * @return The custom prompt for this mode.
	 *
	 * @throws UnsupportedOperationException
	 *                                       If this mode doesn't support a custom
	 *                                       prompt.
	 */
	default String getCustomPrompt() {
		throw new UnsupportedOperationException(
				"This mode doesn't support a custom prompt");
	}

	/**
	 * Get the name of this command mode.
	 *
	 * @return The name of this command mode, or a default string if one isn't
	 *         specified.
	 */
	public default String getName() {
		return "(anonymous)";
	}

	/**
	 * Check if this mode uses a custom prompt.
	 *
	 * @return Whether or not this mode uses a custom prompt.
	 */
	default boolean isCustomPromptEnabled() {
		return false;
	}

	/**
	 * Process a command in this mode..
	 *
	 * @param command
	 *                The command to process.
	 *
	 * @param args
	 *                A list of arguments to the command.
	 *
	 * @return The command mode to use for the next command. Defaults to doing
	 *         nothing, and staying in the current mode.
	 */
	default CommandMode process(final String command, final String[] args) {
		return this;
	}

	@Override
	default int compareTo(final CommandMode o) {
		return getName().compareTo(o.getName());
	}
}