-
Notifications
You must be signed in to change notification settings - Fork 65
/
Copy pathcommand1.java
66 lines (61 loc) · 1.74 KB
/
command1.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
package command;
import java.util.List;
public interface command1 {
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);
}
}
static Config config(List<String> args) {
var config = new Config();
for(var arg: args) {
switch (arg) {
case "-a", "--all" -> {
if (config.showHidden) {
throw new IllegalStateException("--all specified twice");
}
config.showHidden = true;
}
case "-l", "--long" -> {
if (config.longForm) {
throw new IllegalStateException("--long specified twice");
}
config.longForm = true;
}
case "-i", "--inode" -> {
if (config.showInode) {
throw new IllegalStateException("--inode specified twice");
}
config.showInode = true;
}
case "-h", "--help" -> {
if (config.showHelp) {
throw new IllegalStateException("--help specified twice");
}
config.showHelp = true;
}
default -> {} // ignore
}
}
return config;
}
static void main(String[] args){
args = new String[] { "--all", "foo", "-i", "--help" }; // DEBUG
var config = config(List.of(args));
System.out.println(config);
if (config.showHelp) {
System.out.println("""
--all, -a: show hidden files
--long, -l: long form
--inode, -i: show inodes
--help, -h: show this help
""");
}
}
}