From d461a2a618aff745cd1864e6248f51817b596ef0 Mon Sep 17 00:00:00 2001 From: Andrey Zherikov Date: Tue, 2 Jan 2024 17:32:48 -0500 Subject: [PATCH] Fix parsing of boolean value (#152) * Fix parsing of boolean value * Review feedback * Update source/argparse/internal/valueparser.d Co-authored-by: Nickolay Bukreyev --------- Co-authored-by: Nickolay Bukreyev (cherry picked from commit a2092cafd05db3de177fa5d6496768e27371d7c6) --- source/argparse/internal/valueparser.d | 28 ++++++++++++++++++-------- 1 file changed, 20 insertions(+), 8 deletions(-) diff --git a/source/argparse/internal/valueparser.d b/source/argparse/internal/valueparser.d index 829a747..a669897 100644 --- a/source/argparse/internal/valueparser.d +++ b/source/argparse/internal/valueparser.d @@ -144,18 +144,24 @@ if(!is(T == void)) else static if(isBoolean!T) { alias DefaultValueParser = ValueParser!( - void, // pre process - void, // pre validate + (ref RawParam param) // pre process + { + import std.algorithm.iteration: map; + import std.array: array; + import std.ascii: toLower; + import std.string: representation; + + // convert values to lower case and replace "" with "y" + foreach(ref value; param.value) + value = value.length == 0 ? "y" : value.representation.map!(_ => immutable char(_.toLower)).array; + }, + ValueInList!(["true","yes","y","false","no","n"], typeof(RawParam.value)), // pre validate (string value) // parse { switch(value) { - case "": goto case; - case "yes": goto case; - case "y": return true; - case "no": goto case; - case "n": return false; - default: return value.to!T; + case "true", "yes", "y": return true; + default: return false; } }, void, // validate @@ -346,11 +352,17 @@ unittest assert(test!bool([]) == true); assert(test!bool([""]) == true); assert(test!bool(["yes"]) == true); + assert(test!bool(["Yes"]) == true); assert(test!bool(["y"]) == true); + assert(test!bool(["Y"]) == true); assert(test!bool(["true"]) == true); + assert(test!bool(["True"]) == true); assert(test!bool(["no"]) == false); + assert(test!bool(["No"]) == false); assert(test!bool(["n"]) == false); + assert(test!bool(["N"]) == false); assert(test!bool(["false"]) == false); + assert(test!bool(["False"]) == false); assert(test!MyEnum(["foo"]) == MyEnum.foo); assert(test!MyEnum(["bar"]) == MyEnum.bar); assert(test!(MyEnum[])(["bar","foo"]) == [MyEnum.bar, MyEnum.foo]);