diff options
| author | ruki <[email protected]> | 2015-05-02 20:12:39 +0800 |
|---|---|---|
| committer | ruki <[email protected]> | 2015-05-02 20:12:39 +0800 |
| commit | 6d4865a5c2c156ad812ecef9a7973c6a0375e7b4 (patch) | |
| tree | 0d4cba41d3b80c812d0f1cb64dc1045a5b96abb8 /xmake/scripts/base/option.lua | |
| parent | 335840294ec2442d177358cc8e42c036d46401ac (diff) | |
print the menu
Diffstat (limited to 'xmake/scripts/base/option.lua')
| -rw-r--r-- | xmake/scripts/base/option.lua | 205 |
1 files changed, 200 insertions, 5 deletions
diff --git a/xmake/scripts/base/option.lua b/xmake/scripts/base/option.lua index a4d7ce1bf..51c18d701 100644 --- a/xmake/scripts/base/option.lua +++ b/xmake/scripts/base/option.lua @@ -23,6 +23,9 @@ -- define module: option local option = {} +-- load modules +local utils = require("base/utils") + -- init _OPTIONS.metatable to always use lowercase keys local _OPTIONS_metatable = { @@ -45,10 +48,10 @@ xmake._OPTIONS = xmake._OPTIONS or {} setmetatable(xmake._OPTIONS, _OPTIONS_metatable) -- done the option -function option.done(argv) +function option.done(argv, menu) -- check - assert(option._MENU) + assert(argv and menu) -- parse _ARGV to _OPTIONS for i, arg in ipairs(argv) do @@ -76,16 +79,208 @@ function option.done(argv) end end + -- save menu + option._MENU = menu + + -- print main menu + option.print_main() + + -- print action menu: create + option.print_action("create") + option.print_action("config") + option.print_action("install") + option.print_action("clean") + -- ok return true end --- print the help option -function option.help() +-- print the main menu +function option.print_main() + + -- the menu + local menu = option._MENU + assert(menu) + + -- the main menu + local main = menu.main + assert(main) + + -- print title + if menu.title then + print(menu.title) + end + + -- print copyright + if menu.copyright then + print(menu.copyright) + end + + -- print usage + if main.usage then + print("") + print("Usage: " .. main.usage) + end + + -- print description + if main.description then + print("") + print(main.description) + end + + -- print actions + if main.actions then + + -- print header + print("") + print("Actions: ") + + -- the padding spaces + local padding = 32 + + -- print actions + for _, action in ipairs(main.actions) do + + -- the action menu + local action_menu = menu[action] + + -- init the action info + local action_info = " " .. utils.ifelse(action_menu and action_menu.shortname, action_menu.shortname .. ", ", " ") + + -- append the action + action_info = action_info .. action + + if action_menu then + -- append spaces + for i = (#action_info), padding do + action_info = action_info .. " " + end + + -- append the action description + if action_menu.description then + action_info = action_info .. action_menu.description + end + end + + -- print action info + print(action_info) + end + end + + -- print options + if main.options then + option.print_options(main.options) + end +end + +-- print the action menu +function option.print_action(action) + + -- no action? print main menu + if not action then + option.print_main() + end + + -- the menu + local menu = option._MENU + assert(menu) + + -- the action + action = menu[action] + assert(action) + + -- print usage + if action.usage then + print("Usage: " .. action.usage) + end + + -- print description + if action.description then + print("") + print(action.description) + end + + -- print options + if action.options then + option.print_options(action.options) + end +end + +-- print the options menu +function option.print_options(options) -- check - assert(option._MENU) + assert(options) + + -- print header + print("") + print("Options: ") + + -- the padding spaces + local padding = 32 + + -- print options + for _, option in ipairs(options) do + + -- init the option info + local option_info = "" + + -- append the shortname + local shortname = option[1]; + local name = option[2]; + local mode = option[3]; + if shortname then + option_info = option_info .. " -" .. shortname + if mode == "kv" then + option_info = option_info .. " " .. utils.ifelse(name, name:upper(), "XXX") + end + end + + -- append the name + if name then + option_info = option_info .. utils.ifelse(shortname, ", --", " --") .. name + if mode == "kv" then + option_info = option_info .. "=" .. name:upper() + end + elseif mode == "v" then + option_info = option_info .. " ..." + end + + -- append spaces + for i = (#option_info), padding do + option_info = option_info .. " " + end + + -- append the option description + local description = option[5]; + if description then + option_info = option_info .. description + end + -- append the default value + local default = option[4]; + if default then + option_info = option_info .. " (default: " .. default .. ")" + end + + -- print option info + print(option_info) + + -- print more description if exists + for i = 6, #option do + if option[i] then + + -- make spaces + local spaces = "" + for i = 0, padding do + spaces = spaces .. " " + end + + -- print this description + print(spaces .. option[i]) + end + end + end end -- return module: option |
