# HG changeset patch # User Matthew Wild # Date 1741717656 0 # Node ID 81856814d74f7d6f304718ed6e0f25a34f282b99 # Parent 8f516d20d2887f0b0ba3b327726131ecbb0cfec1 util.argparse: Fix bug (regression?) in argument parsing with --foo=bar After recent changes, '--foo bar' was working, but '--foo=bar' was not. The test had a typo (?) (bar != baz) and because util.argparse is not strict by default, the typo was not caught. The typo caused the code to take a different path, and bypassed the buggy handling of --foo=bar options. I've preserved the existing test (typo and all!) because it's still an interesting test, and ensures no unintended behaviour changes compared to the old code. However I've added a new variant of the test, with strict mode enabled and the typo fixed. This test failed due to the bug, and this commit introduces a fix. diff -r 8f516d20d288 -r 81856814d74f spec/util_argparse_spec.lua --- a/spec/util_argparse_spec.lua Mon Mar 10 11:54:52 2025 +0000 +++ b/spec/util_argparse_spec.lua Tue Mar 11 18:27:36 2025 +0000 @@ -54,6 +54,12 @@ assert.same({ foo = "bar"; baz = "moo" }, opts); end); + it("supports value arguments in strict mode", function() + local opts, err = parse({ "--foo"; "bar"; "--baz=moo" }, { strict = true, value_params = { foo = true; baz = true } }); + assert.falsy(err); + assert.same({ foo = "bar"; baz = "moo" }, opts); + end); + it("demands values for value params", function() local opts, err, where = parse({ "--foo" }, { value_params = { foo = true } }); assert.falsy(opts); diff -r 8f516d20d288 -r 81856814d74f util/argparse.lua --- a/util/argparse.lua Mon Mar 10 11:54:52 2025 +0000 +++ b/util/argparse.lua Tue Mar 11 18:27:36 2025 +0000 @@ -39,9 +39,13 @@ local param_k, param_v; if value_params[uparam] or array_params[uparam] then - param_k, param_v = uparam, table.remove(arg, 1); + param_k = uparam; + param_v = param:match("^=(.*)$", #uparam+1); if not param_v then - return nil, "missing-value", raw_param; + param_v = table.remove(arg, 1); + if not param_v then + return nil, "missing-value", raw_param; + end end else param_k, param_v = param:match("^([^=]+)=(.+)$");