Mercurial > prosody-hg
comparison util/argparse.lua @ 13898:aac30c5ee334
util.argparse: Rename kv_params to bool_params, remove support for arbitrary values
We have value_params now, which has overlapping behaviour and support
for '--foo bar' as well as '--foo=bar' (only the latter syntax is supported by
kv_params).
The remaining unique property of kv_params is that when used without a value,
i.e. '--foo' then it defaults to boolean true, and when used as '--no-foo' it
defaults to boolean false. This is a useful thing that value_params don't do.
So this commit officially renames kv_params to bool_params (kv_params is still
allowed for now, for some backwards compatibility).
Breaking change though: if any kv_params expected a value, it will no longer
work (--foo=bar will not work). In this case the param should be moved to
value_params.
| author | Matthew Wild <mwild1@gmail.com> |
|---|---|
| date | Thu, 19 Jun 2025 11:19:13 +0100 |
| parents | 81856814d74f |
| children |
comparison
equal
deleted
inserted
replaced
| 13897:6d067ec8ec4a | 13898:aac30c5ee334 |
|---|---|
| 1 local function parse(arg, config) | 1 local function parse(arg, config) |
| 2 local short_params = config and config.short_params or {}; | 2 local short_params = config and config.short_params or {}; |
| 3 local value_params = config and config.value_params or {}; | 3 local value_params = config and config.value_params or {}; |
| 4 local array_params = config and config.array_params or {}; | 4 local array_params = config and config.array_params or {}; |
| 5 local kv_params = config and config.kv_params or {}; | 5 local bool_params = config and (config.bool_params or config.kv_params) or {}; -- COMPAT: kv_params in 13.0 and earlier |
| 6 local strict = config and config.strict; | 6 local strict = config and config.strict; |
| 7 local stop_on_positional = not config or config.stop_on_positional ~= false; | 7 local stop_on_positional = not config or config.stop_on_positional ~= false; |
| 8 | 8 |
| 9 local parsed_opts = {}; | 9 local parsed_opts = {}; |
| 10 | 10 |
| 46 if not param_v then | 46 if not param_v then |
| 47 return nil, "missing-value", raw_param; | 47 return nil, "missing-value", raw_param; |
| 48 end | 48 end |
| 49 end | 49 end |
| 50 else | 50 else |
| 51 param_k, param_v = param:match("^([^=]+)=(.+)$"); | 51 if param:match("^no%-") then |
| 52 if not param_k then | 52 param_k, param_v = param:sub(4), false; |
| 53 if param:match("^no%-") then | 53 else |
| 54 param_k, param_v = param:sub(4), false; | 54 param_k, param_v = param, true; |
| 55 else | |
| 56 param_k, param_v = param, true; | |
| 57 end | |
| 58 end | 55 end |
| 59 param_k = param_k:gsub("%-", "_"); | 56 param_k = param_k:gsub("%-", "_"); |
| 60 if strict and not kv_params[param_k] then | 57 if strict and not bool_params[param_k] then |
| 61 return nil, "param-not-found", raw_param; | 58 return nil, "param-not-found", raw_param; |
| 62 end | 59 end |
| 63 end | 60 end |
| 64 if array_params[uparam] then | 61 if array_params[uparam] then |
| 65 if parsed_opts[param_k] then | 62 if parsed_opts[param_k] then |
