@@ -3,6 +3,7 @@ module argparse.internal.arguments;
3
3
import argparse.internal.lazystring;
4
4
5
5
import argparse.config;
6
+ import argparse.param;
6
7
import argparse.result;
7
8
8
9
import std.typecons : Nullable;
@@ -36,23 +37,23 @@ package(argparse) struct ArgumentInfo
36
37
Nullable! size_t minValuesCount;
37
38
Nullable! size_t maxValuesCount;
38
39
39
- auto checkValuesCount (const Config config, string argName, size_t count ) const
40
+ auto checkValuesCount (const ref RawParam param ) const
40
41
{
41
42
immutable min = minValuesCount.get ;
42
43
immutable max = maxValuesCount.get ;
43
44
44
45
// override for boolean flags
45
- if (isBooleanFlag && count == 1 )
46
+ if (isBooleanFlag && param.value.length == 1 )
46
47
return Result.Success;
47
48
48
- if (min == max && count != min)
49
- return Result.Error(" Argument '" ,config.styling.argumentName(argName )," ': expected " ,min,min == 1 ? " value" : " values" );
49
+ if (min == max && param.value.length != min)
50
+ return Result.Error(" Argument '" ,param. config.styling.argumentName(param.name )," ': expected " ,min,min == 1 ? " value" : " values" );
50
51
51
- if (count < min)
52
- return Result.Error(" Argument '" ,config.styling.argumentName(argName )," ': expected at least " ,min,min == 1 ? " value" : " values" );
52
+ if (param.value.length < min)
53
+ return Result.Error(" Argument '" ,param. config.styling.argumentName(param.name )," ': expected at least " ,min,min == 1 ? " value" : " values" );
53
54
54
- if (count > max)
55
- return Result.Error(" Argument '" ,config.styling.argumentName(argName )," ': expected at most " ,max,max == 1 ? " value" : " values" );
55
+ if (param.value.length > max)
56
+ return Result.Error(" Argument '" ,param. config.styling.argumentName(param.name )," ': expected at most " ,max,max == 1 ? " value" : " values" );
56
57
57
58
return Result.Success;
58
59
}
@@ -70,28 +71,36 @@ unittest
70
71
return info;
71
72
}
72
73
73
- assert (info(2 ,4 ).checkValuesCount(Config.init, " " , 1 ).isError(" expected at least 2 values" ));
74
- assert (info(2 ,4 ).checkValuesCount(Config.init, " " , 2 ));
75
- assert (info(2 ,4 ).checkValuesCount(Config.init, " " , 3 ));
76
- assert (info(2 ,4 ).checkValuesCount(Config.init, " " , 4 ));
77
- assert (info(2 ,4 ).checkValuesCount(Config.init, " " , 5 ).isError(" expected at most 4 values" ));
78
-
79
- assert (info(2 ,2 ).checkValuesCount(Config.init, " " , 1 ).isError(" expected 2 values" ));
80
- assert (info(2 ,2 ).checkValuesCount(Config.init, " " , 2 ));
81
- assert (info(2 ,2 ).checkValuesCount(Config.init, " " , 3 ).isError(" expected 2 values" ));
82
-
83
- assert (info(1 ,1 ).checkValuesCount(Config.init, " " , 0 ).isError(" expected 1 value" ));
84
- assert (info(1 ,1 ).checkValuesCount(Config.init, " " , 1 ));
85
- assert (info(1 ,1 ).checkValuesCount(Config.init, " " , 2 ).isError(" expected 1 value" ));
86
-
87
- assert (info(0 ,1 ).checkValuesCount(Config.init, " " , 0 ));
88
- assert (info(0 ,1 ).checkValuesCount(Config.init, " " , 1 ));
89
- assert (info(0 ,1 ).checkValuesCount(Config.init, " " , 2 ).isError(" expected at most 1 value" ));
90
-
91
- assert (info(1 ,2 ).checkValuesCount(Config.init, " " , 0 ).isError(" expected at least 1 value" ));
92
- assert (info(1 ,2 ).checkValuesCount(Config.init, " " , 1 ));
93
- assert (info(1 ,2 ).checkValuesCount(Config.init, " " , 2 ));
94
- assert (info(1 ,2 ).checkValuesCount(Config.init, " " , 3 ).isError(" expected at most 2 values" ));
74
+ Config config;
75
+ auto p0 = RawParam(&config, " " , []);
76
+ auto p1 = RawParam(&config, " " , [" " ,]);
77
+ auto p2 = RawParam(&config, " " , [" " ," " ,]);
78
+ auto p3 = RawParam(&config, " " , [" " ," " ," " ,]);
79
+ auto p4 = RawParam(&config, " " , [" " ," " ," " ," " ,]);
80
+ auto p5 = RawParam(&config, " " , [" " ," " ," " ," " ," " ,]);
81
+
82
+ assert (info(2 ,4 ).checkValuesCount(p1).isError(" expected at least 2 values" ));
83
+ assert (info(2 ,4 ).checkValuesCount(p2));
84
+ assert (info(2 ,4 ).checkValuesCount(p3));
85
+ assert (info(2 ,4 ).checkValuesCount(p4));
86
+ assert (info(2 ,4 ).checkValuesCount(p5).isError(" expected at most 4 values" ));
87
+
88
+ assert (info(2 ,2 ).checkValuesCount(p1).isError(" expected 2 values" ));
89
+ assert (info(2 ,2 ).checkValuesCount(p2));
90
+ assert (info(2 ,2 ).checkValuesCount(p3).isError(" expected 2 values" ));
91
+
92
+ assert (info(1 ,1 ).checkValuesCount(p0).isError(" expected 1 value" ));
93
+ assert (info(1 ,1 ).checkValuesCount(p1));
94
+ assert (info(1 ,1 ).checkValuesCount(p2).isError(" expected 1 value" ));
95
+
96
+ assert (info(0 ,1 ).checkValuesCount(p0));
97
+ assert (info(0 ,1 ).checkValuesCount(p1));
98
+ assert (info(0 ,1 ).checkValuesCount(p2).isError(" expected at most 1 value" ));
99
+
100
+ assert (info(1 ,2 ).checkValuesCount(p0).isError(" expected at least 1 value" ));
101
+ assert (info(1 ,2 ).checkValuesCount(p1));
102
+ assert (info(1 ,2 ).checkValuesCount(p2));
103
+ assert (info(1 ,2 ).checkValuesCount(p3).isError(" expected at most 2 values" ));
95
104
}
96
105
97
106
// /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
0 commit comments