aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/luarocks/command_line.lua15
-rw-r--r--src/luarocks/util.lua103
-rw-r--r--src/luarocks/write_rockspec.lua4
-rwxr-xr-xtest/testing.sh8
4 files changed, 106 insertions, 24 deletions
diff --git a/src/luarocks/command_line.lua b/src/luarocks/command_line.lua
index 96d7cb11..9cc34c42 100644
--- a/src/luarocks/command_line.lua
+++ b/src/luarocks/command_line.lua
@@ -64,6 +64,9 @@ function command_line.run_command(...)
64 end 64 end
65 local nonflags = { util.parse_flags(unpack(args)) } 65 local nonflags = { util.parse_flags(unpack(args)) }
66 local flags = table.remove(nonflags, 1) 66 local flags = table.remove(nonflags, 1)
67 if flags.ERROR then
68 die(flags.ERROR.." See --help.")
69 end
67 70
68 if flags["from"] then flags["server"] = flags["from"] end 71 if flags["from"] then flags["server"] = flags["from"] end
69 if flags["only-from"] then flags["only-server"] = flags["only-from"] end 72 if flags["only-from"] then flags["only-server"] = flags["only-from"] end
@@ -126,16 +129,10 @@ function command_line.run_command(...)
126 end 129 end
127 130
128 if flags["branch"] then 131 if flags["branch"] then
129 if flags["branch"] == true or flags["branch"] == "" then
130 die("Argument error: use --branch=<branch-name>")
131 end
132 cfg.branch = flags["branch"] 132 cfg.branch = flags["branch"]
133 end 133 end
134 134
135 if flags["tree"] then 135 if flags["tree"] then
136 if flags["tree"] == true or flags["tree"] == "" then
137 die("Argument error: use --tree=<path>")
138 end
139 local named = false 136 local named = false
140 for _, tree in ipairs(cfg.rocks_trees) do 137 for _, tree in ipairs(cfg.rocks_trees) do
141 if type(tree) == "table" and flags["tree"] == tree.name then 138 if type(tree) == "table" and flags["tree"] == tree.name then
@@ -178,17 +175,11 @@ function command_line.run_command(...)
178 cfg.variables.SCRIPTS_DIR = cfg.deploy_bin_dir 175 cfg.variables.SCRIPTS_DIR = cfg.deploy_bin_dir
179 176
180 if flags["server"] then 177 if flags["server"] then
181 if flags["server"] == true then
182 die("Argument error: use --server=<url>")
183 end
184 local protocol, path = dir.split_url(flags["server"]) 178 local protocol, path = dir.split_url(flags["server"])
185 table.insert(cfg.rocks_servers, 1, protocol.."://"..path) 179 table.insert(cfg.rocks_servers, 1, protocol.."://"..path)
186 end 180 end
187 181
188 if flags["only-server"] then 182 if flags["only-server"] then
189 if flags["only-server"] == true then
190 die("Argument error: use --only-server=<url>")
191 end
192 cfg.rocks_servers = { flags["only-server"] } 183 cfg.rocks_servers = { flags["only-server"] }
193 end 184 end
194 185
diff --git a/src/luarocks/util.lua b/src/luarocks/util.lua
index 40af35bd..6319ae14 100644
--- a/src/luarocks/util.lua
+++ b/src/luarocks/util.lua
@@ -63,6 +63,63 @@ function util.matchquote(s)
63 return (s:gsub("[?%-+*%[%].%%()$^]","%%%1")) 63 return (s:gsub("[?%-+*%[%].%%()$^]","%%%1"))
64end 64end
65 65
66local supported_flags = {
67 ["all"] = true,
68 ["api-key"] = "<key>",
69 ["append"] = true,
70 ["arch"] = "<arch>",
71 ["bin"] = true,
72 ["binary"] = true,
73 ["branch"] = "<branch-name>",
74 ["debug"] = true,
75 ["deps"] = true,
76 ["deps-mode"] = "<mode>",
77 ["detailed"] = "\"<text>\"",
78 ["extensions"] = true,
79 ["force"] = true,
80 ["from"] = "<server>",
81 ["help"] = true,
82 ["home"] = true,
83 ["homepage"] = true,
84 ["keep"] = true,
85 ["lib"] = true,
86 ["license"] = "\"<text>\"",
87 ["list"] = true,
88 ["local"] = true,
89 ["local-tree"] = true,
90 ["lr-bin"] = true,
91 ["lr-cpath"] = true,
92 ["lr-path"] = true,
93 ["lua-version"] = "<vers>",
94 ["modules"] = true,
95 ["mversion"] = true,
96 ["no-refresh"] = true,
97 ["nodeps"] = true,
98 ["old-versions"] = true,
99 ["only-from"] = "<server>",
100 ["only-server"] = "<server>",
101 ["only-sources"] = "<url>",
102 ["only-sources-from"] = "<url>",
103 ["outdated"] = true,
104 ["output"] = "<file>",
105 ["pack-binary-rock"] = true,
106 ["porcelain"] = true,
107 ["quick"] = true,
108 ["rock-dir"] = true,
109 ["rock-tree"] = true,
110 ["rockspec"] = true,
111 ["server"] = "<server>",
112 ["skip-pack"] = true,
113 ["source"] = true,
114 ["summary"] = "\"<text>\"",
115 ["tag"] = "<tag>",
116 ["timeout"] = "<seconds>",
117 ["to"] = "<path>",
118 ["tree"] = "<path>",
119 ["verbose"] = true,
120 ["version"] = true,
121}
122
66--- Extract flags from an arguments list. 123--- Extract flags from an arguments list.
67-- Given string arguments, extract flag arguments into a flags set. 124-- Given string arguments, extract flag arguments into a flags set.
68-- For example, given "foo", "--tux=beep", "--bla", "bar", "--baz", 125-- For example, given "foo", "--tux=beep", "--bla", "bar", "--baz",
@@ -71,19 +128,53 @@ end
71function util.parse_flags(...) 128function util.parse_flags(...)
72 local args = {...} 129 local args = {...}
73 local flags = {} 130 local flags = {}
74 for i = #args, 1, -1 do 131 local i = 1
132 local out = {}
133 local ignore_flags = false
134 while i <= #args do
75 local flag = args[i]:match("^%-%-(.*)") 135 local flag = args[i]:match("^%-%-(.*)")
76 if flag then 136 if flag == "--" then
137 ignore_flags = true
138 end
139 if flag and not ignore_flags then
77 local var,val = flag:match("([a-z_%-]*)=(.*)") 140 local var,val = flag:match("([a-z_%-]*)=(.*)")
78 if val then 141 if val then
79 flags[var] = val 142 local vartype = supported_flags[var]
143 if type(vartype) == "string" then
144 flags[var] = val
145 else
146 if vartype then
147 return { ERROR = "Invalid argument: flag --"..var.." does not take an parameter." }
148 else
149 return { ERROR = "Invalid argument: unknown flag --"..var.."." }
150 end
151 end
80 else 152 else
81 flags[flag] = true 153 local var = flag
154 local vartype = supported_flags[var]
155 if type(vartype) == "string" then
156 i = i + 1
157 local val = args[i]
158 if not val then
159 return { ERROR = "Invalid argument: flag --"..var.."="..vartype.." expects a parameter." }
160 end
161 if val:match("^%-%-.*") then
162 return { ERROR = "Invalid argument: flag --"..var.."="..vartype.." expects a parameter (if you really want to pass "..val.." as an argument to --"..var..", use --"..var.."="..val..")." }
163 else
164 flags[var] = val
165 end
166 elseif vartype == true then
167 flags[var] = true
168 else
169 return { ERROR = "Invalid argument: unknown flag --"..var.."." }
170 end
82 end 171 end
83 table.remove(args, i) 172 else
173 table.insert(out, args[i])
84 end 174 end
175 i = i + 1
85 end 176 end
86 return flags, unpack(args) 177 return flags, unpack(out)
87end 178end
88 179
89--- Build a sequence of flags for forwarding from one command to 180--- Build a sequence of flags for forwarding from one command to
diff --git a/src/luarocks/write_rockspec.lua b/src/luarocks/write_rockspec.lua
index 5b129ab0..fb3a120e 100644
--- a/src/luarocks/write_rockspec.lua
+++ b/src/luarocks/write_rockspec.lua
@@ -209,10 +209,6 @@ function write_rockspec.run(...)
209 elseif not url_or_dir then 209 elseif not url_or_dir then
210 url_or_dir = version 210 url_or_dir = version
211 end 211 end
212
213 if flags["tag"] == true then
214 return nil, "Incorrect usage: --tag requires an argument. "..util.see_help("write_rockspec")
215 end
216 212
217 if flags["tag"] then 213 if flags["tag"] then
218 if not version then 214 if not version then
diff --git a/test/testing.sh b/test/testing.sh
index becaa14f..1c411a98 100755
--- a/test/testing.sh
+++ b/test/testing.sh
@@ -319,10 +319,14 @@ need_luasocket() { need luasocket $verrev_luasocket; }
319 319
320test_version() { $luarocks --version; } 320test_version() { $luarocks --version; }
321 321
322fail_arg_server() { $luarocks --server; }
323fail_arg_only_server() { $luarocks --only-server; }
324fail_unknown_command() { $luarocks unknown_command; } 322fail_unknown_command() { $luarocks unknown_command; }
325 323
324fail_arg_boolean_parameter() { $luarocks --porcelain=invalid; }
325fail_arg_boolean_unknown() { $luarocks --invalid-flag; }
326fail_arg_string_no_parameter() { $luarocks --server; }
327fail_arg_string_followed_by_flag() { $luarocks --server --porcelain; }
328fail_arg_string_unknown() { $luarocks --invalid-flag=abc; }
329
326test_empty_list() { $luarocks list; } 330test_empty_list() { $luarocks list; }
327 331
328fail_bad_sysconfig() { local err=0; local scdir="$testing_lrprefix/etc/luarocks/"; mkdir -p "$scdir"; local sysconfig="$scdir/config.lua"; echo "aoeui" > "$sysconfig"; echo $sysconfig; $luarocks list; err=$?; rm "$sysconfig"; return "$err"; } 332fail_bad_sysconfig() { local err=0; local scdir="$testing_lrprefix/etc/luarocks/"; mkdir -p "$scdir"; local sysconfig="$scdir/config.lua"; echo "aoeui" > "$sysconfig"; echo $sysconfig; $luarocks list; err=$?; rm "$sysconfig"; return "$err"; }