diff options
| author | ruki <[email protected]> | 2019-12-10 00:39:34 +0800 |
|---|---|---|
| committer | ruki <[email protected]> | 2019-12-09 21:57:25 +0800 |
| commit | 7380532ecbf4050087fa5b1c8d04b7191176449c (patch) | |
| tree | 1c4d515f9429eb628ecff5e6159898c57ae68fe9 | |
| parent | aec347873556ecb5457ff3cc9447a37b9b4a900f (diff) | |
improve to parse args
| -rw-r--r-- | CHANGELOG.md | 2 | ||||
| -rw-r--r-- | xmake/core/base/option.lua | 80 |
2 files changed, 31 insertions, 51 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md index 01ff9d4e1..78738fa06 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -19,6 +19,7 @@ * [#598](https://github.com/xmake-io/xmake/issues/598): Improve find_package to support .tbd libraries on macOS * [#615](https://github.com/xmake-io/xmake/issues/615): Support to install and use other archs and ios conan packages * [#629](https://github.com/xmake-io/xmake/issues/629): Improve hash.uuid and implement uuid v4 +* [#639](https://github.com/xmake-io/xmake/issues/639): Improve to parse argument options to support -jN ### Bugs fixed @@ -670,6 +671,7 @@ * [#598](https://github.com/xmake-io/xmake/issues/598): 改进`find_package`支持在macOS上对.tbd系统库文件的查找 * [#615](https://github.com/xmake-io/xmake/issues/615): 支持安装和使用其他arch和ios的conan包 * [#629](https://github.com/xmake-io/xmake/issues/629): 改进hash.uuid并且实现uuid v4 +* [#639](https://github.com/xmake-io/xmake/issues/639): 改进参数解析器支持`-jN`风格传参 ### Bugs修复 diff --git a/xmake/core/base/option.lua b/xmake/core/base/option.lua index 029a252af..c6a04487d 100644 --- a/xmake/core/base/option.lua +++ b/xmake/core/base/option.lua @@ -212,6 +212,11 @@ function option.init(menu) if not argkv_end and key:startswith("--") then key = key:sub(3) prefix = 2 + -- -kvalue? + elseif not argkv_end and key:startswith("-") and #key > 2 then + value = key:sub(3) + key = key:sub(2, 2) + prefix = 1 -- -k? elseif not argkv_end and key:startswith("-") then key = key:sub(2) @@ -220,15 +225,11 @@ function option.init(menu) -- check key if prefix and #key == 0 then - - -- print menu option.show_menu(context.taskname) - - -- invalid option return false, "invalid option: " .. arg end - -- --key=value or -k value or -k? + -- --key=value or -kvalue or -k value or -k? if prefix ~= 0 then -- find this option @@ -258,42 +259,26 @@ function option.init(menu) -- not found? if not opt then - - -- print menu option.show_menu(context.taskname) - - -- invalid option return false, "invalid option: " .. arg end - -- -k value? continue to get the value + -- -k value or -kvalue? continue to get the value if prefix == 1 and opt[3] == "kv" then - - -- get the next idx and arg - idx, arg = _iter(_s, _k) - - -- exists value? - _k = idx - if idx == nil or (arg:startswith("-") and not arg:find("%s")) then - - -- print menu - option.show_menu(context.taskname) - - -- invalid option - return false, "invalid option: " .. option._ifelse(idx, arg, key) + if type(value) ~= "string" then + idx, arg = _iter(_s, _k) + _k = idx + if idx == nil or (arg:startswith("-") and not arg:find("%s")) then + option.show_menu(context.taskname) + return false, "invalid option: " .. option._ifelse(idx, arg, key) + end + value = arg end - - -- get value - value = arg end -- check mode if (opt[3] == "k" and type(value) ~= "boolean") or (opt[3] == "kv" and type(value) ~= "string") then - - -- print menu option.show_menu(context.taskname) - - -- invalid option return false, "invalid option: " .. arg end @@ -486,6 +471,11 @@ function option.parse(argv, options) if not argkv_end and key:startswith("--") then key = key:sub(3) prefix = 2 + -- -kvalue? + elseif not argkv_end and key:startswith("-") and #key > 2 then + value = key:sub(3) + key = key:sub(2, 2) + prefix = 1 -- -k? elseif not argkv_end and key:startswith("-") then key = key:sub(2) @@ -494,12 +484,10 @@ function option.parse(argv, options) -- check key if prefix and #key == 0 then - - -- failed return nil, "invalid option: " .. arg end - -- --key=value or -k value or -k? + -- --key=value or -kvalue or -k value or -k? if prefix ~= 0 then -- find this option @@ -529,33 +517,23 @@ function option.parse(argv, options) -- not found? if not opt then - - -- failed return nil, "invalid option: " .. arg end - -- -k value? continue to get the value + -- -k value or -kvalue? continue to get the value if prefix == 1 and opt[3] == "kv" then - - -- get the next idx and arg - idx, arg = _iter(_s, _k) - - -- exists value? - _k = idx - if idx == nil or (arg:startswith("-") and not arg:find("%s")) then - - -- failed - return nil, "invalid option: " .. option._ifelse(idx, arg, key) + if type(value) ~= "string" then + idx, arg = _iter(_s, _k) + _k = idx + if idx == nil or (arg:startswith("-") and not arg:find("%s")) then + return nil, "invalid option: " .. option._ifelse(idx, arg, key) + end + value = arg end - - -- get value - value = arg end -- check mode if (opt[3] == "k" and type(value) ~= "boolean") or (opt[3] == "kv" and type(value) ~= "string") then - - -- failed return nil, "invalid option: " .. arg end |
