Skip to content

Commit 3efd55a

Browse files
fix: Add checking for number of positional args
1 parent d968fda commit 3efd55a

2 files changed

Lines changed: 11 additions & 3 deletions

File tree

src/main/java/net/marcellperger/mathexpr/IntRange.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,9 @@ public int getMax() {
3030
public boolean includes(int v) {
3131
return lo <= v && v <= hi;
3232
}
33+
34+
public String fancyRepr() {
35+
if(lo == hi) return "exactly %d".formatted(lo);
36+
return "%d to %d".formatted(lo, hi);
37+
}
3338
}

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

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package net.marcellperger.mathexpr.cli.minicli;
22

3-
import net.marcellperger.mathexpr.IntRange;
43
import net.marcellperger.mathexpr.UIntRange;
54
import net.marcellperger.mathexpr.util.Pair;
65
import net.marcellperger.mathexpr.util.Util;
@@ -12,7 +11,6 @@
1211
import java.util.HashMap;
1312
import java.util.List;
1413
import java.util.Map;
15-
import java.util.Objects;
1614
import java.util.function.Function;
1715

1816
public class MiniCLI {
@@ -124,7 +122,11 @@ public void pumpSingleArg(String arg) {
124122
}
125123

126124
public void finish() {
127-
if(prev != null) flushPrev();
125+
if(prev != null) flushPrev(); // TODO handle required kw-args
126+
int nArgs = positionalArgs.size();
127+
if(!nPositionalArgs.includes(nArgs))
128+
throw new CLIParseException("Incorrect number of positional args (required %s, got %d)"
129+
.formatted(nPositionalArgs.fancyRepr(), nArgs));
128130
}
129131

130132
// Makes more logical sense reading the code
@@ -153,6 +155,7 @@ private void addPositionalArg(String arg) {
153155
int maxArgc = nPositionalArgs.getMax();
154156
if(newSize > maxArgc)
155157
throw new CLIParseException("Too many positional args (expected max %d, got %d)".formatted(maxArgc, newSize));
158+
positionalArgs.add(arg);
156159
}
157160
}
158161
private enum ArgType {

0 commit comments

Comments
 (0)