clix.lua

Wed, 15 Sep 2010 16:53:32 +0100

author
Matthew Wild <mwild1@gmail.com>
date
Wed, 15 Sep 2010 16:53:32 +0100
changeset 43
1ec64b321834
parent 42
4f4fe532a889
child 45
36f5bf718d3c
permissions
-rwxr-xr-x

clix.lua: Filter all log messages except those of the 'error' level unless -v is given

4
ead275885948 clix.lua: Add shebang for people who don't use squish
Matthew Wild <mwild1@gmail.com>
parents: 2
diff changeset
1 #!/usr/bin/env lua
5
7209e1f8e66b clix.lua, COPYING: Add COPYING file with MIT license, add copyright header to clix.lua
Matthew Wild <mwild1@gmail.com>
parents: 4
diff changeset
2 -- Clix -- Command-line XMPP
7209e1f8e66b clix.lua, COPYING: Add COPYING file with MIT license, add copyright header to clix.lua
Matthew Wild <mwild1@gmail.com>
parents: 4
diff changeset
3 -- Copyright (C) 2008-2010 Matthew Wild
7209e1f8e66b clix.lua, COPYING: Add COPYING file with MIT license, add copyright header to clix.lua
Matthew Wild <mwild1@gmail.com>
parents: 4
diff changeset
4 --
7209e1f8e66b clix.lua, COPYING: Add COPYING file with MIT license, add copyright header to clix.lua
Matthew Wild <mwild1@gmail.com>
parents: 4
diff changeset
5 -- This project is MIT/X11 licensed. Please see the
7209e1f8e66b clix.lua, COPYING: Add COPYING file with MIT license, add copyright header to clix.lua
Matthew Wild <mwild1@gmail.com>
parents: 4
diff changeset
6 -- COPYING file in the source package for more information.
7209e1f8e66b clix.lua, COPYING: Add COPYING file with MIT license, add copyright header to clix.lua
Matthew Wild <mwild1@gmail.com>
parents: 4
diff changeset
7 --
0
ae83411a89c9 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8 require "verse"
ae83411a89c9 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
9 require "verse.client"
ae83411a89c9 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
10
8
df4cb4a73549 clix: Make short_opts table global, to allow commands to add their own short options
Matthew Wild <mwild1@gmail.com>
parents: 7
diff changeset
11 -- Global to allow commands to add to it
23
c5f04bdc7c64 clix, clix.raw: Move 'Connected as' to clix_connect(), and suppress on -q/--quiet
Matthew Wild <mwild1@gmail.com>
parents: 21
diff changeset
12 short_opts = { v = "verbose", q = "quiet", t = "to", f = "from", e = "type",
c5f04bdc7c64 clix, clix.raw: Move 'Connected as' to clix_connect(), and suppress on -q/--quiet
Matthew Wild <mwild1@gmail.com>
parents: 21
diff changeset
13 a = "account", p = "password", r = "resource", o = "presence", c = "chatroom" }
8
df4cb4a73549 clix: Make short_opts table global, to allow commands to add their own short options
Matthew Wild <mwild1@gmail.com>
parents: 7
diff changeset
14
33
f1901c9de891 clix.lua: Improvements to argument handling and command detection. In particular options may now precede commands.
Matthew Wild <mwild1@gmail.com>
parents: 32
diff changeset
15 if #arg < 1 then
0
ae83411a89c9 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
16 print("Command Line XMPP, available commands:");
ae83411a89c9 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
17 for module in pairs(package.preload) do
ae83411a89c9 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
18 if module:match("^clix%.") then
ae83411a89c9 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
19 local m = require(module);
28
c2998f70dfd4 clix: Pass short_help option to modules correctly
Matthew Wild <mwild1@gmail.com>
parents: 23
diff changeset
20 io.write("\t", module:gsub("^clix%.", ""), ": ");
c2998f70dfd4 clix: Pass short_help option to modules correctly
Matthew Wild <mwild1@gmail.com>
parents: 23
diff changeset
21 m({ short_help = true }, {});
0
ae83411a89c9 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
22 end
ae83411a89c9 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
23 end
ae83411a89c9 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
24 return 0;
ae83411a89c9 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
25 end
ae83411a89c9 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
26
ae83411a89c9 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
27 local accounts = { default = {} };
ae83411a89c9 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
28 local current_account;
32
cda6a004ff79 clix.lua: Report when config file couldn't be found, change name to .clixrc and comply with fd.o's base directory specifications (now usually ~/.config/.clixrc)
Matthew Wild
parents: 28
diff changeset
29
33
f1901c9de891 clix.lua: Improvements to argument handling and command detection. In particular options may now precede commands.
Matthew Wild <mwild1@gmail.com>
parents: 32
diff changeset
30 local config_path = (os.getenv("XDG_CONFIG_HOME") or (os.getenv("HOME").."/.config"));
32
cda6a004ff79 clix.lua: Report when config file couldn't be found, change name to .clixrc and comply with fd.o's base directory specifications (now usually ~/.config/.clixrc)
Matthew Wild
parents: 28
diff changeset
31 local config_file, err = io.open(config_path.."/.clixrc") or io.open(config_path.."/.clix");
cda6a004ff79 clix.lua: Report when config file couldn't be found, change name to .clixrc and comply with fd.o's base directory specifications (now usually ~/.config/.clixrc)
Matthew Wild
parents: 28
diff changeset
32
cda6a004ff79 clix.lua: Report when config file couldn't be found, change name to .clixrc and comply with fd.o's base directory specifications (now usually ~/.config/.clixrc)
Matthew Wild
parents: 28
diff changeset
33 if not config_file then
cda6a004ff79 clix.lua: Report when config file couldn't be found, change name to .clixrc and comply with fd.o's base directory specifications (now usually ~/.config/.clixrc)
Matthew Wild
parents: 28
diff changeset
34 print("Unable to open config file... looked for "..config_path.."/.clixrc");
cda6a004ff79 clix.lua: Report when config file couldn't be found, change name to .clixrc and comply with fd.o's base directory specifications (now usually ~/.config/.clixrc)
Matthew Wild
parents: 28
diff changeset
35 if err then print(err); end
cda6a004ff79 clix.lua: Report when config file couldn't be found, change name to .clixrc and comply with fd.o's base directory specifications (now usually ~/.config/.clixrc)
Matthew Wild
parents: 28
diff changeset
36 os.exit(1);
cda6a004ff79 clix.lua: Report when config file couldn't be found, change name to .clixrc and comply with fd.o's base directory specifications (now usually ~/.config/.clixrc)
Matthew Wild
parents: 28
diff changeset
37 end
cda6a004ff79 clix.lua: Report when config file couldn't be found, change name to .clixrc and comply with fd.o's base directory specifications (now usually ~/.config/.clixrc)
Matthew Wild
parents: 28
diff changeset
38
cda6a004ff79 clix.lua: Report when config file couldn't be found, change name to .clixrc and comply with fd.o's base directory specifications (now usually ~/.config/.clixrc)
Matthew Wild
parents: 28
diff changeset
39 for line in config_file:lines() do
0
ae83411a89c9 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
40 line = line:match("^%s*(.-)%s*$");
ae83411a89c9 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
41 if line:match("^%[") then
ae83411a89c9 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
42 current_account = line:match("^%[(.-)%]");
ae83411a89c9 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
43 accounts[current_account] = {};
ae83411a89c9 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
44 if not current_account then -- This is the first defined account
ae83411a89c9 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
45 accounts.default = accounts[current_account];
ae83411a89c9 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
46 end
ae83411a89c9 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
47 elseif current_account then
ae83411a89c9 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
48 local k,v = line:match("^(%w+)%s*[:=]%s*(.+)$");
34
d4f8fc71d936 clix.lua: Only set config key on valid lines
Matthew Wild <mwild1@gmail.com>
parents: 33
diff changeset
49 if k then
d4f8fc71d936 clix.lua: Only set config key on valid lines
Matthew Wild <mwild1@gmail.com>
parents: 33
diff changeset
50 accounts[current_account or "default"][k] = v;
d4f8fc71d936 clix.lua: Only set config key on valid lines
Matthew Wild <mwild1@gmail.com>
parents: 33
diff changeset
51 end
0
ae83411a89c9 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
52 end
ae83411a89c9 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
53 end
ae83411a89c9 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
54
42
4f4fe532a889 clix.lua: clix_connect(): Accept a list of plugins to load for the Verse connection
Matthew Wild <mwild1@gmail.com>
parents: 41
diff changeset
55 function clix_connect(opts, on_connect, plugins)
0
ae83411a89c9 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
56 local account = accounts[opts.account or "default"];
ae83411a89c9 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
57 if not (account and account.jid) then
ae83411a89c9 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
58 io.stderr:write("The specified account (", opts.account or "default", ") wasn't found in the config file\n");
ae83411a89c9 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
59 return nil;
ae83411a89c9 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
60 end
43
1ec64b321834 clix.lua: Filter all log messages except those of the 'error' level unless -v is given
Matthew Wild <mwild1@gmail.com>
parents: 42
diff changeset
61 verse.set_logger(opts.verbose and verse.logger() or verse.filter_log({"error"}, verse.logger()));
2
fd77e75c4891 clix: Make more use of Verse's new logging controls
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
62 local conn = verse.new(verse.logger());
42
4f4fe532a889 clix.lua: clix_connect(): Accept a list of plugins to load for the Verse connection
Matthew Wild <mwild1@gmail.com>
parents: 41
diff changeset
63 for _, plugin in ipairs(plugins or {}) do
4f4fe532a889 clix.lua: clix_connect(): Accept a list of plugins to load for the Verse connection
Matthew Wild <mwild1@gmail.com>
parents: 41
diff changeset
64 conn:add_plugin(plugin);
4f4fe532a889 clix.lua: clix_connect(): Accept a list of plugins to load for the Verse connection
Matthew Wild <mwild1@gmail.com>
parents: 41
diff changeset
65 end
2
fd77e75c4891 clix: Make more use of Verse's new logging controls
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
66 conn.log.debug = opts.verbose;
0
ae83411a89c9 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
67 conn:hook("authentication-failure", function (err)
2
fd77e75c4891 clix: Make more use of Verse's new logging controls
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
68 conn:error("Authentication failure ("..(err.condition or "unknown error")..")"..(err.text and (": "..err.text) or ""));
14
1e927484c6ec clix: Close connection after fatal error during login
Matthew Wild <mwild1@gmail.com>
parents: 13
diff changeset
69 conn:close();
0
ae83411a89c9 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
70 end);
35
37540f89d4e2 clix.lua: Hook Verse's new 'ready' event instead of 'binding-success' and switch to 'bind-failure' for hooking resource bind failures
Matthew Wild <mwild1@gmail.com>
parents: 34
diff changeset
71 conn:hook("ready", function ()
23
c5f04bdc7c64 clix, clix.raw: Move 'Connected as' to clix_connect(), and suppress on -q/--quiet
Matthew Wild <mwild1@gmail.com>
parents: 21
diff changeset
72 if not opts.quiet then
c5f04bdc7c64 clix, clix.raw: Move 'Connected as' to clix_connect(), and suppress on -q/--quiet
Matthew Wild <mwild1@gmail.com>
parents: 21
diff changeset
73 io.stderr:write("clix: connected as ", conn.jid, "\n");
c5f04bdc7c64 clix, clix.raw: Move 'Connected as' to clix_connect(), and suppress on -q/--quiet
Matthew Wild <mwild1@gmail.com>
parents: 21
diff changeset
74 end
21
cdeb02d9546d clix, clix.send: Support for chatrooms (-c/--chatroom with --nick=somenick)
Matthew Wild <mwild1@gmail.com>
parents: 20
diff changeset
75 if opts.chatroom then
cdeb02d9546d clix, clix.send: Support for chatrooms (-c/--chatroom with --nick=somenick)
Matthew Wild <mwild1@gmail.com>
parents: 20
diff changeset
76 conn:send(verse.presence{to=opts.to.."/"..(opts.nick or "clix")});
cdeb02d9546d clix, clix.send: Support for chatrooms (-c/--chatroom with --nick=somenick)
Matthew Wild <mwild1@gmail.com>
parents: 20
diff changeset
77 end
17
fa9efbef8a0c Move presence sending into clix_connect(), and control with -o/--presence
Matthew Wild <mwild1@gmail.com>
parents: 15
diff changeset
78 if opts.presence then
fa9efbef8a0c Move presence sending into clix_connect(), and control with -o/--presence
Matthew Wild <mwild1@gmail.com>
parents: 15
diff changeset
79 conn:send(verse.presence());
fa9efbef8a0c Move presence sending into clix_connect(), and control with -o/--presence
Matthew Wild <mwild1@gmail.com>
parents: 15
diff changeset
80 end
fa9efbef8a0c Move presence sending into clix_connect(), and control with -o/--presence
Matthew Wild <mwild1@gmail.com>
parents: 15
diff changeset
81 return on_connect(conn);
fa9efbef8a0c Move presence sending into clix_connect(), and control with -o/--presence
Matthew Wild <mwild1@gmail.com>
parents: 15
diff changeset
82 end);
35
37540f89d4e2 clix.lua: Hook Verse's new 'ready' event instead of 'binding-success' and switch to 'bind-failure' for hooking resource bind failures
Matthew Wild <mwild1@gmail.com>
parents: 34
diff changeset
83 conn:hook("bind-failure", function (err)
2
fd77e75c4891 clix: Make more use of Verse's new logging controls
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
84 conn:error("Resource binding failure ("..(err.condition or "unknown error")..")"..(err.text and (": "..err.text) or ""));
14
1e927484c6ec clix: Close connection after fatal error during login
Matthew Wild <mwild1@gmail.com>
parents: 13
diff changeset
85 conn:close();
0
ae83411a89c9 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
86 end);
ae83411a89c9 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
87 conn:hook("disconnected", function (info)
ae83411a89c9 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
88 if info.reason then
2
fd77e75c4891 clix: Make more use of Verse's new logging controls
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
89 conn:warn("Disconnecting: %s", tostring(info.reason));
0
ae83411a89c9 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
90 end
ae83411a89c9 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
91 verse.quit();
ae83411a89c9 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
92 end);
ae83411a89c9 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
93 -- Optional config parameters
ae83411a89c9 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
94 conn.connect_host = account.address;
ae83411a89c9 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
95 conn.connect_port = account.port;
13
751db005032e clix: Allow specifying the resource with -r/--resource
Matthew Wild <mwild1@gmail.com>
parents: 11
diff changeset
96
0
ae83411a89c9 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
97 -- Connect!
15
54314164a2a3 clix: Allow -p/--password to specify the login password
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
98 conn:connect_client(account.jid, opts.password or account.password);
11
a502c905527c clix: Handle errors (including 'interrupted!') in verse.loop()
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
99
40
c74380af7075 clix.lua: Add workaround for Tigase bug which discards stanzas sent immediately before stream close, use --close-delay=X to delay close for some seconds
Matthew Wild <mwild1@gmail.com>
parents: 38
diff changeset
100 -- COMPAT: Tigase discards stanzas sent at the same time as </stream:stream>
c74380af7075 clix.lua: Add workaround for Tigase bug which discards stanzas sent immediately before stream close, use --close-delay=X to delay close for some seconds
Matthew Wild <mwild1@gmail.com>
parents: 38
diff changeset
101 local _real_close = conn.close;
c74380af7075 clix.lua: Add workaround for Tigase bug which discards stanzas sent immediately before stream close, use --close-delay=X to delay close for some seconds
Matthew Wild <mwild1@gmail.com>
parents: 38
diff changeset
102 function conn:close()
c74380af7075 clix.lua: Add workaround for Tigase bug which discards stanzas sent immediately before stream close, use --close-delay=X to delay close for some seconds
Matthew Wild <mwild1@gmail.com>
parents: 38
diff changeset
103 conn:debug("Delaying close...");
c74380af7075 clix.lua: Add workaround for Tigase bug which discards stanzas sent immediately before stream close, use --close-delay=X to delay close for some seconds
Matthew Wild <mwild1@gmail.com>
parents: 38
diff changeset
104 conn:hook("drained", function ()
c74380af7075 clix.lua: Add workaround for Tigase bug which discards stanzas sent immediately before stream close, use --close-delay=X to delay close for some seconds
Matthew Wild <mwild1@gmail.com>
parents: 38
diff changeset
105 local function do_close()
c74380af7075 clix.lua: Add workaround for Tigase bug which discards stanzas sent immediately before stream close, use --close-delay=X to delay close for some seconds
Matthew Wild <mwild1@gmail.com>
parents: 38
diff changeset
106 if _real_close then
c74380af7075 clix.lua: Add workaround for Tigase bug which discards stanzas sent immediately before stream close, use --close-delay=X to delay close for some seconds
Matthew Wild <mwild1@gmail.com>
parents: 38
diff changeset
107 conn:debug("Closing now...");
c74380af7075 clix.lua: Add workaround for Tigase bug which discards stanzas sent immediately before stream close, use --close-delay=X to delay close for some seconds
Matthew Wild <mwild1@gmail.com>
parents: 38
diff changeset
108 local close = _real_close;
c74380af7075 clix.lua: Add workaround for Tigase bug which discards stanzas sent immediately before stream close, use --close-delay=X to delay close for some seconds
Matthew Wild <mwild1@gmail.com>
parents: 38
diff changeset
109 _real_close = nil;
c74380af7075 clix.lua: Add workaround for Tigase bug which discards stanzas sent immediately before stream close, use --close-delay=X to delay close for some seconds
Matthew Wild <mwild1@gmail.com>
parents: 38
diff changeset
110 close(conn);
c74380af7075 clix.lua: Add workaround for Tigase bug which discards stanzas sent immediately before stream close, use --close-delay=X to delay close for some seconds
Matthew Wild <mwild1@gmail.com>
parents: 38
diff changeset
111 end
c74380af7075 clix.lua: Add workaround for Tigase bug which discards stanzas sent immediately before stream close, use --close-delay=X to delay close for some seconds
Matthew Wild <mwild1@gmail.com>
parents: 38
diff changeset
112 end
c74380af7075 clix.lua: Add workaround for Tigase bug which discards stanzas sent immediately before stream close, use --close-delay=X to delay close for some seconds
Matthew Wild <mwild1@gmail.com>
parents: 38
diff changeset
113 local close_delay = tonumber(opts.close_delay) or 0;
c74380af7075 clix.lua: Add workaround for Tigase bug which discards stanzas sent immediately before stream close, use --close-delay=X to delay close for some seconds
Matthew Wild <mwild1@gmail.com>
parents: 38
diff changeset
114 if close_delay > 0 then
c74380af7075 clix.lua: Add workaround for Tigase bug which discards stanzas sent immediately before stream close, use --close-delay=X to delay close for some seconds
Matthew Wild <mwild1@gmail.com>
parents: 38
diff changeset
115 verse.add_task(close_delay, do_close);
c74380af7075 clix.lua: Add workaround for Tigase bug which discards stanzas sent immediately before stream close, use --close-delay=X to delay close for some seconds
Matthew Wild <mwild1@gmail.com>
parents: 38
diff changeset
116 else
c74380af7075 clix.lua: Add workaround for Tigase bug which discards stanzas sent immediately before stream close, use --close-delay=X to delay close for some seconds
Matthew Wild <mwild1@gmail.com>
parents: 38
diff changeset
117 do_close();
c74380af7075 clix.lua: Add workaround for Tigase bug which discards stanzas sent immediately before stream close, use --close-delay=X to delay close for some seconds
Matthew Wild <mwild1@gmail.com>
parents: 38
diff changeset
118 end
c74380af7075 clix.lua: Add workaround for Tigase bug which discards stanzas sent immediately before stream close, use --close-delay=X to delay close for some seconds
Matthew Wild <mwild1@gmail.com>
parents: 38
diff changeset
119 end);
c74380af7075 clix.lua: Add workaround for Tigase bug which discards stanzas sent immediately before stream close, use --close-delay=X to delay close for some seconds
Matthew Wild <mwild1@gmail.com>
parents: 38
diff changeset
120 end
c74380af7075 clix.lua: Add workaround for Tigase bug which discards stanzas sent immediately before stream close, use --close-delay=X to delay close for some seconds
Matthew Wild <mwild1@gmail.com>
parents: 38
diff changeset
121 -- /COMPAT
c74380af7075 clix.lua: Add workaround for Tigase bug which discards stanzas sent immediately before stream close, use --close-delay=X to delay close for some seconds
Matthew Wild <mwild1@gmail.com>
parents: 38
diff changeset
122
c74380af7075 clix.lua: Add workaround for Tigase bug which discards stanzas sent immediately before stream close, use --close-delay=X to delay close for some seconds
Matthew Wild <mwild1@gmail.com>
parents: 38
diff changeset
123
13
751db005032e clix: Allow specifying the resource with -r/--resource
Matthew Wild <mwild1@gmail.com>
parents: 11
diff changeset
124 if type(opts.resource) == "string" then
751db005032e clix: Allow specifying the resource with -r/--resource
Matthew Wild <mwild1@gmail.com>
parents: 11
diff changeset
125 conn.resource = opts.resource;
751db005032e clix: Allow specifying the resource with -r/--resource
Matthew Wild <mwild1@gmail.com>
parents: 11
diff changeset
126 end
751db005032e clix: Allow specifying the resource with -r/--resource
Matthew Wild <mwild1@gmail.com>
parents: 11
diff changeset
127
11
a502c905527c clix: Handle errors (including 'interrupted!') in verse.loop()
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
128 local ok, ret = pcall(verse.loop);
a502c905527c clix: Handle errors (including 'interrupted!') in verse.loop()
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
129 if not ok and not ret:match("interrupted!$") then
a502c905527c clix: Handle errors (including 'interrupted!') in verse.loop()
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
130 io.stderr:write("Fatal error: ", ret, "\n");
a502c905527c clix: Handle errors (including 'interrupted!') in verse.loop()
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
131 return 1;
a502c905527c clix: Handle errors (including 'interrupted!') in verse.loop()
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
132 end
a502c905527c clix: Handle errors (including 'interrupted!') in verse.loop()
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
133 return err or 0;
0
ae83411a89c9 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
134 end
ae83411a89c9 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
135
ae83411a89c9 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
136 local opts = {};
ae83411a89c9 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
137
37
bb7a51aca282 clix.lua: Fix argument handling (thanks uhoreg)
Matthew Wild <mwild1@gmail.com>
parents: 35
diff changeset
138 local command, args_handled_up_to;
0
ae83411a89c9 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
139 for i, opt in ipairs(arg) do
37
bb7a51aca282 clix.lua: Fix argument handling (thanks uhoreg)
Matthew Wild <mwild1@gmail.com>
parents: 35
diff changeset
140 print(i, opt)
0
ae83411a89c9 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
141 if opt:match("^%-") and opt ~= "--" then
ae83411a89c9 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
142 local name = opt:match("^%-%-?([^%s=]+)()")
ae83411a89c9 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
143 name = (short_opts[name] or name):gsub("%-+", "_");
ae83411a89c9 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
144 if name:match("^no_") then
ae83411a89c9 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
145 name = name:sub(4, -1);
ae83411a89c9 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
146 opts[name] = false;
ae83411a89c9 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
147 else
ae83411a89c9 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
148 opts[name] = opt:match("=(.*)$") or true;
ae83411a89c9 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
149 end
37
bb7a51aca282 clix.lua: Fix argument handling (thanks uhoreg)
Matthew Wild <mwild1@gmail.com>
parents: 35
diff changeset
150 elseif not command then
bb7a51aca282 clix.lua: Fix argument handling (thanks uhoreg)
Matthew Wild <mwild1@gmail.com>
parents: 35
diff changeset
151 command = arg[i];
bb7a51aca282 clix.lua: Fix argument handling (thanks uhoreg)
Matthew Wild <mwild1@gmail.com>
parents: 35
diff changeset
152 args_handled_up_to = i-1;
0
ae83411a89c9 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
153 else
37
bb7a51aca282 clix.lua: Fix argument handling (thanks uhoreg)
Matthew Wild <mwild1@gmail.com>
parents: 35
diff changeset
154 args_handled_up_to = i-1;
bb7a51aca282 clix.lua: Fix argument handling (thanks uhoreg)
Matthew Wild <mwild1@gmail.com>
parents: 35
diff changeset
155 break;
bb7a51aca282 clix.lua: Fix argument handling (thanks uhoreg)
Matthew Wild <mwild1@gmail.com>
parents: 35
diff changeset
156 end
0
ae83411a89c9 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
157 end
ae83411a89c9 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
158
ae83411a89c9 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
159 -- Remove all the handled args from the arg array
38
eb9f706324c7 clix.lua: Correctly remove handled args
Florian Zeitz <florob@babelmonkeys.de>
parents: 37
diff changeset
160 for n=((args_handled_up_to > 0) and args_handled_up_to or #arg),1,-1 do
0
ae83411a89c9 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
161 table.remove(arg, n);
ae83411a89c9 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
162 end
ae83411a89c9 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
163
33
f1901c9de891 clix.lua: Improvements to argument handling and command detection. In particular options may now precede commands.
Matthew Wild <mwild1@gmail.com>
parents: 32
diff changeset
164 local ok, m = pcall(require, "clix."..command);
f1901c9de891 clix.lua: Improvements to argument handling and command detection. In particular options may now precede commands.
Matthew Wild <mwild1@gmail.com>
parents: 32
diff changeset
165 if not ok then
f1901c9de891 clix.lua: Improvements to argument handling and command detection. In particular options may now precede commands.
Matthew Wild <mwild1@gmail.com>
parents: 32
diff changeset
166 local is_debug;
f1901c9de891 clix.lua: Improvements to argument handling and command detection. In particular options may now precede commands.
Matthew Wild <mwild1@gmail.com>
parents: 32
diff changeset
167 for i=1,#arg do
f1901c9de891 clix.lua: Improvements to argument handling and command detection. In particular options may now precede commands.
Matthew Wild <mwild1@gmail.com>
parents: 32
diff changeset
168 if arg[i] == "--debug" then
f1901c9de891 clix.lua: Improvements to argument handling and command detection. In particular options may now precede commands.
Matthew Wild <mwild1@gmail.com>
parents: 32
diff changeset
169 is_debug = true; break;
f1901c9de891 clix.lua: Improvements to argument handling and command detection. In particular options may now precede commands.
Matthew Wild <mwild1@gmail.com>
parents: 32
diff changeset
170 end
f1901c9de891 clix.lua: Improvements to argument handling and command detection. In particular options may now precede commands.
Matthew Wild <mwild1@gmail.com>
parents: 32
diff changeset
171 end
f1901c9de891 clix.lua: Improvements to argument handling and command detection. In particular options may now precede commands.
Matthew Wild <mwild1@gmail.com>
parents: 32
diff changeset
172 print("Error running command '"..command..(is_debug and "" or "' (run with --debug to see full error)"));
f1901c9de891 clix.lua: Improvements to argument handling and command detection. In particular options may now precede commands.
Matthew Wild <mwild1@gmail.com>
parents: 32
diff changeset
173 if is_debug then
f1901c9de891 clix.lua: Improvements to argument handling and command detection. In particular options may now precede commands.
Matthew Wild <mwild1@gmail.com>
parents: 32
diff changeset
174 print(m);
f1901c9de891 clix.lua: Improvements to argument handling and command detection. In particular options may now precede commands.
Matthew Wild <mwild1@gmail.com>
parents: 32
diff changeset
175 end
f1901c9de891 clix.lua: Improvements to argument handling and command detection. In particular options may now precede commands.
Matthew Wild <mwild1@gmail.com>
parents: 32
diff changeset
176 return 1;
f1901c9de891 clix.lua: Improvements to argument handling and command detection. In particular options may now precede commands.
Matthew Wild <mwild1@gmail.com>
parents: 32
diff changeset
177 end
f1901c9de891 clix.lua: Improvements to argument handling and command detection. In particular options may now precede commands.
Matthew Wild <mwild1@gmail.com>
parents: 32
diff changeset
178
f1901c9de891 clix.lua: Improvements to argument handling and command detection. In particular options may now precede commands.
Matthew Wild <mwild1@gmail.com>
parents: 32
diff changeset
179 if type(m) ~= "function" then
f1901c9de891 clix.lua: Improvements to argument handling and command detection. In particular options may now precede commands.
Matthew Wild <mwild1@gmail.com>
parents: 32
diff changeset
180 print(command.." is not a valid command");
f1901c9de891 clix.lua: Improvements to argument handling and command detection. In particular options may now precede commands.
Matthew Wild <mwild1@gmail.com>
parents: 32
diff changeset
181 return 1;
f1901c9de891 clix.lua: Improvements to argument handling and command detection. In particular options may now precede commands.
Matthew Wild <mwild1@gmail.com>
parents: 32
diff changeset
182 end
f1901c9de891 clix.lua: Improvements to argument handling and command detection. In particular options may now precede commands.
Matthew Wild <mwild1@gmail.com>
parents: 32
diff changeset
183
0
ae83411a89c9 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
184 return m(opts, arg) or 0;
ae83411a89c9 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
185

mercurial