Skip to content

Commit 003752a

Browse files
feat: Finish MiniCLI.Parser
1 parent 148e3ce commit 003752a

1 file changed

Lines changed: 26 additions & 14 deletions

File tree

  • src/main/java/net/marcellperger/mathexpr/cli/minicli

src/main/java/net/marcellperger/mathexpr/cli/minicli/MiniCLI.java

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,12 @@ public Parser(List<String> args) {
6060
}
6161

6262
public void parse() {
63+
pumpAllArgs();
64+
finish();
65+
}
66+
67+
public void pumpAllArgs() {
6368
args.forEach(this::pumpSingleArg);
64-
// TODO: finalize: check prev, handle if not null
6569
}
6670

6771
// Make this public as it could be useful for making stream-ing type stuff
@@ -74,30 +78,41 @@ public void pumpSingleArg(String arg) {
7478
}
7579
switch (argT) {
7680
case NORMAL -> {
77-
if (prev == null) positionalArgs.add(arg);
78-
else flushPrevWithValue(arg);
81+
if (prev != null) flushPrevWithValue(arg);
82+
else positionalArgs.add(arg);
7983
}
8084
case SINGLE -> {
8185
if(prev != null) flushPrev();
82-
if(arg.contains("=")) {
83-
Pair<String, String> kv = Pair.ofArray(arg.split("=", 2));
84-
CLIOption<?> opt = lookupOption(kv.left);
85-
opt.setValueFromString(kv.right);
86-
} else {
86+
if(!setFromKeyEqualsValue(arg)) {
8787
prev = lookupOption(arg);
8888
// flush immediately if next one cannot be a value
8989
if(!prev.supportsSeparateValueAfterShortForm()) flushPrev();
9090
}
9191
}
9292
case DOUBLE -> {
9393
if(prev != null) flushPrev();
94-
95-
// TODO lookup here
96-
prev = null;
94+
if(!setFromKeyEqualsValue(arg)) {
95+
lookupOption(arg).setValueFromString(null);
96+
}
9797
}
9898
}
9999
}
100100

101+
public void finish() {
102+
if(prev != null) flushPrev();
103+
}
104+
105+
// Makes more logical sense reading the code
106+
// (`if (![did]setFromKeyEqualValue(arg)) {...}` )
107+
@SuppressWarnings("BooleanMethodIsAlwaysInverted")
108+
private boolean /*return success?*/ setFromKeyEqualsValue(String arg) {
109+
if(!arg.contains("=")) return false;
110+
Pair<String, String> kv = Pair.ofArray(arg.split("=", 2));
111+
CLIOption<?> opt = lookupOption(kv.left);
112+
opt.setValueFromString(kv.right);
113+
return true;
114+
}
115+
101116
private void flushPrev() {
102117
Util.realAssert(prev != null, "flushPrev must have a `prev` to flush");
103118
prev = null;
@@ -107,9 +122,6 @@ private void flushPrevWithValue(String value) {
107122
prev.setValueFromString(value);
108123
prev = null;
109124
}
110-
}
111-
private record PrevState(CLIOption<?> option, @Nullable String value) {
112-
113125
}
114126
private enum ArgType {
115127
NORMAL, SINGLE, DOUBLE;

0 commit comments

Comments
 (0)