-
Notifications
You must be signed in to change notification settings - Fork 65
/
Copy pathcommand3.java
81 lines (70 loc) · 2.59 KB
/
command3.java
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
package command;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.function.Consumer;
public interface command3 {
class Config {
boolean showHidden = false;
boolean longForm = false;
boolean showInode = false;
boolean showHelp = false;
@Override
public String toString() {
return "Config[showHidden: %s, longForm: %s, showInode: %s, showHelp: %s]"
.formatted(showHidden, longForm, showInode, showHelp);
}
}
record Command(String name, Consumer<Config> action) {}
record CommandRegistry(Map<String, Command> commandMap, String help) {
public static class Builder {
private final HashMap<String, Command> map = new HashMap<>();
private final StringBuilder help = new StringBuilder();
public Builder registerOptions(List<String> options, String description, Consumer<Config> action) {
var command = new Command(options.get(0), action);
options.forEach(option -> map.put(option, command));
help.append(String.join(", ", options)).append(": ").append(description).append("\n");
return this;
}
public CommandRegistry toRegistry() {
return new CommandRegistry(Map.copyOf(map), help.toString());
}
}
public Command command(String option) {
return commandMap.get(option);
}
}
static Config config(CommandRegistry registry, List<String> args) {
var config = new Config();
var commandSet = new HashSet<String>();
for(var arg: args) {
var command = registry.command(arg);
if (command == null) {
continue; // ignore
}
if (!commandSet.add(command.name)) {
throw new IllegalStateException(command.name + " specified twice");
}
command.action.accept(config);
}
return config;
}
static CommandRegistry commandRegistry() {
return new CommandRegistry.Builder()
.registerOptions(List.of("--all", "-a"), "show hidden files", c -> c.showHidden = true)
.registerOptions(List.of("--long", "-l"), "long form", c -> c.longForm = true)
.registerOptions(List.of("--inode", "-i"), "show inodes", c -> c.showInode = true)
.registerOptions(List.of("--help", "-h"), "show this help", c -> c.showHelp = true)
.toRegistry();
}
static void main(String[] args) {
args = new String[] { "--all", "foo", "-i", "--help" }; // DEBUG
var registry = commandRegistry();
var config = config(registry, List.of(args));
System.out.println(config);
if (config.showHelp) {
System.out.println(registry.help());
}
}
}