Repository navigation
Expand file tree
/
Copy pathCommandContext.java
More file actions
129 lines (103 loc) · 3.56 KB
/
Copy pathCommandContext.java
File metadata and controls
129 lines (103 loc) · 3.56 KB
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
package org.javawebstack.command;
import org.javawebstack.command.handler.CommandExitException;
import org.javawebstack.command.io.Input;
import org.javawebstack.command.io.Output;
import org.javawebstack.command.router.OptionValues;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class CommandContext {
private final Map<String, Object> arguments;
private final Map<String, Object> parameters;
private final Map<String, List<OptionValues>> options;
private final Map<String, Object> attributes = new HashMap<>();
private Input input;
private Output output;
public CommandContext(Map<String, Object> arguments, Map<String, Object> parameters, Map<String, List<OptionValues>> options) {
this.arguments = arguments;
this.parameters = parameters;
this.options = options;
}
public <T> T arg(String name) {
if(!arguments.containsKey(name))
throw new IllegalArgumentException("Unknown argument '" + name + "'");
return (T) arguments.get(name);
}
public <T> List<T> varArg(String name) {
Object v = arg(name);
if(!(v instanceof List))
throw new IllegalArgumentException("Invalid vararg '" + name + "'");
return (List<T>) v;
}
public <T> T param(String name) {
if(!parameters.containsKey(name))
throw new IllegalArgumentException("Unknown parameter '" + name + "'");
return (T) parameters.get(name);
}
public boolean hasOption(String name) {
return optionCount(name) > 0;
}
public int optionCount(String name) {
if(!options.containsKey(name))
throw new IllegalArgumentException("Unknown option '" + name + "'");
return options.get(name).size();
}
public OptionValues option(String name) {
return option(name, 0);
}
public <T> T option(String name, String param) {
return option(name, 0).value(param);
}
public OptionValues option(String name, int i) {
if(!options.containsKey(name))
throw new IllegalArgumentException("Unknown option '" + name + "'");
List<OptionValues> instances = options.get(name);
if(i < 0 || i >= instances.size())
throw new IllegalArgumentException("The option '" + name + "' only exists " + instances.size() + " times, requested index " + i);
return instances.get(i);
}
public CommandContext attrib(String key, Object value) {
attributes.put(key, value);
return this;
}
public <T> T attrib(String key) {
return (T) attributes.get(key);
}
public void done() {
throw new CommandExitException(true);
}
public void print(String message) {
if(output != null)
output.print(message);
}
public void warn(String message) {
if(output != null)
output.warn(message);
}
public void error(String message) {
if(output != null)
output.error(message);
}
public void panic(String message) {
if(output != null)
output.error(message);
throw new CommandExitException(false);
}
public void panic(Throwable t) {
if(output != null)
output.error(t);
throw new CommandExitException(false);
}
public Input getInput() {
return input;
}
public Output getOutput() {
return output;
}
public void setInput(Input input) {
this.input = input;
}
public void setOutput(Output output) {
this.output = output;
}
}