Skip to content

Commit da2bc7f

Browse files
committed
Fix --key= empty string value consuming next arg in std.getopt
Fixes #10791
1 parent 58d78d5 commit da2bc7f

1 file changed

Lines changed: 13 additions & 2 deletions

File tree

std/getopt.d

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -906,7 +906,7 @@ private bool handleOption(R)(string option, R receiver, ref string[] args,
906906
enum isCallbackWithLessThanTwoParameters =
907907
(is(R == delegate) || is(Target == function)) &&
908908
!is(typeof(receiver("", "")));
909-
if (!isCallbackWithLessThanTwoParameters && !(val.length) && !incremental)
909+
if (!isCallbackWithLessThanTwoParameters && val is null && !incremental)
910910
{
911911
// Eat the next argument too. Check to make sure there's one
912912
// to be eaten first, though.
@@ -1192,7 +1192,7 @@ private bool optMatch(string arg, scope string optPattern, ref string value,
11921192
if (!isLong && !cfg.bundling)
11931193
{
11941194
// argument looks like -ovalue and there's no bundling
1195-
value = arg[1 .. $];
1195+
value = arg.length > 1 ? arg[1 .. $] : null;
11961196
arg = arg[0 .. 1];
11971197
}
11981198
else
@@ -2050,3 +2050,14 @@ void defaultGetoptFormatter(Output)(Output output, string text, Option[] opt, st
20502050
getopt(args, "foo|f", &a);
20512051
assert(a == 1337);
20522052
}
2053+
2054+
// https://github.com/dlang/phobos/issues/10791
2055+
@safe unittest
2056+
{
2057+
string arg1, arg2;
2058+
2059+
auto args = ["prog", "--arg1=", "--arg2=p2"];
2060+
getopt(args, "arg1", &arg1, "arg2", &arg2);
2061+
assert(arg1 == "", "arg1 should be empty string, got: " ~ arg1);
2062+
assert(arg2 == "p2");
2063+
}

0 commit comments

Comments
 (0)