From 1e1afb206bf7e37d7394874bd066cdefda20432d Mon Sep 17 00:00:00 2001 From: Adam Harrison Date: Sun, 27 Jun 2021 17:02:15 -0400 Subject: [PATCH] Added in command line parsing in lua. --- data/core/init.lua | 2 +- data/core/start.lua | 38 +++++++++++++++++++++++++++++++++++++- src/main.c | 11 ++++++----- 3 files changed, 44 insertions(+), 7 deletions(-) diff --git a/data/core/init.lua b/data/core/init.lua index a23026b2..2c99fd67 100644 --- a/data/core/init.lua +++ b/data/core/init.lua @@ -423,7 +423,7 @@ local function reload_on_user_module_save() end -function core.init() +function core.init(options) command = require "core.command" keymap = require "core.keymap" RootView = require "core.rootview" diff --git a/data/core/start.lua b/data/core/start.lua index 3ef806cc..5e0ff4bc 100644 --- a/data/core/start.lua +++ b/data/core/start.lua @@ -1,4 +1,4 @@ --- this file is used by lite-xl to setup the Lua environment when starting +-- this file is used by lite-xl to setup the Lua environment when starting, parse command line options, and return the desired core, in a normal flow VERSION = "1.16.11" MOD_VERSION = "1" @@ -19,3 +19,39 @@ package.path = DATADIR .. '/?/init.lua;' .. package.path package.path = USERDIR .. '/?.lua;' .. package.path package.path = USERDIR .. '/?/init.lua;' .. package.path +local function arg_error(error) io.stderr:write(ARGV[1] .. ": " .. error .. "\n") os.exit(-1) end +-- returns a lua table with both numeric and non-numeric keys like getoptlong +local function parse_args_to_dict_and_array(argv, t) + local options, arg = {} + local function parse_value(value) + if t[arg] == "i" and not tonumber(value) then + arg_error("error parsing argument " .. value) + end + options[arg], arg = t[arg] == "i" and tonumber(value) or value, nil + end + for i=2, #argv do + if argv[i]:sub(1,1) == "-" then + local equal, value = argv[i]:find("="), "" + arg = argv[i]:sub(argv[i]:find("[^-]"), equal and equal - 1 or #argv[i]) + if t[arg] == nil then arg_error("unknown argument " .. argv[i]) end + if #t[arg] == 0 or equal then + parse_value(argv[i]:sub((equal or #argv[i])+1)) + end + elseif arg then + parse_value(argv[i]) + else + table.insert(options, argv[i]) + end + end + if arg then arg_error("expected argument for " .. arg) end + return options +end + +ARGS = parse_args_to_dict_and_array(ARGV, { + ["core"] = "s", + ["version"] = "", ["v"] = "" +}); +if ARGS["version"] or ARGS["v"] then print(VERSION) os.exit(0) end + +return require(ARGS["core"] or 'core') + diff --git a/src/main.c b/src/main.c index f84cc3b8..e0fe7420 100644 --- a/src/main.c +++ b/src/main.c @@ -150,7 +150,7 @@ init_lua: lua_pushstring(L, argv[i]); lua_rawseti(L, -2, i + 1); } - lua_setglobal(L, "ARGS"); + lua_setglobal(L, "ARGV"); lua_pushstring(L, SDL_GetPlatform()); lua_setglobal(L, "PLATFORM"); @@ -174,10 +174,11 @@ init_lua: " HOME = os.getenv('" LITE_OS_HOME "')\n" " local exedir = EXEFILE:match(\"^(.*)" LITE_PATHSEP_PATTERN LITE_NONPATHSEP_PATTERN "$\")\n" " local prefix = exedir:match(\"^(.*)" LITE_PATHSEP_PATTERN "bin$\")\n" - " dofile((MACOS_RESOURCES or (prefix and prefix .. '/share/lite-xl' or exedir .. '/data')) .. '/core/start.lua')\n" - " core = require('core')\n" - " core.init()\n" - " core.run()\n" + " core = dofile((MACOS_RESOURCES or (prefix and prefix .. '/share/lite-xl' or exedir .. '/data')) .. '/core/start.lua')\n" + " if core then\n" + " core.init()\n" + " core.run()\n" + " end\n" "end, function(err)\n" " local error_dir\n" " io.stdout:write('Error: '..tostring(err)..'\\n')\n"