Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added in CLI Argument Processing. #1626

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions data/core/common.lua
Original file line number Diff line number Diff line change
Expand Up @@ -707,5 +707,21 @@ function common.rm(path, recursively)
return true
end

-- grabs a flag from the argument list, removing it from the ARGS variable
function common.grab_flag(name, with_value)
for i,v in ipairs(ARGS) do
if v:find("^%-%-" .. name:gsub("%-", "%%-") .. "$") then
local value = ARGS[i+1]
ARGS = { table.unpack(ARGS, 1, i - 1), table.unpack(ARGS, i + (with_value and 2 or 1)) }
return with_value and value
elseif with_value and v:find("^%-%-" .. name .. "=") then
local s, e = v:find("^%-%-" .. name .. "=")
ARGS = { table.unpack(ARGS, 1, i - 1), table.unpack(ARGS, i + 1) }
return v:sub(e + 1)
end
end
return nil
end
function common.grab_arg(name) return common.grab_flag(name, true) end

return common
45 changes: 24 additions & 21 deletions data/core/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -703,27 +703,6 @@ function core.init()
core.previous_replace = session.previous_replace or {}
end

local project_dir = core.recent_projects[1] or "."
local project_dir_explicit = false
local files = {}
for i = 2, #ARGS do
local arg_filename = strip_trailing_slash(ARGS[i])
local info = system.get_file_info(arg_filename) or {}
if info.type == "dir" then
project_dir = arg_filename
project_dir_explicit = true
else
-- on macOS we can get an argument like "-psn_0_52353" that we just ignore.
if not ARGS[i]:match("^-psn") then
local file_abs = core.project_absolute_path(arg_filename)
if file_abs then
table.insert(files, file_abs)
project_dir = file_abs:match("^(.+)[/\\].+$")
end
end
end
end

core.frame_start = 0
core.clip_rect_stack = {{ 0,0,0,0 }}
core.docs = {}
Expand Down Expand Up @@ -763,6 +742,30 @@ function core.init()
-- Load defaiult commands first so plugins can override them
command.add_defaults()


local project_dir = core.recent_projects[1] or "."
local project_dir_explicit = false
local files = {}
for i = 2, #ARGS do
if not ARGS[i]:find("^%-%-") and (ARGS[i]:find(PATHSEP) or system.get_file_info(ARGS[i])) then
local arg_filename = strip_trailing_slash(ARGS[i])
local info = system.get_file_info(arg_filename) or {}
if info.type == "dir" then
project_dir = arg_filename
project_dir_explicit = true
else
-- on macOS we can get an argument like "-psn_0_52353" that we just ignore.
if not ARGS[i]:match("^-psn") then
local file_abs = core.project_absolute_path(arg_filename)
if file_abs then
table.insert(files, file_abs)
project_dir = file_abs:match("^(.+)[/\\].+$")
end
end
end
end
end

-- Load user module, plugins and project module
local got_user_error, got_project_error = not core.load_user_directory()

Expand Down
43 changes: 43 additions & 0 deletions data/plugins/cli.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
-- mod-version:3 -- priority:1
local core = require "core"
local config = require "core.config"
local common = require "core.common"
local command = require "core.command"

local function parse_value(value)
if value == "true" then return true end
if value == "false" then return false end
if value:find("^%d+$") then return tonumber(value) end
return value
end

while true do
local setting = common.grab_arg('config')
if not setting then break end
local s,e = setting:find('=')
if not s then error("can't find value for --config setting") end
local name, value = setting:sub(1, s-1), setting:sub(s+1)
local location, remainder = config, name
while true do
s = remainder:find('%.')
if not s then location[remainder] = parse_value(value) break end
location, remainder = location[remainder:sub(1, s-1)], remainder:sub(s+1)
end
end
while true do
local cmd = common.grab_arg('command')
if not cmd then break end
core.add_thread(function() command.perform(cmd) end)
end

local old_run = core.run
core.run = function()
local t = {}
for i,v in ipairs(ARGS) do
if i > 1 and v:find("^%-%-") then core.error("unrecognized flag " .. v) else table.insert(t, v) end
end
ARGS = t
old_run()
end