Skip to content

Commit b6df9f6

Browse files
refactor: Start creating MiniCLI
1 parent ca7406f commit b6df9f6

5 files changed

Lines changed: 128 additions & 0 deletions

File tree

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package net.marcellperger.mathexpr.cli;
2+
3+
import java.util.Arrays;
4+
5+
public class CLI {
6+
public static void main(String[] args) {
7+
System.out.println("args = " + Arrays.stream(args).map(s -> "`" + s + "`").toList());
8+
}
9+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package net.marcellperger.mathexpr.cli.minicli;
2+
3+
import java.util.List;
4+
5+
public class BooleanCLIOption extends CLIOption<Boolean> {
6+
public BooleanCLIOption(List<String> optionNames) {
7+
super(Boolean.class, optionNames);
8+
}
9+
10+
@Override
11+
public void setValueFromString(String s) {
12+
setValue(switch (s.strip().toLowerCase()) {
13+
case "0", "no", "false" -> false;
14+
case "1", "yes", "true" -> true;
15+
case String s2 -> throw new IllegalArgumentException("Bad boolean value '" + s2 + "'"); // TODO special CLIParseException
16+
});
17+
}
18+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package net.marcellperger.mathexpr.cli.minicli;
2+
3+
import org.jetbrains.annotations.Nullable;
4+
5+
import java.util.List;
6+
7+
public abstract class CLIOption<T> {
8+
List<String> names;
9+
Class<T> type;
10+
@Nullable T value;
11+
12+
public CLIOption(Class<T> valueType, List<String> optionNames) {
13+
names = optionNames;
14+
type = valueType;
15+
}
16+
17+
public List<String> getNames() {
18+
return names;
19+
}
20+
21+
public @Nullable T getValue() {
22+
return value;
23+
}
24+
public void setValue(@Nullable T value) {
25+
this.value = value;
26+
}
27+
28+
public abstract void setValueFromString(String s);
29+
30+
public void reset() {
31+
value = null;
32+
}
33+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package net.marcellperger.mathexpr.cli.minicli;
2+
3+
import org.jetbrains.annotations.Contract;
4+
import org.jetbrains.annotations.NotNull;
5+
import org.jetbrains.annotations.Nullable;
6+
7+
import java.util.ArrayList;
8+
import java.util.HashMap;
9+
import java.util.List;
10+
import java.util.Map;
11+
12+
public class MiniCLI {
13+
Map<String, CLIOption<?>> options = new HashMap<>();
14+
List<String> positionalArgs = new ArrayList<>();
15+
16+
public MiniCLI() {
17+
18+
}
19+
20+
@Contract("_ -> new")
21+
public CLIOption<String> addStringOption(String @NotNull ... names) {
22+
// First validate all of them so we don't mutate unless all are valid
23+
for(String name : names) {
24+
if (!name.startsWith("-")) throw new IllegalArgumentException("Option name must start with '-'");
25+
if (options.containsKey(name)) {
26+
throw new IllegalStateException("Argument '%s' has already been registered".formatted(name));
27+
}
28+
}
29+
CLIOption<String> opt = new StringCLIOption(List.of(names));
30+
for(String name : names) {
31+
options.put(name, opt);
32+
}
33+
return opt;
34+
}
35+
36+
37+
@Contract("_ -> this")
38+
public MiniCLI parseArgs(String[] args) {
39+
return parseArgs(List.of(args));
40+
}
41+
@Contract("_ -> this")
42+
public MiniCLI parseArgs(List<String> args) {
43+
44+
return this;
45+
}
46+
private enum ArgType {
47+
NORMAL, SINGLE, DOUBLE;
48+
public static ArgType fromArg(String arg) {
49+
if(arg.startsWith("--")) return DOUBLE;
50+
if(arg.startsWith("-")) return SINGLE;
51+
return NORMAL;
52+
}
53+
}
54+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package net.marcellperger.mathexpr.cli.minicli;
2+
3+
import java.util.List;
4+
5+
public class StringCLIOption extends CLIOption<String> {
6+
public StringCLIOption(List<String> keys) {
7+
super(String.class, keys);
8+
}
9+
10+
@Override
11+
public void setValueFromString(String s) {
12+
setValue(s);
13+
}
14+
}

0 commit comments

Comments
 (0)