Software /
code /
clix
Annotate
clix.lua @ 171:eb7625161d76
clix.lua: Read resouce from config file.
author | Trần H. Trung <xmpp:trần.h.trung@trung.fun> |
---|---|
date | Fri, 07 Jul 2023 22:11:47 +0700 |
parent | 169:4e67d34c1298 |
child | 173:9b36119c9900 |
rev | line source |
---|---|
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 -- |
127
6a27edf256c3
clix: Initialize verse in modern way
Kim Alvefur <zash@zash.se>
parents:
122
diff
changeset
|
8 local verse = require "verse".init "client" |
0 | 9 |
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
|
10 -- 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
|
11 short_opts = { v = "verbose", q = "quiet", t = "to", f = "from", e = "type", |
120 | 12 a = "account", p = "password", r = "resource", o = "presence", c = "chatroom", i = "interactive", |
171
eb7625161d76
clix.lua: Read resouce from config file.
Trần H. Trung <xmpp:trần.h.trung@trung.fun>
parents:
169
diff
changeset
|
13 f = "file" } |
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 |
168
75e8ca131178
Update to handle Prosody module namespacing
Kim Alvefur <zash@zash.se>
parents:
166
diff
changeset
|
15 |
75e8ca131178
Update to handle Prosody module namespacing
Kim Alvefur <zash@zash.se>
parents:
166
diff
changeset
|
16 local commands = "adhoc archive avatar bounce export mirror moderate ping\ |
75e8ca131178
Update to handle Prosody module namespacing
Kim Alvefur <zash@zash.se>
parents:
166
diff
changeset
|
17 presence publish_atom raw receive roster send sendfile sendfilecontent vcard\ |
75e8ca131178
Update to handle Prosody module namespacing
Kim Alvefur <zash@zash.se>
parents:
166
diff
changeset
|
18 version watch_pep" |
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
|
19 if #arg < 1 then |
0 | 20 print("Command Line XMPP, available commands:"); |
168
75e8ca131178
Update to handle Prosody module namespacing
Kim Alvefur <zash@zash.se>
parents:
166
diff
changeset
|
21 for command in commands:gmatch("%S+") do |
75e8ca131178
Update to handle Prosody module namespacing
Kim Alvefur <zash@zash.se>
parents:
166
diff
changeset
|
22 local m = require("clix."..command); |
75e8ca131178
Update to handle Prosody module namespacing
Kim Alvefur <zash@zash.se>
parents:
166
diff
changeset
|
23 io.write("\t", command, ": "); |
75e8ca131178
Update to handle Prosody module namespacing
Kim Alvefur <zash@zash.se>
parents:
166
diff
changeset
|
24 m({ short_help = true }, {}); |
0 | 25 end |
26 return 0; | |
27 end | |
28 | |
29 local accounts = { default = {} }; | |
171
eb7625161d76
clix.lua: Read resouce from config file.
Trần H. Trung <xmpp:trần.h.trung@trung.fun>
parents:
169
diff
changeset
|
30 local current_account, config_file, err; |
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 |
171
eb7625161d76
clix.lua: Read resouce from config file.
Trần H. Trung <xmpp:trần.h.trung@trung.fun>
parents:
169
diff
changeset
|
32 local home_dir = (os.getenv("XDG_CONFIG_HOME") or os.getenv("HOME")); |
eb7625161d76
clix.lua: Read resouce from config file.
Trần H. Trung <xmpp:trần.h.trung@trung.fun>
parents:
169
diff
changeset
|
33 if home_dir then |
eb7625161d76
clix.lua: Read resouce from config file.
Trần H. Trung <xmpp:trần.h.trung@trung.fun>
parents:
169
diff
changeset
|
34 local config_path = (home_dir.."/.config"); |
eb7625161d76
clix.lua: Read resouce from config file.
Trần H. Trung <xmpp:trần.h.trung@trung.fun>
parents:
169
diff
changeset
|
35 config_file, err = io.open(config_path.."/.clixrc") or io.open(config_path.."/.clix"); |
eb7625161d76
clix.lua: Read resouce from config file.
Trần H. Trung <xmpp:trần.h.trung@trung.fun>
parents:
169
diff
changeset
|
36 if not config_file then |
eb7625161d76
clix.lua: Read resouce from config file.
Trần H. Trung <xmpp:trần.h.trung@trung.fun>
parents:
169
diff
changeset
|
37 print("Unable to open config file... looked for "..config_path.."/.clixrc"); |
eb7625161d76
clix.lua: Read resouce from config file.
Trần H. Trung <xmpp:trần.h.trung@trung.fun>
parents:
169
diff
changeset
|
38 if err then print(err); end |
eb7625161d76
clix.lua: Read resouce from config file.
Trần H. Trung <xmpp:trần.h.trung@trung.fun>
parents:
169
diff
changeset
|
39 os.exit(1); |
eb7625161d76
clix.lua: Read resouce from config file.
Trần H. Trung <xmpp:trần.h.trung@trung.fun>
parents:
169
diff
changeset
|
40 end |
eb7625161d76
clix.lua: Read resouce from config file.
Trần H. Trung <xmpp:trần.h.trung@trung.fun>
parents:
169
diff
changeset
|
41 else |
eb7625161d76
clix.lua: Read resouce from config file.
Trần H. Trung <xmpp:trần.h.trung@trung.fun>
parents:
169
diff
changeset
|
42 config_file, err = io.open("/etc/clixrc") or io.open("/etc/clix"); |
eb7625161d76
clix.lua: Read resouce from config file.
Trần H. Trung <xmpp:trần.h.trung@trung.fun>
parents:
169
diff
changeset
|
43 if not config_file then |
eb7625161d76
clix.lua: Read resouce from config file.
Trần H. Trung <xmpp:trần.h.trung@trung.fun>
parents:
169
diff
changeset
|
44 print("Could not find any config file."); |
eb7625161d76
clix.lua: Read resouce from config file.
Trần H. Trung <xmpp:trần.h.trung@trung.fun>
parents:
169
diff
changeset
|
45 if err then print(err); end |
eb7625161d76
clix.lua: Read resouce from config file.
Trần H. Trung <xmpp:trần.h.trung@trung.fun>
parents:
169
diff
changeset
|
46 os.exit(1); |
eb7625161d76
clix.lua: Read resouce from config file.
Trần H. Trung <xmpp:trần.h.trung@trung.fun>
parents:
169
diff
changeset
|
47 end |
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
|
48 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
|
49 |
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
|
50 for line in config_file:lines() do |
0 | 51 line = line:match("^%s*(.-)%s*$"); |
52 if line:match("^%[") then | |
53 current_account = line:match("^%[(.-)%]"); | |
54 accounts[current_account] = {}; | |
55 if not current_account then -- This is the first defined account | |
56 accounts.default = accounts[current_account]; | |
57 end | |
58 elseif current_account then | |
59 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
|
60 if k then |
d4f8fc71d936
clix.lua: Only set config key on valid lines
Matthew Wild <mwild1@gmail.com>
parents:
33
diff
changeset
|
61 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
|
62 end |
0 | 63 end |
64 end | |
65 | |
165
e4391832be1e
clix: Read password from stdin if not available
Kim Alvefur <zash@zash.se>
parents:
131
diff
changeset
|
66 local function read_password() |
e4391832be1e
clix: Read password from stdin if not available
Kim Alvefur <zash@zash.se>
parents:
131
diff
changeset
|
67 io.write("Password: ") |
e4391832be1e
clix: Read password from stdin if not available
Kim Alvefur <zash@zash.se>
parents:
131
diff
changeset
|
68 local stty_ret = os.execute("stty -echo 2>/dev/null"); |
e4391832be1e
clix: Read password from stdin if not available
Kim Alvefur <zash@zash.se>
parents:
131
diff
changeset
|
69 if stty_ret ~= 0 then |
e4391832be1e
clix: Read password from stdin if not available
Kim Alvefur <zash@zash.se>
parents:
131
diff
changeset
|
70 io.write("\027[08m"); -- ANSI 'hidden' text attribute |
e4391832be1e
clix: Read password from stdin if not available
Kim Alvefur <zash@zash.se>
parents:
131
diff
changeset
|
71 end |
e4391832be1e
clix: Read password from stdin if not available
Kim Alvefur <zash@zash.se>
parents:
131
diff
changeset
|
72 local ok, pass = pcall(io.read, "*l"); |
e4391832be1e
clix: Read password from stdin if not available
Kim Alvefur <zash@zash.se>
parents:
131
diff
changeset
|
73 if stty_ret == 0 then |
e4391832be1e
clix: Read password from stdin if not available
Kim Alvefur <zash@zash.se>
parents:
131
diff
changeset
|
74 os.execute("stty sane"); |
e4391832be1e
clix: Read password from stdin if not available
Kim Alvefur <zash@zash.se>
parents:
131
diff
changeset
|
75 else |
e4391832be1e
clix: Read password from stdin if not available
Kim Alvefur <zash@zash.se>
parents:
131
diff
changeset
|
76 io.write("\027[00m"); |
e4391832be1e
clix: Read password from stdin if not available
Kim Alvefur <zash@zash.se>
parents:
131
diff
changeset
|
77 end |
e4391832be1e
clix: Read password from stdin if not available
Kim Alvefur <zash@zash.se>
parents:
131
diff
changeset
|
78 io.write("\n"); |
e4391832be1e
clix: Read password from stdin if not available
Kim Alvefur <zash@zash.se>
parents:
131
diff
changeset
|
79 if ok then return pass; end |
e4391832be1e
clix: Read password from stdin if not available
Kim Alvefur <zash@zash.se>
parents:
131
diff
changeset
|
80 end |
e4391832be1e
clix: Read password from stdin if not available
Kim Alvefur <zash@zash.se>
parents:
131
diff
changeset
|
81 |
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
|
82 function clix_connect(opts, on_connect, plugins) |
0 | 83 local account = accounts[opts.account or "default"]; |
84 if not (account and account.jid) then | |
85 io.stderr:write("The specified account (", opts.account or "default", ") wasn't found in the config file\n"); | |
86 return nil; | |
87 end | |
103
82c21f1d6f46
clix.lua: Print logged warnings by default
Kim Alvefur <zash@zash.se>
parents:
89
diff
changeset
|
88 verse.set_log_handler(io.stderr, opts.quiet and {} or not opts.verbose and {"info", "warn", "error"}); |
89
a8b63fbad807
clix.lua: Create the logger with the name "clix"
Kim Alvefur <zash@zash.se>
parents:
88
diff
changeset
|
89 local conn = verse.new(verse.new_logger("clix")); |
104
730fcde562ce
clix.lua: Add --raw option for printing raw incoming and outgoing data
Kim Alvefur <zash@zash.se>
parents:
103
diff
changeset
|
90 if opts.raw then |
730fcde562ce
clix.lua: Add --raw option for printing raw incoming and outgoing data
Kim Alvefur <zash@zash.se>
parents:
103
diff
changeset
|
91 conn:hook("incoming-raw", print, 1000); |
730fcde562ce
clix.lua: Add --raw option for printing raw incoming and outgoing data
Kim Alvefur <zash@zash.se>
parents:
103
diff
changeset
|
92 conn:hook("outgoing-raw", print, 1000); |
730fcde562ce
clix.lua: Add --raw option for printing raw incoming and outgoing data
Kim Alvefur <zash@zash.se>
parents:
103
diff
changeset
|
93 end |
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
|
94 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
|
95 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
|
96 end |
0 | 97 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
|
98 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
|
99 conn:close(); |
0 | 100 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
|
101 conn:hook("ready", function () |
88
2c2e241e68df
clix.lua: Log "connected as ..." instead of writing it directly to stderr
Kim Alvefur <zash@zash.se>
parents:
87
diff
changeset
|
102 conn:info("Connected as %s", conn.jid); |
21
cdeb02d9546d
clix, clix.send: Support for chatrooms (-c/--chatroom with --nick=somenick)
Matthew Wild <mwild1@gmail.com>
parents:
20
diff
changeset
|
103 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
|
104 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
|
105 end |
17
fa9efbef8a0c
Move presence sending into clix_connect(), and control with -o/--presence
Matthew Wild <mwild1@gmail.com>
parents:
15
diff
changeset
|
106 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
|
107 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
|
108 end |
fa9efbef8a0c
Move presence sending into clix_connect(), and control with -o/--presence
Matthew Wild <mwild1@gmail.com>
parents:
15
diff
changeset
|
109 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
|
110 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
|
111 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
|
112 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
|
113 conn:close(); |
0 | 114 end); |
115 conn:hook("disconnected", function (info) | |
105
a074eaacefe7
clix.lua: Don't break if the disconnected event has no event data, and don't warn if the stream was closed cleanly
Kim Alvefur <zash@zash.se>
parents:
104
diff
changeset
|
116 if info and info.reason and info.reason ~= "stream closed" then |
2
fd77e75c4891
clix: Make more use of Verse's new logging controls
Matthew Wild <mwild1@gmail.com>
parents:
0
diff
changeset
|
117 conn:warn("Disconnecting: %s", tostring(info.reason)); |
0 | 118 end |
119 verse.quit(); | |
120 end); | |
121 -- Optional config parameters | |
122 conn.connect_host = account.address; | |
123 conn.connect_port = account.port; | |
165
e4391832be1e
clix: Read password from stdin if not available
Kim Alvefur <zash@zash.se>
parents:
131
diff
changeset
|
124 |
166
b0c586241224
clix: Support SCRAM hashes stored as hex in config
Kim Alvefur <zash@zash.se>
parents:
165
diff
changeset
|
125 if not account.password and not (account.clientkey and account.serverkey) and opts.interactive then |
165
e4391832be1e
clix: Read password from stdin if not available
Kim Alvefur <zash@zash.se>
parents:
131
diff
changeset
|
126 account.password = read_password() |
e4391832be1e
clix: Read password from stdin if not available
Kim Alvefur <zash@zash.se>
parents:
131
diff
changeset
|
127 end |
166
b0c586241224
clix: Support SCRAM hashes stored as hex in config
Kim Alvefur <zash@zash.se>
parents:
165
diff
changeset
|
128 |
b0c586241224
clix: Support SCRAM hashes stored as hex in config
Kim Alvefur <zash@zash.se>
parents:
165
diff
changeset
|
129 local clientkey, serverkey = account.clientkey, account.serverkey; |
b0c586241224
clix: Support SCRAM hashes stored as hex in config
Kim Alvefur <zash@zash.se>
parents:
165
diff
changeset
|
130 if clientkey and serverkey then |
168
75e8ca131178
Update to handle Prosody module namespacing
Kim Alvefur <zash@zash.se>
parents:
166
diff
changeset
|
131 local hex = require "prosody.util.hex"; |
166
b0c586241224
clix: Support SCRAM hashes stored as hex in config
Kim Alvefur <zash@zash.se>
parents:
165
diff
changeset
|
132 clientkey = hex.from(clientkey); |
b0c586241224
clix: Support SCRAM hashes stored as hex in config
Kim Alvefur <zash@zash.se>
parents:
165
diff
changeset
|
133 serverkey = hex.from(serverkey); |
b0c586241224
clix: Support SCRAM hashes stored as hex in config
Kim Alvefur <zash@zash.se>
parents:
165
diff
changeset
|
134 elseif clientkey or serverkey then |
b0c586241224
clix: Support SCRAM hashes stored as hex in config
Kim Alvefur <zash@zash.se>
parents:
165
diff
changeset
|
135 conn:warn("Only one of 'clientkey' and 'serverkey' available, both reqired for SCRAM") |
b0c586241224
clix: Support SCRAM hashes stored as hex in config
Kim Alvefur <zash@zash.se>
parents:
165
diff
changeset
|
136 clientkey, serverkey = nil, nil; |
b0c586241224
clix: Support SCRAM hashes stored as hex in config
Kim Alvefur <zash@zash.se>
parents:
165
diff
changeset
|
137 end |
13
751db005032e
clix: Allow specifying the resource with -r/--resource
Matthew Wild <mwild1@gmail.com>
parents:
11
diff
changeset
|
138 |
0 | 139 -- Connect! |
166
b0c586241224
clix: Support SCRAM hashes stored as hex in config
Kim Alvefur <zash@zash.se>
parents:
165
diff
changeset
|
140 conn:connect_client(account.jid, account.password, clientkey, serverkey); |
13
751db005032e
clix: Allow specifying the resource with -r/--resource
Matthew Wild <mwild1@gmail.com>
parents:
11
diff
changeset
|
141 if type(opts.resource) == "string" then |
751db005032e
clix: Allow specifying the resource with -r/--resource
Matthew Wild <mwild1@gmail.com>
parents:
11
diff
changeset
|
142 conn.resource = opts.resource; |
171
eb7625161d76
clix.lua: Read resouce from config file.
Trần H. Trung <xmpp:trần.h.trung@trung.fun>
parents:
169
diff
changeset
|
143 elseif account.resource then |
eb7625161d76
clix.lua: Read resouce from config file.
Trần H. Trung <xmpp:trần.h.trung@trung.fun>
parents:
169
diff
changeset
|
144 conn.resource = account.resource; |
13
751db005032e
clix: Allow specifying the resource with -r/--resource
Matthew Wild <mwild1@gmail.com>
parents:
11
diff
changeset
|
145 end |
171
eb7625161d76
clix.lua: Read resouce from config file.
Trần H. Trung <xmpp:trần.h.trung@trung.fun>
parents:
169
diff
changeset
|
146 |
11
a502c905527c
clix: Handle errors (including 'interrupted!') in verse.loop()
Matthew Wild <mwild1@gmail.com>
parents:
8
diff
changeset
|
147 local ok, ret = pcall(verse.loop); |
a502c905527c
clix: Handle errors (including 'interrupted!') in verse.loop()
Matthew Wild <mwild1@gmail.com>
parents:
8
diff
changeset
|
148 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
|
149 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
|
150 return 1; |
a502c905527c
clix: Handle errors (including 'interrupted!') in verse.loop()
Matthew Wild <mwild1@gmail.com>
parents:
8
diff
changeset
|
151 end |
a502c905527c
clix: Handle errors (including 'interrupted!') in verse.loop()
Matthew Wild <mwild1@gmail.com>
parents:
8
diff
changeset
|
152 return err or 0; |
0 | 153 end |
154 | |
155 local opts = {}; | |
156 | |
37
bb7a51aca282
clix.lua: Fix argument handling (thanks uhoreg)
Matthew Wild <mwild1@gmail.com>
parents:
35
diff
changeset
|
157 local command, args_handled_up_to; |
0 | 158 for i, opt in ipairs(arg) do |
131
58d485001448
clix: Use tabs for indentation in command line parsing
Kim Alvefur <zash@zash.se>
parents:
127
diff
changeset
|
159 if opt:match("^%-") and opt ~= "--" then |
58d485001448
clix: Use tabs for indentation in command line parsing
Kim Alvefur <zash@zash.se>
parents:
127
diff
changeset
|
160 local name = opt:match("^%-%-?([^%s=]+)()") |
58d485001448
clix: Use tabs for indentation in command line parsing
Kim Alvefur <zash@zash.se>
parents:
127
diff
changeset
|
161 name = (short_opts[name] or name):gsub("%-+", "_"); |
58d485001448
clix: Use tabs for indentation in command line parsing
Kim Alvefur <zash@zash.se>
parents:
127
diff
changeset
|
162 if name:match("^no_") then |
58d485001448
clix: Use tabs for indentation in command line parsing
Kim Alvefur <zash@zash.se>
parents:
127
diff
changeset
|
163 name = name:sub(4, -1); |
58d485001448
clix: Use tabs for indentation in command line parsing
Kim Alvefur <zash@zash.se>
parents:
127
diff
changeset
|
164 opts[name] = false; |
58d485001448
clix: Use tabs for indentation in command line parsing
Kim Alvefur <zash@zash.se>
parents:
127
diff
changeset
|
165 else |
58d485001448
clix: Use tabs for indentation in command line parsing
Kim Alvefur <zash@zash.se>
parents:
127
diff
changeset
|
166 opts[name] = opt:match("=(.*)$") or true; |
58d485001448
clix: Use tabs for indentation in command line parsing
Kim Alvefur <zash@zash.se>
parents:
127
diff
changeset
|
167 end |
58d485001448
clix: Use tabs for indentation in command line parsing
Kim Alvefur <zash@zash.se>
parents:
127
diff
changeset
|
168 elseif not command then |
58d485001448
clix: Use tabs for indentation in command line parsing
Kim Alvefur <zash@zash.se>
parents:
127
diff
changeset
|
169 command = arg[i]; |
58d485001448
clix: Use tabs for indentation in command line parsing
Kim Alvefur <zash@zash.se>
parents:
127
diff
changeset
|
170 args_handled_up_to = i-1; |
58d485001448
clix: Use tabs for indentation in command line parsing
Kim Alvefur <zash@zash.se>
parents:
127
diff
changeset
|
171 else |
58d485001448
clix: Use tabs for indentation in command line parsing
Kim Alvefur <zash@zash.se>
parents:
127
diff
changeset
|
172 args_handled_up_to = i-1; |
58d485001448
clix: Use tabs for indentation in command line parsing
Kim Alvefur <zash@zash.se>
parents:
127
diff
changeset
|
173 break; |
58d485001448
clix: Use tabs for indentation in command line parsing
Kim Alvefur <zash@zash.se>
parents:
127
diff
changeset
|
174 end |
0 | 175 end |
176 | |
177 -- 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
|
178 for n=((args_handled_up_to > 0) and args_handled_up_to or #arg),1,-1 do |
0 | 179 table.remove(arg, n); |
180 end | |
181 | |
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
|
182 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
|
183 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
|
184 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
|
185 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
|
186 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
|
187 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
|
188 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
|
189 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
|
190 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
|
191 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
|
192 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
|
193 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
|
194 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
|
195 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
|
196 |
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
|
197 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
|
198 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
|
199 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
|
200 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
|
201 |
0 | 202 return m(opts, arg) or 0; |
203 |