Changeset

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
parents 32:cda6a004ff79
children 34:d4f8fc71d936
files clix.lua
diffstat 1 files changed, 24 insertions(+), 24 deletions(-) [+]
line wrap: on
line diff
--- a/clix.lua	Sat May 29 20:58:27 2010 +0100
+++ b/clix.lua	Sat May 29 21:13:27 2010 +0100
@@ -12,9 +12,7 @@
 short_opts = { v = "verbose", q = "quiet", t = "to", f = "from", e = "type",
 	a = "account", p = "password", r = "resource", o = "presence", c = "chatroom" }
 
-local command = arg[1];
-
-if not command then
+if #arg < 1 then
 	print("Command Line XMPP, available commands:");
 	for module in pairs(package.preload) do
 		if module:match("^clix%.") then
@@ -26,30 +24,10 @@
 	return 0;
 end
 
-local ok, m = pcall(require, "clix."..command);
-if not ok then
-	local is_debug;
-	for i=1,#arg do
-		if arg[i] == "--debug" then
-			is_debug = true; break;
-		end
-	end
-	print("Error running command '"..command..(is_debug and "" or "' (run with --debug to see full error)"));
-	if is_debug then
-		print(m);
-	end
-	return 1;
-end
-
-if type(m) ~= "function" then
-	print(command.." is not a valid command");
-	return 1;
-end
-
 local accounts = { default = {} };
 local current_account;
 
-local config_path = (os.getenv("XDG_CONFIG_HOME") or (os.getenv("HOME").."/.config");
+local config_path = (os.getenv("XDG_CONFIG_HOME") or (os.getenv("HOME").."/.config"));
 local config_file, err = io.open(config_path.."/.clixrc") or io.open(config_path.."/.clix");
 
 if not config_file then
@@ -152,5 +130,27 @@
 	table.remove(arg, n);
 end
 
+local command = arg[1];
+
+local ok, m = pcall(require, "clix."..command);
+if not ok then
+	local is_debug;
+	for i=1,#arg do
+		if arg[i] == "--debug" then
+			is_debug = true; break;
+		end
+	end
+	print("Error running command '"..command..(is_debug and "" or "' (run with --debug to see full error)"));
+	if is_debug then
+		print(m);
+	end
+	return 1;
+end
+
+if type(m) ~= "function" then
+	print(command.." is not a valid command");
+	return 1;
+end
+
 return m(opts, arg) or 0;