Software /
code /
clix
Comparison
clix.lua @ 33:f1901c9de891
clix.lua: Improvements to argument handling and command detection. In particular options may now precede commands.
author | Matthew Wild <mwild1@gmail.com> |
---|---|
date | Sat, 29 May 2010 21:13:27 +0100 |
parent | 32:cda6a004ff79 |
child | 34:d4f8fc71d936 |
comparison
equal
deleted
inserted
replaced
32:cda6a004ff79 | 33:f1901c9de891 |
---|---|
10 | 10 |
11 -- Global to allow commands to add to it | 11 -- Global to allow commands to add to it |
12 short_opts = { v = "verbose", q = "quiet", t = "to", f = "from", e = "type", | 12 short_opts = { v = "verbose", q = "quiet", t = "to", f = "from", e = "type", |
13 a = "account", p = "password", r = "resource", o = "presence", c = "chatroom" } | 13 a = "account", p = "password", r = "resource", o = "presence", c = "chatroom" } |
14 | 14 |
15 local command = arg[1]; | 15 if #arg < 1 then |
16 | |
17 if not command then | |
18 print("Command Line XMPP, available commands:"); | 16 print("Command Line XMPP, available commands:"); |
19 for module in pairs(package.preload) do | 17 for module in pairs(package.preload) do |
20 if module:match("^clix%.") then | 18 if module:match("^clix%.") then |
21 local m = require(module); | 19 local m = require(module); |
22 io.write("\t", module:gsub("^clix%.", ""), ": "); | 20 io.write("\t", module:gsub("^clix%.", ""), ": "); |
24 end | 22 end |
25 end | 23 end |
26 return 0; | 24 return 0; |
27 end | 25 end |
28 | 26 |
29 local ok, m = pcall(require, "clix."..command); | |
30 if not ok then | |
31 local is_debug; | |
32 for i=1,#arg do | |
33 if arg[i] == "--debug" then | |
34 is_debug = true; break; | |
35 end | |
36 end | |
37 print("Error running command '"..command..(is_debug and "" or "' (run with --debug to see full error)")); | |
38 if is_debug then | |
39 print(m); | |
40 end | |
41 return 1; | |
42 end | |
43 | |
44 if type(m) ~= "function" then | |
45 print(command.." is not a valid command"); | |
46 return 1; | |
47 end | |
48 | |
49 local accounts = { default = {} }; | 27 local accounts = { default = {} }; |
50 local current_account; | 28 local current_account; |
51 | 29 |
52 local config_path = (os.getenv("XDG_CONFIG_HOME") or (os.getenv("HOME").."/.config"); | 30 local config_path = (os.getenv("XDG_CONFIG_HOME") or (os.getenv("HOME").."/.config")); |
53 local config_file, err = io.open(config_path.."/.clixrc") or io.open(config_path.."/.clix"); | 31 local config_file, err = io.open(config_path.."/.clixrc") or io.open(config_path.."/.clix"); |
54 | 32 |
55 if not config_file then | 33 if not config_file then |
56 print("Unable to open config file... looked for "..config_path.."/.clixrc"); | 34 print("Unable to open config file... looked for "..config_path.."/.clixrc"); |
57 if err then print(err); end | 35 if err then print(err); end |
150 -- Remove all the handled args from the arg array | 128 -- Remove all the handled args from the arg array |
151 for n=(args_handled_up_to or #arg),1,-1 do | 129 for n=(args_handled_up_to or #arg),1,-1 do |
152 table.remove(arg, n); | 130 table.remove(arg, n); |
153 end | 131 end |
154 | 132 |
133 local command = arg[1]; | |
134 | |
135 local ok, m = pcall(require, "clix."..command); | |
136 if not ok then | |
137 local is_debug; | |
138 for i=1,#arg do | |
139 if arg[i] == "--debug" then | |
140 is_debug = true; break; | |
141 end | |
142 end | |
143 print("Error running command '"..command..(is_debug and "" or "' (run with --debug to see full error)")); | |
144 if is_debug then | |
145 print(m); | |
146 end | |
147 return 1; | |
148 end | |
149 | |
150 if type(m) ~= "function" then | |
151 print(command.." is not a valid command"); | |
152 return 1; | |
153 end | |
154 | |
155 return m(opts, arg) or 0; | 155 return m(opts, arg) or 0; |
156 | 156 |