aboutsummaryrefslogtreecommitdiffhomepage
path: root/core/args.lua
diff options
context:
space:
mode:
authorGravatar mitchell <70453897+667e-11@users.noreply.github.com>2010-06-17 17:27:01 -0400
committerGravatar mitchell <70453897+667e-11@users.noreply.github.com>2010-06-17 17:27:01 -0400
commitaf284ee664ab3da7ce6324a46d4d9a59ef1e2362 (patch)
tree2b17a6fdc7ca19546937a433e6dbe00f7d60d855 /core/args.lua
parent8a7566b91ed01957f635d21362b039cb41a76280 (diff)
Added dynamic command line argument handling.
Diffstat (limited to 'core/args.lua')
-rw-r--r--core/args.lua99
1 files changed, 99 insertions, 0 deletions
diff --git a/core/args.lua b/core/args.lua
new file mode 100644
index 00000000..28ecfd4a
--- /dev/null
+++ b/core/args.lua
@@ -0,0 +1,99 @@
+-- Copyright 2007-2010 Mitchell mitchell<att>caladbolg.net. See LICENSE.
+
+--- Processes command line arguments for Textadept.
+module('args', package.seeall)
+
+---
+-- [Local table] Contains registered command line switches.
+-- @type table
+-- @name switches
+local switches = {}
+
+---
+-- Registers a command line switch.
+-- @param switch1 String switch (short version).
+-- @param switch2 String switch (long version).
+-- @param narg The number of expected parameters for the switch.
+-- @param f The Lua function to run when the switch is tripped.
+-- @param description Description of the switch.
+function register(switch1, switch2, narg, f, description)
+ local t = { f, narg, description }
+ switches[switch1] = t
+ switches[switch2] = t
+end
+
+---
+-- Processes command line arguments.
+-- Add command line switches with args.register(). Any unrecognized arguments
+-- are treated as filepaths and opened.
+-- Generates an 'arg_none' event when no args are present.
+-- @see register
+function process()
+ if #arg == 0 then
+ events.emit('arg_none')
+ return
+ end
+ local i = 1
+ while i <= #arg do
+ local switch = switches[arg[i]]
+ if switch then
+ local f, n = unpack(switch)
+ local args = {}
+ for j = i + 1, i + n do args[#args + 1] = arg[j] end
+ f(unpack(args))
+ i = i + n
+ else
+ io.open_file(arg[i])
+ end
+ i = i + 1
+ end
+end
+
+local function show_help()
+ local line = "%s [%d args] -- %s"
+ local help = {}
+ for key, switch in pairs(switches) do
+ help[#help + 1] = line:format(key, unpack(switch, 2))
+ end
+ table.sort(help)
+ gui.dialog('textbox',
+ '--informative-text', 'Command line parameters',
+ '--text', table.concat(help, '\n'),
+ '--button1', 'gtk-ok')
+ events.emit('arg_none')
+end
+register('-h', '--help', 0, show_help, 'Displays this')
+
+-- For Windows, create arg table from single command line string (arg[0]).
+if WIN32 and #arg[0] > 0 then
+ local lpeg = require 'lpeg'
+ local P, C = lpeg.P, lpeg.C
+ local param = P('"') * C((1 - P('"'))^0) * '"' + C((1 - P(' '))^1)
+ local params = lpeg.match(lpeg.Ct(param * (P(' ')^1 * param)^0), arg[0])
+ for i = 1, #params do arg[#arg + 1] = params[i] end
+end
+
+-- For Mac, remove junk parameter if necessary.
+if MAC and arg[1] and arg[1]:find('^%-psn_0') then table.remove(arg, 1) end
+
+-- Set _G._USERHOME.
+local userhome = os.getenv(not WIN32 and 'HOME' or 'USERPROFILE')..'/.textadept'
+for i = 1, #arg do
+ if (arg[i] == '-u' or arg[i] == '--userhome') and arg[i + 1] then
+ userhome = table.remove(arg, i + 1)
+ table.remove(arg, i)
+ break
+ end
+end
+local lfs = require 'lfs'
+if not lfs.attributes(userhome) then lfs.mkdir(userhome) end
+if not lfs.attributes(userhome..'/init.lua') then
+ local f = io.open(userhome..'/init.lua', 'w')
+ if f then
+ f:write("require 'textadept'\n")
+ f:close()
+ end
+end
+_G._USERHOME = userhome
+
+register('-u', '--userhome', 1, function() end, 'Sets alternate _USERHOME')