diff options
| author | ruki <[email protected]> | 2019-05-25 22:03:58 +0800 |
|---|---|---|
| committer | ruki <[email protected]> | 2019-05-25 22:03:58 +0800 |
| commit | 2cae1ff2dc7f1756a8a831cfde5c6610342f978f (patch) | |
| tree | 397a3b92fd0405dff31e2cb0ea9cd1d02a5c32fe /xmake/core/base/utils.lua | |
| parent | e2b3965574e574fafbd76d89ee6f5d18ca1265cd (diff) | |
add confirm option
Diffstat (limited to 'xmake/core/base/utils.lua')
| -rw-r--r-- | xmake/core/base/utils.lua | 50 |
1 files changed, 50 insertions, 0 deletions
diff --git a/xmake/core/base/utils.lua b/xmake/core/base/utils.lua index bee1a2813..03b2f4c36 100644 --- a/xmake/core/base/utils.lua +++ b/xmake/core/base/utils.lua @@ -216,5 +216,55 @@ function utils.trycall(script, traceback, ...) end, ...) end +-- get confirm result +-- +-- @code +-- if utils.confirm({description = "xmake.lua not found, try generating it", default = true}) then +-- TODO +-- end +-- @endcode +-- +function utils.confirm(opt) + + -- init options + opt = opt or {} + + -- get default + local default = opt.default + if default == nil then + default = false + end + + -- get description + local description = opt.description or "" + + -- get confirm result + local confirm = option.get("yes") or option.get("confirm") + if type(confirm) == "string" then + confirm = confirm:lower() + if confirm == "d" or confirm == "def" then + confirm = default + else + confirm = nil + end + end + + -- get user confirm + if confirm == nil then + + -- show tips + utils.cprint("${bright color.warning}note: ${clear}%s (pass -y or --confirm=y/n/d to skip confirm)?", description) + utils.cprint("please input: %s (y/n)", default and "y" or "n") + + -- get answer + io.flush() + confirm = option.boolean(io.read():trim()) + if type(confirm) ~= "boolean" then + confirm = default + end + end + return confirm +end + -- return module return utils |
