summaryrefslogtreecommitdiff
path: root/xmake/scripts
diff options
context:
space:
mode:
authorruki <[email protected]>2015-05-03 14:02:49 +0800
committerruki <[email protected]>2015-05-03 14:03:10 +0800
commit4a0ac3ab3b236df85fcad676a2570128a3363f07 (patch)
tree8c9e53cb77198208963d439da2dbb2b9eb61a8a3 /xmake/scripts
parentd5c1749a6f01d05cced27538798989c36b9d18f3 (diff)
parse command line and process help and version
Diffstat (limited to 'xmake/scripts')
-rw-r--r--xmake/scripts/_xmake_main.lua3
-rw-r--r--xmake/scripts/base/main.lua62
-rw-r--r--xmake/scripts/base/option.lua314
-rw-r--r--xmake/scripts/base/utils.lua2
4 files changed, 301 insertions, 80 deletions
diff --git a/xmake/scripts/_xmake_main.lua b/xmake/scripts/_xmake_main.lua
index 210c89726..c407b8fae 100644
--- a/xmake/scripts/_xmake_main.lua
+++ b/xmake/scripts/_xmake_main.lua
@@ -23,8 +23,7 @@
-- init namespace: xmake
xmake = xmake or {}
xmake._ARGV = _ARGV
-xmake._VERBOSE = _VERBOSE
-xmake._VERSION = "1.0.1"
+xmake._VERSION = "XMake v1.0.1"
xmake._PROGRAM_DIR = _PROGRAM_DIR
xmake._PROJECT_DIR = _PROJECT_DIR
xmake._SCRIPTS_DIR = _PROGRAM_DIR .. "/scripts/"
diff --git a/xmake/scripts/base/main.lua b/xmake/scripts/base/main.lua
index 77a3110a5..e790dacda 100644
--- a/xmake/scripts/base/main.lua
+++ b/xmake/scripts/base/main.lua
@@ -31,7 +31,7 @@ local option = require("base/option")
local menu =
{
-- title
- title = "XMake " .. xmake._VERSION .. ", The Automatic Cross-platform Build Tool"
+ title = xmake._VERSION .. ", The Automatic Cross-platform Build Tool"
-- copyright
, copyright = "Copyright (C) 2015-2016 Ruki Wang, tboox.org\nCopyright (C) 2005-2014 Mike Pall, luajit.org"
@@ -144,8 +144,8 @@ local menu =
, {nil, "profile", "k", nil, "Compile for the profiling mode and disable the debugging mode."}
, {}
- , {nil, "output", "kv", "build", "Set the build output directory" }
- , {nil, "packages", "kv", "pkg", "Set the packages directory" }
+ , {'o', "output", "kv", "build", "Set the build output directory" }
+ , {'k', "packages", "kv", "pkg", "Set the packages directory" }
, {}
, {nil, "cc", "kv", nil, "The c compiler" }
@@ -255,14 +255,64 @@ local menu =
}
}
+-- done option
+function main.done_action()
+
+ -- the options
+ local options = xmake._OPTIONS
+ assert(options)
+
+ -- the action
+ local action = options._ACTION or "build"
+
+
+ -- ok
+ return true
+end
+
+-- done option
+function main.done_option()
+
+ -- the options
+ local options = xmake._OPTIONS
+ assert(options)
+
+ -- done help
+ if options.help then
+ option.print_menu(options._ACTION)
+ -- done version
+ elseif options.version then
+ -- print title
+ if option._MENU.title then
+ print(option._MENU.title)
+ end
+
+ -- print copyright
+ if option._MENU.copyright then
+ print(option._MENU.copyright)
+ end
+ -- done action
+ else
+ return main.done_action()
+ end
+
+ -- ok
+ return true
+end
+
-- the main function
function main.done()
- -- done option first
- if not option.done(xmake._ARGV, menu) then
+ -- init option first
+ if not option.init(xmake._ARGV, menu) then
return -1
end
-
+
+ -- done option
+ if not main.done_option() then
+ return -1
+ end
+
-- ok
return 0
end
diff --git a/xmake/scripts/base/option.lua b/xmake/scripts/base/option.lua
index 85de6881c..364b59f57 100644
--- a/xmake/scripts/base/option.lua
+++ b/xmake/scripts/base/option.lua
@@ -26,35 +26,32 @@ local option = {}
-- load modules
local utils = require("base/utils")
--- init _OPTIONS.metatable to always use lowercase keys
-local _OPTIONS_metatable =
-{
- __index = function(table, key)
- -- make lowercase key
- if type(key) == "string" then
- key = key:lower()
- end
- return rawget(table, key)
- end
-, __newindex = function(table, key, value)
- -- make lowercase key
- if type(key) == "string" then
- key = key:lower()
- end
- rawset(table, key, value)
- end
-}
-xmake._OPTIONS = xmake._OPTIONS or {}
-setmetatable(xmake._OPTIONS, _OPTIONS_metatable)
-
--- done the option
-function option.done(argv, menu)
+-- init the option
+function option.init(argv, menu)
-- check
assert(argv and menu)
+ -- the main menu
+ local main = menu.main
+ assert(main)
+
+ -- init _OPTIONS
+ xmake._OPTIONS = {}
+
+ -- save menu
+ option._MENU = menu
+
-- parse _ARGV to _OPTIONS
- for i, arg in ipairs(argv) do
+ local _iter, _s, _k = ipairs(argv)
+ while true do
+
+ -- the idx and arg
+ local idx, arg = _iter(_s, _k)
+
+ -- end?
+ _k = idx
+ if idx == nil then break end
-- parse key and value
local key, value
@@ -70,31 +67,239 @@ function option.done(argv, menu)
value = ""
end
+ -- --key?
+ local prefix = 0
+ if key:startswith("--") then
+ key = key:sub(3)
+ prefix = 2
-- -k?
- if key:startswith("-") then
- xmake._OPTIONS[key:sub(2)] = value
- -- --key=value?
- elseif key:startswith("--") then
- xmake. _OPTIONS[key:sub(3)] = value
+ elseif key:startswith("-") then
+ key = key:sub(2)
+ prefix = 1
+ end
+
+ -- check key
+ if prefix and #key == 0 then
+
+ -- invalid option
+ print("invalid option: " .. arg)
+
+ -- print menu
+ option.print_menu(xmake._OPTIONS._ACTION)
+
+ -- failed
+ return false
+ end
+
+ -- --key=value or -k value or -k?
+ if prefix ~= 0 then
+
+ -- find this option
+ local opt = nil
+ for _, o in ipairs(menu[xmake._OPTIONS._ACTION or "main"].options) do
+
+ -- check
+ assert(o)
+
+ -- --key?
+ if prefix == 2 and key == o[2] then
+ opt = o
+ break
+ -- k?
+ elseif prefix == 1 and key == o[1] then
+ opt = o
+ break
+ end
+ end
+
+ -- not found?
+ if not opt then
+
+ -- invalid option
+ print("invalid option: " .. arg)
+
+ -- print menu
+ option.print_menu(xmake._OPTIONS._ACTION)
+
+ -- failed
+ return false
+ end
+
+ -- -k value? 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("-") then
+
+ -- invalid option
+ print("invalid option: " .. utils.ifelse(idx, arg, key))
+
+ -- print menu
+ option.print_menu(xmake._OPTIONS._ACTION)
+
+ -- failed
+ return false
+ end
+
+ -- get value
+ value = arg
+ end
+
+ -- check mode
+ if (opt[3] == "k" and #value ~= 0) or (opt[3] == "kv" and #value == 0) then
+
+ -- invalid option
+ print("invalid option: " .. arg)
+
+ -- print menu
+ option.print_menu(xmake._OPTIONS._ACTION)
+
+ -- failed
+ return false
+ end
+
+ -- save option
+ xmake._OPTIONS[utils.ifelse(prefix == 1 and opt[2], opt[2], key)] = value
+
+ -- action?
+ elseif idx == 1 then
+
+ -- find this action
+ for _, action in ipairs(main.actions) do
+
+ -- check
+ assert(menu[action])
+
+ -- ok?
+ if action == key or menu[action].shortname == key then
+ -- save this action
+ xmake._OPTIONS._ACTION = action
+ break
+ end
+ end
+
+ -- not found?
+ if not xmake._OPTIONS._ACTION or not menu[xmake._OPTIONS._ACTION] then
+
+ -- invalid action
+ print("invalid action: " .. key)
+
+ -- print the main menu
+ option.print_main()
+
+ -- failed
+ return false
+
+ end
+
+ -- value?
+ else
+
+ -- find a value option with name
+ local opt = nil
+ for _, o in ipairs(menu[xmake._OPTIONS._ACTION or "main"].options) do
+
+ -- check
+ assert(o and (o[3] ~= "v" or o[2]))
+
+ -- is value and with name?
+ if o[3] == "v" and o[2] and not xmake._OPTIONS[o[2]] then
+ opt = o
+ break
+ end
+ end
+
+ -- ok? save this value with name opt[2]
+ if opt then
+ xmake._OPTIONS[opt[2]] = key
+ else
+ -- invalid option
+ print("invalid option: " .. arg)
+
+ -- print menu
+ option.print_menu(xmake._OPTIONS._ACTION)
+
+ -- failed
+ return false
+ end
+
end
end
- -- save menu
- option._MENU = menu
+ -- init the default value
+ for _, o in ipairs(menu[xmake._OPTIONS._ACTION or "main"].options) do
+
+ -- key=value?
+ if o[3] == "kv" then
+
+ -- the key
+ local key = o[2] or o[1]
+ assert(key)
- -- print main menu
- option.print_main()
+ -- exists the default value?
+ if not xmake._OPTIONS[key] and o[4] then
+ -- save the default value
+ xmake._OPTIONS[key] = o[4]
+ end
+ end
+ end
- -- print action menu: create
- option.print_action("create")
- option.print_action("config")
- option.print_action("install")
- option.print_action("clean")
+ -- dump options
+-- for a, b in pairs(xmake._OPTIONS) do
+-- print(a, b)
+-- end
-- ok
return true
end
+-- print the menu
+function option.print_menu(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 title
+ if menu.title then
+ print(menu.title)
+ end
+
+ -- print copyright
+ if menu.copyright then
+ print(menu.copyright)
+ end
+
+ -- 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 main menu
function option.print_main()
@@ -173,39 +378,6 @@ function option.print_main()
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)
diff --git a/xmake/scripts/base/utils.lua b/xmake/scripts/base/utils.lua
index 1c05fd480..59cfc4cd0 100644
--- a/xmake/scripts/base/utils.lua
+++ b/xmake/scripts/base/utils.lua
@@ -30,7 +30,7 @@ end
-- the verbose function
function utils.verbose(msg, ...)
- if xmake._VERBOSE then
+ if xmake._OPTIONS.verbose then
print(string.format(msg, ...))
end
end