aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPeter Melnichenko <mpeterval@gmail.com>2016-06-03 11:16:55 +0300
committerPeter Melnichenko <mpeterval@gmail.com>2016-06-03 11:58:47 +0300
commitd55041dbc477000d0aa8a1585824958b0ade66a1 (patch)
tree99e2d28a30da52973fbc9caf2817f5fdf5c5fe19
parentd524b515c67ceded00a15c2617283f81a2f21f7c (diff)
downloadluarocks-d55041dbc477000d0aa8a1585824958b0ade66a1.tar.gz
luarocks-d55041dbc477000d0aa8a1585824958b0ade66a1.tar.bz2
luarocks-d55041dbc477000d0aa8a1585824958b0ade66a1.zip
Refactor CLI to avoid double args parsing
New command module interface: instead of 'run' function they must have 'command' function that accepts flags table and other arguments. For compatibility a new util function is called on all command modules: it adds 'run' function that parses command-line args before passing them to 'command'.
-rw-r--r--src/luarocks/add.lua6
-rw-r--r--src/luarocks/admin_remove.lua6
-rw-r--r--src/luarocks/build.lua4
-rw-r--r--src/luarocks/command_line.lua37
-rw-r--r--src/luarocks/config_cmd.lua5
-rw-r--r--src/luarocks/doc.lua4
-rw-r--r--src/luarocks/download.lua5
-rw-r--r--src/luarocks/help.lua5
-rw-r--r--src/luarocks/install.lua4
-rw-r--r--src/luarocks/lint.lua5
-rw-r--r--src/luarocks/list.lua4
-rw-r--r--src/luarocks/make.lua4
-rw-r--r--src/luarocks/make_manifest.lua5
-rw-r--r--src/luarocks/new_version.lua4
-rw-r--r--src/luarocks/pack.lua4
-rw-r--r--src/luarocks/path_cmd.lua4
-rw-r--r--src/luarocks/purge.lua5
-rw-r--r--src/luarocks/refresh_cache.lua4
-rw-r--r--src/luarocks/remove.lua5
-rw-r--r--src/luarocks/search.lua5
-rw-r--r--src/luarocks/show.lua5
-rw-r--r--src/luarocks/unpack.lua5
-rw-r--r--src/luarocks/upload.lua4
-rw-r--r--src/luarocks/util.lua7
-rw-r--r--src/luarocks/validate.lua4
-rw-r--r--src/luarocks/write_rockspec.lua5
26 files changed, 64 insertions, 91 deletions
diff --git a/src/luarocks/add.lua b/src/luarocks/add.lua
index bea2d861..f37d334d 100644
--- a/src/luarocks/add.lua
+++ b/src/luarocks/add.lua
@@ -12,6 +12,7 @@ local index = require("luarocks.index")
12local fs = require("luarocks.fs") 12local fs = require("luarocks.fs")
13local cache = require("luarocks.cache") 13local cache = require("luarocks.cache")
14 14
15util.add_run_function(add)
15add.help_summary = "Add a rock or rockspec to a rocks server." 16add.help_summary = "Add a rock or rockspec to a rocks server."
16add.help_arguments = "[--server=<server>] [--no-refresh] {<rockspec>|<rock>...}" 17add.help_arguments = "[--server=<server>] [--no-refresh] {<rockspec>|<rock>...}"
17add.help = [[ 18add.help = [[
@@ -107,9 +108,8 @@ local function add_files_to_server(refresh, rockfiles, server, upload_server)
107 return true 108 return true
108end 109end
109 110
110function add.run(...) 111function add.command(flags, ...)
111 local files = { util.parse_flags(...) } 112 local files = {...}
112 local flags = table.remove(files, 1)
113 if #files < 1 then 113 if #files < 1 then
114 return nil, "Argument missing. "..util.see_help("add", "luarocks-admin") 114 return nil, "Argument missing. "..util.see_help("add", "luarocks-admin")
115 end 115 end
diff --git a/src/luarocks/admin_remove.lua b/src/luarocks/admin_remove.lua
index 59ca4975..621f1317 100644
--- a/src/luarocks/admin_remove.lua
+++ b/src/luarocks/admin_remove.lua
@@ -12,6 +12,7 @@ local index = require("luarocks.index")
12local fs = require("luarocks.fs") 12local fs = require("luarocks.fs")
13local cache = require("luarocks.cache") 13local cache = require("luarocks.cache")
14 14
15util.add_run_function(admin_remove)
15admin_remove.help_summary = "Remove a rock or rockspec from a rocks server." 16admin_remove.help_summary = "Remove a rock or rockspec from a rocks server."
16admin_remove.help_arguments = "[--server=<server>] [--no-refresh] {<rockspec>|<rock>...}" 17admin_remove.help_arguments = "[--server=<server>] [--no-refresh] {<rockspec>|<rock>...}"
17admin_remove.help = [[ 18admin_remove.help = [[
@@ -77,9 +78,8 @@ local function remove_files_from_server(refresh, rockfiles, server, upload_serve
77 return true 78 return true
78end 79end
79 80
80function admin_remove.run(...) 81function admin_remove.command(flags, ...)
81 local files = { util.parse_flags(...) } 82 local files = {...}
82 local flags = table.remove(files, 1)
83 if #files < 1 then 83 if #files < 1 then
84 return nil, "Argument missing. "..util.see_help("remove", "luarocks-admin") 84 return nil, "Argument missing. "..util.see_help("remove", "luarocks-admin")
85 end 85 end
diff --git a/src/luarocks/build.lua b/src/luarocks/build.lua
index edf4efb4..0e72ca85 100644
--- a/src/luarocks/build.lua
+++ b/src/luarocks/build.lua
@@ -16,6 +16,7 @@ local manif = require("luarocks.manif")
16local remove = require("luarocks.remove") 16local remove = require("luarocks.remove")
17local cfg = require("luarocks.cfg") 17local cfg = require("luarocks.cfg")
18 18
19util.add_run_function(build)
19build.help_summary = "Build/compile a rock." 20build.help_summary = "Build/compile a rock."
20build.help_arguments = "[--pack-binary-rock] [--keep] {<rockspec>|<rock>|<name> [<version>]}" 21build.help_arguments = "[--pack-binary-rock] [--keep] {<rockspec>|<rock>|<name> [<version>]}"
21build.help = [[ 22build.help = [[
@@ -390,8 +391,7 @@ end
390-- also be given. 391-- also be given.
391-- @return boolean or (nil, string, exitcode): True if build was successful; nil and an 392-- @return boolean or (nil, string, exitcode): True if build was successful; nil and an
392-- error message otherwise. exitcode is optionally returned. 393-- error message otherwise. exitcode is optionally returned.
393function build.run(...) 394function build.command(flags, name, version)
394 local flags, name, version = util.parse_flags(...)
395 if type(name) ~= "string" then 395 if type(name) ~= "string" then
396 return nil, "Argument missing. "..util.see_help("build") 396 return nil, "Argument missing. "..util.see_help("build")
397 end 397 end
diff --git a/src/luarocks/command_line.lua b/src/luarocks/command_line.lua
index b3284534..1a8c0fe7 100644
--- a/src/luarocks/command_line.lua
+++ b/src/luarocks/command_line.lua
@@ -27,20 +27,9 @@ local function die(message, exitcode)
27 os.exit(exitcode or cfg.errorcodes.UNSPECIFIED) 27 os.exit(exitcode or cfg.errorcodes.UNSPECIFIED)
28end 28end
29 29
30local function replace_tree(flags, args, tree) 30local function replace_tree(flags, tree)
31 tree = dir.normalize(tree) 31 tree = dir.normalize(tree)
32 flags["tree"] = tree 32 flags["tree"] = tree
33 local added = false
34 for i = 1, #args do
35 if args[i]:match("%-%-tree=") then
36 args[i] = "--tree="..tree
37 added = true
38 break
39 end
40 end
41 if not added then
42 args[#args + 1] = "--tree="..tree
43 end
44 path.use_tree(tree) 33 path.use_tree(tree)
45end 34end
46 35
@@ -78,7 +67,6 @@ function command_line.run_command(...)
78 if flags["to"] then flags["tree"] = flags["to"] end 67 if flags["to"] then flags["tree"] = flags["to"] end
79 if flags["nodeps"] then 68 if flags["nodeps"] then
80 flags["deps-mode"] = "none" 69 flags["deps-mode"] = "none"
81 table.insert(args, "--deps-mode=none")
82 end 70 end
83 71
84 cfg.flags = flags 72 cfg.flags = flags
@@ -106,15 +94,8 @@ function command_line.run_command(...)
106 os.exit(cfg.errorcodes.OK) 94 os.exit(cfg.errorcodes.OK)
107 elseif flags["help"] or #nonflags == 0 then 95 elseif flags["help"] or #nonflags == 0 then
108 command = "help" 96 command = "help"
109 args = nonflags
110 else 97 else
111 command = nonflags[1] 98 command = table.remove(nonflags, 1)
112 for i, arg in ipairs(args) do
113 if arg == command then
114 table.remove(args, i)
115 break
116 end
117 end
118 end 99 end
119 command = command:gsub("-", "_") 100 command = command:gsub("-", "_")
120 101
@@ -137,14 +118,14 @@ function command_line.run_command(...)
137 if not tree.root then 118 if not tree.root then
138 die("Configuration error: tree '"..tree.name.."' has no 'root' field.") 119 die("Configuration error: tree '"..tree.name.."' has no 'root' field.")
139 end 120 end
140 replace_tree(flags, args, tree.root) 121 replace_tree(flags, tree.root)
141 named = true 122 named = true
142 break 123 break
143 end 124 end
144 end 125 end
145 if not named then 126 if not named then
146 local root_dir = fs.absolute_name(flags["tree"]) 127 local root_dir = fs.absolute_name(flags["tree"])
147 replace_tree(flags, args, root_dir) 128 replace_tree(flags, root_dir)
148 end 129 end
149 elseif flags["local"] then 130 elseif flags["local"] then
150 if not cfg.home_tree then 131 if not cfg.home_tree then
@@ -152,7 +133,7 @@ function command_line.run_command(...)
152 "You are running as a superuser, which is intended for system-wide operation.\n".. 133 "You are running as a superuser, which is intended for system-wide operation.\n"..
153 "To force using the superuser's home, use --tree explicitly.") 134 "To force using the superuser's home, use --tree explicitly.")
154 end 135 end
155 replace_tree(flags, args, cfg.home_tree) 136 replace_tree(flags, cfg.home_tree)
156 else 137 else
157 local trees = cfg.rocks_trees 138 local trees = cfg.rocks_trees
158 path.use_tree(trees[#trees]) 139 path.use_tree(trees[#trees])
@@ -195,14 +176,8 @@ function command_line.run_command(...)
195 end 176 end
196 177
197 if commands[command] then 178 if commands[command] then
198 -- TODO the interface of run should be modified, to receive the
199 -- flags table and the (possibly unpacked) nonflags arguments.
200 -- This would remove redundant parsing of arguments.
201 -- I'm not changing this now to avoid messing with the run()
202 -- interface, which I know some people use (even though
203 -- I never published it as a public API...)
204 local cmd = require(commands[command]) 179 local cmd = require(commands[command])
205 local xp, ok, err, exitcode = xpcall(function() return cmd.run(unpack(args)) end, function(err) 180 local xp, ok, err, exitcode = xpcall(function() return cmd.command(flags, unpack(nonflags)) end, function(err)
206 die(debug.traceback("LuaRocks "..cfg.program_version 181 die(debug.traceback("LuaRocks "..cfg.program_version
207 .." bug (please report at https://github.com/keplerproject/luarocks/issues).\n" 182 .." bug (please report at https://github.com/keplerproject/luarocks/issues).\n"
208 ..err, 2), cfg.errorcodes.CRASH) 183 ..err, 2), cfg.errorcodes.CRASH)
diff --git a/src/luarocks/config_cmd.lua b/src/luarocks/config_cmd.lua
index bf282a7a..fe3cc637 100644
--- a/src/luarocks/config_cmd.lua
+++ b/src/luarocks/config_cmd.lua
@@ -6,6 +6,7 @@ local cfg = require("luarocks.cfg")
6local util = require("luarocks.util") 6local util = require("luarocks.util")
7local dir = require("luarocks.dir") 7local dir = require("luarocks.dir")
8 8
9util.add_run_function(config_cmd)
9config_cmd.help_summary = "Query information about the LuaRocks configuration." 10config_cmd.help_summary = "Query information about the LuaRocks configuration."
10config_cmd.help_arguments = "<flag>" 11config_cmd.help_arguments = "<flag>"
11config_cmd.help = [[ 12config_cmd.help = [[
@@ -33,9 +34,7 @@ end
33 34
34--- Driver function for "config" command. 35--- Driver function for "config" command.
35-- @return boolean: True if succeeded, nil on errors. 36-- @return boolean: True if succeeded, nil on errors.
36function config_cmd.run(...) 37function config_cmd.command(flags)
37 local flags = util.parse_flags(...)
38
39 if flags["lua-incdir"] then 38 if flags["lua-incdir"] then
40 print(cfg.variables.LUA_INCDIR) 39 print(cfg.variables.LUA_INCDIR)
41 return true 40 return true
diff --git a/src/luarocks/doc.lua b/src/luarocks/doc.lua
index 62e80232..ec2b1110 100644
--- a/src/luarocks/doc.lua
+++ b/src/luarocks/doc.lua
@@ -12,6 +12,7 @@ local fetch = require("luarocks.fetch")
12local fs = require("luarocks.fs") 12local fs = require("luarocks.fs")
13local download = require("luarocks.download") 13local download = require("luarocks.download")
14 14
15util.add_run_function(doc)
15doc.help_summary = "Show documentation for an installed rock." 16doc.help_summary = "Show documentation for an installed rock."
16 17
17doc.help = [[ 18doc.help = [[
@@ -57,8 +58,7 @@ end
57-- @param name or nil: an existing package name. 58-- @param name or nil: an existing package name.
58-- @param version string or nil: a version may also be passed. 59-- @param version string or nil: a version may also be passed.
59-- @return boolean: True if succeeded, nil on errors. 60-- @return boolean: True if succeeded, nil on errors.
60function doc.run(...) 61function doc.command(flags, name, version)
61 local flags, name, version = util.parse_flags(...)
62 if not name then 62 if not name then
63 return nil, "Argument missing. "..util.see_help("doc") 63 return nil, "Argument missing. "..util.see_help("doc")
64 end 64 end
diff --git a/src/luarocks/download.lua b/src/luarocks/download.lua
index 181aca75..2e434b03 100644
--- a/src/luarocks/download.lua
+++ b/src/luarocks/download.lua
@@ -12,6 +12,7 @@ local fs = require("luarocks.fs")
12local dir = require("luarocks.dir") 12local dir = require("luarocks.dir")
13local cfg = require("luarocks.cfg") 13local cfg = require("luarocks.cfg")
14 14
15util.add_run_function(download)
15download.help_summary = "Download a specific rock file from a rocks server." 16download.help_summary = "Download a specific rock file from a rocks server."
16download.help_arguments = "[--all] [--arch=<arch> | --source | --rockspec] [<name> [<version>]]" 17download.help_arguments = "[--all] [--arch=<arch> | --source | --rockspec] [<name> [<version>]]"
17 18
@@ -84,9 +85,7 @@ end
84-- version may also be passed. 85-- version may also be passed.
85-- @return boolean or (nil, string): true if successful or nil followed 86-- @return boolean or (nil, string): true if successful or nil followed
86-- by an error message. 87-- by an error message.
87function download.run(...) 88function download.command(flags, name, version)
88 local flags, name, version = util.parse_flags(...)
89
90 assert(type(version) == "string" or not version) 89 assert(type(version) == "string" or not version)
91 if type(name) ~= "string" and not flags["all"] then 90 if type(name) ~= "string" and not flags["all"] then
92 return nil, "Argument missing, see help." 91 return nil, "Argument missing, see help."
diff --git a/src/luarocks/help.lua b/src/luarocks/help.lua
index 0f66f64b..28f97702 100644
--- a/src/luarocks/help.lua
+++ b/src/luarocks/help.lua
@@ -12,6 +12,7 @@ local dir = require("luarocks.dir")
12 12
13local program = util.this_program("luarocks") 13local program = util.this_program("luarocks")
14 14
15util.add_run_function(help)
15help.help_summary = "Help on commands. Type '"..program.." help <command>' for more." 16help.help_summary = "Help on commands. Type '"..program.." help <command>' for more."
16 17
17help.help_arguments = "[<command>]" 18help.help_arguments = "[<command>]"
@@ -40,9 +41,7 @@ end
40-- given, help summaries for all commands are shown. 41-- given, help summaries for all commands are shown.
41-- @return boolean or (nil, string): true if there were no errors 42-- @return boolean or (nil, string): true if there were no errors
42-- or nil and an error message if an invalid command was requested. 43-- or nil and an error message if an invalid command was requested.
43function help.run(...) 44function help.command(flags, command)
44 local flags, command = util.parse_flags(...)
45
46 if not command then 45 if not command then
47 local conf = cfg.which_config() 46 local conf = cfg.which_config()
48 print_banner() 47 print_banner()
diff --git a/src/luarocks/install.lua b/src/luarocks/install.lua
index f78c6a0d..d961f525 100644
--- a/src/luarocks/install.lua
+++ b/src/luarocks/install.lua
@@ -13,6 +13,7 @@ local manif = require("luarocks.manif")
13local remove = require("luarocks.remove") 13local remove = require("luarocks.remove")
14local cfg = require("luarocks.cfg") 14local cfg = require("luarocks.cfg")
15 15
16util.add_run_function(install)
16install.help_summary = "Install a rock." 17install.help_summary = "Install a rock."
17 18
18install.help_arguments = "{<rock>|<name> [<version>]}" 19install.help_arguments = "{<rock>|<name> [<version>]}"
@@ -149,8 +150,7 @@ end
149-- may also be given. 150-- may also be given.
150-- @return boolean or (nil, string, exitcode): True if installation was 151-- @return boolean or (nil, string, exitcode): True if installation was
151-- successful, nil and an error message otherwise. exitcode is optionally returned. 152-- successful, nil and an error message otherwise. exitcode is optionally returned.
152function install.run(...) 153function install.command(flags, name, version)
153 local flags, name, version = util.parse_flags(...)
154 if type(name) ~= "string" then 154 if type(name) ~= "string" then
155 return nil, "Argument missing. "..util.see_help("install") 155 return nil, "Argument missing. "..util.see_help("install")
156 end 156 end
diff --git a/src/luarocks/lint.lua b/src/luarocks/lint.lua
index 81d8bab2..d5cc48d0 100644
--- a/src/luarocks/lint.lua
+++ b/src/luarocks/lint.lua
@@ -8,6 +8,7 @@ local util = require("luarocks.util")
8local download = require("luarocks.download") 8local download = require("luarocks.download")
9local fetch = require("luarocks.fetch") 9local fetch = require("luarocks.fetch")
10 10
11util.add_run_function(lint)
11lint.help_summary = "Check syntax of a rockspec." 12lint.help_summary = "Check syntax of a rockspec."
12lint.help_arguments = "<rockspec>" 13lint.help_arguments = "<rockspec>"
13lint.help = [[ 14lint.help = [[
@@ -17,9 +18,7 @@ It returns success or failure if the text of a rockspec is
17syntactically correct. 18syntactically correct.
18]] 19]]
19 20
20function lint.run(...) 21function lint.command(flags, input)
21 local flags, input = util.parse_flags(...)
22
23 if not input then 22 if not input then
24 return nil, "Argument missing. "..util.see_help("lint") 23 return nil, "Argument missing. "..util.see_help("lint")
25 end 24 end
diff --git a/src/luarocks/list.lua b/src/luarocks/list.lua
index 09fa9ad7..c65e058f 100644
--- a/src/luarocks/list.lua
+++ b/src/luarocks/list.lua
@@ -10,6 +10,7 @@ local cfg = require("luarocks.cfg")
10local util = require("luarocks.util") 10local util = require("luarocks.util")
11local path = require("luarocks.path") 11local path = require("luarocks.path")
12 12
13util.add_run_function(list)
13list.help_summary = "List currently installed rocks." 14list.help_summary = "List currently installed rocks."
14list.help_arguments = "[--porcelain] <filter>" 15list.help_arguments = "[--porcelain] <filter>"
15list.help = [[ 16list.help = [[
@@ -69,8 +70,7 @@ end
69-- @param filter string or nil: A substring of a rock name to filter by. 70-- @param filter string or nil: A substring of a rock name to filter by.
70-- @param version string or nil: a version may also be passed. 71-- @param version string or nil: a version may also be passed.
71-- @return boolean: True if succeeded, nil on errors. 72-- @return boolean: True if succeeded, nil on errors.
72function list.run(...) 73function list.command(flags, filter, version)
73 local flags, filter, version = util.parse_flags(...)
74 local query = search.make_query(filter and filter:lower() or "", version) 74 local query = search.make_query(filter and filter:lower() or "", version)
75 query.exact_name = false 75 query.exact_name = false
76 local trees = cfg.rocks_trees 76 local trees = cfg.rocks_trees
diff --git a/src/luarocks/make.lua b/src/luarocks/make.lua
index 9d675884..1464def7 100644
--- a/src/luarocks/make.lua
+++ b/src/luarocks/make.lua
@@ -15,6 +15,7 @@ local pack = require("luarocks.pack")
15local remove = require("luarocks.remove") 15local remove = require("luarocks.remove")
16local deps = require("luarocks.deps") 16local deps = require("luarocks.deps")
17 17
18util.add_run_function(make)
18make.help_summary = "Compile package in current directory using a rockspec." 19make.help_summary = "Compile package in current directory using a rockspec."
19make.help_arguments = "[--pack-binary-rock] [<rockspec>]" 20make.help_arguments = "[--pack-binary-rock] [<rockspec>]"
20make.help = [[ 21make.help = [[
@@ -50,8 +51,7 @@ To install rocks, you'll normally want to use the "install" and
50-- @param name string: A local rockspec. 51-- @param name string: A local rockspec.
51-- @return boolean or (nil, string, exitcode): True if build was successful; nil and an 52-- @return boolean or (nil, string, exitcode): True if build was successful; nil and an
52-- error message otherwise. exitcode is optionally returned. 53-- error message otherwise. exitcode is optionally returned.
53function make.run(...) 54function make.command(flags, rockspec)
54 local flags, rockspec = util.parse_flags(...)
55 assert(type(rockspec) == "string" or not rockspec) 55 assert(type(rockspec) == "string" or not rockspec)
56 56
57 if not rockspec then 57 if not rockspec then
diff --git a/src/luarocks/make_manifest.lua b/src/luarocks/make_manifest.lua
index 3d9a6bac..c39c2939 100644
--- a/src/luarocks/make_manifest.lua
+++ b/src/luarocks/make_manifest.lua
@@ -12,6 +12,7 @@ local deps = require("luarocks.deps")
12local fs = require("luarocks.fs") 12local fs = require("luarocks.fs")
13local dir = require("luarocks.dir") 13local dir = require("luarocks.dir")
14 14
15util.add_run_function(make_manifest)
15make_manifest.help_summary = "Compile a manifest file for a repository." 16make_manifest.help_summary = "Compile a manifest file for a repository."
16 17
17make_manifest.help = [[ 18make_manifest.help = [[
@@ -26,9 +27,7 @@ make_manifest.help = [[
26-- the default local repository configured as cfg.rocks_dir is used. 27-- the default local repository configured as cfg.rocks_dir is used.
27-- @return boolean or (nil, string): True if manifest was generated, 28-- @return boolean or (nil, string): True if manifest was generated,
28-- or nil and an error message. 29-- or nil and an error message.
29function make_manifest.run(...) 30function make_manifest.command(flags, repo)
30 local flags, repo = util.parse_flags(...)
31
32 assert(type(repo) == "string" or not repo) 31 assert(type(repo) == "string" or not repo)
33 repo = repo or cfg.rocks_dir 32 repo = repo or cfg.rocks_dir
34 33
diff --git a/src/luarocks/new_version.lua b/src/luarocks/new_version.lua
index 3382b85c..bd73e308 100644
--- a/src/luarocks/new_version.lua
+++ b/src/luarocks/new_version.lua
@@ -10,6 +10,7 @@ local persist = require("luarocks.persist")
10local fs = require("luarocks.fs") 10local fs = require("luarocks.fs")
11local type_check = require("luarocks.type_check") 11local type_check = require("luarocks.type_check")
12 12
13util.add_run_function(new_version)
13new_version.help_summary = "Auto-write a rockspec for a new version of a rock." 14new_version.help_summary = "Auto-write a rockspec for a new version of a rock."
14new_version.help_arguments = "[--tag=<tag>] [<package>|<rockspec>] [<new_version>] [<new_url>]" 15new_version.help_arguments = "[--tag=<tag>] [<package>|<rockspec>] [<new_version>] [<new_url>]"
15new_version.help = [[ 16new_version.help = [[
@@ -123,8 +124,7 @@ local function update_source_section(out_rs, url, tag, old_ver, new_ver)
123 return true 124 return true
124end 125end
125 126
126function new_version.run(...) 127function new_version.command(flags, input, version, url)
127 local flags, input, version, url = util.parse_flags(...)
128 if not input then 128 if not input then
129 local err 129 local err
130 input, err = util.get_default_rockspec() 130 input, err = util.get_default_rockspec()
diff --git a/src/luarocks/pack.lua b/src/luarocks/pack.lua
index 685b84bd..277cf246 100644
--- a/src/luarocks/pack.lua
+++ b/src/luarocks/pack.lua
@@ -16,6 +16,7 @@ local dir = require("luarocks.dir")
16local manif = require("luarocks.manif") 16local manif = require("luarocks.manif")
17local search = require("luarocks.search") 17local search = require("luarocks.search")
18 18
19util.add_run_function(pack)
19pack.help_summary = "Create a rock, packing sources or binaries." 20pack.help_summary = "Create a rock, packing sources or binaries."
20pack.help_arguments = "{<rockspec>|<name> [<version>]}" 21pack.help_arguments = "{<rockspec>|<name> [<version>]}"
21pack.help = [[ 22pack.help = [[
@@ -191,8 +192,7 @@ end
191-- version may also be passed. 192-- version may also be passed.
192-- @return boolean or (nil, string): true if successful or nil followed 193-- @return boolean or (nil, string): true if successful or nil followed
193-- by an error message. 194-- by an error message.
194function pack.run(...) 195function pack.command(flags, arg, version)
195 local flags, arg, version = util.parse_flags(...)
196 assert(type(version) == "string" or not version) 196 assert(type(version) == "string" or not version)
197 if type(arg) ~= "string" then 197 if type(arg) ~= "string" then
198 return nil, "Argument missing. "..util.see_help("pack") 198 return nil, "Argument missing. "..util.see_help("pack")
diff --git a/src/luarocks/path_cmd.lua b/src/luarocks/path_cmd.lua
index ecd6d4b1..15fb9ca2 100644
--- a/src/luarocks/path_cmd.lua
+++ b/src/luarocks/path_cmd.lua
@@ -7,6 +7,7 @@ local util = require("luarocks.util")
7local deps = require("luarocks.deps") 7local deps = require("luarocks.deps")
8local cfg = require("luarocks.cfg") 8local cfg = require("luarocks.cfg")
9 9
10util.add_run_function(path_cmd)
10path_cmd.help_summary = "Return the currently configured package path." 11path_cmd.help_summary = "Return the currently configured package path."
11path_cmd.help_arguments = "" 12path_cmd.help_arguments = ""
12path_cmd.help = [[ 13path_cmd.help = [[
@@ -33,8 +34,7 @@ And on Windows:
33 34
34--- Driver function for "path" command. 35--- Driver function for "path" command.
35-- @return boolean This function always succeeds. 36-- @return boolean This function always succeeds.
36function path_cmd.run(...) 37function path_cmd.command(flags)
37 local flags = util.parse_flags(...)
38 local deps_mode = deps.get_deps_mode(flags) 38 local deps_mode = deps.get_deps_mode(flags)
39 39
40 local lr_path, lr_cpath, lr_bin = cfg.package_paths(flags["tree"]) 40 local lr_path, lr_cpath, lr_bin = cfg.package_paths(flags["tree"])
diff --git a/src/luarocks/purge.lua b/src/luarocks/purge.lua
index 0be6ef21..1ce46c0f 100644
--- a/src/luarocks/purge.lua
+++ b/src/luarocks/purge.lua
@@ -14,6 +14,7 @@ local manif = require("luarocks.manif")
14local cfg = require("luarocks.cfg") 14local cfg = require("luarocks.cfg")
15local remove = require("luarocks.remove") 15local remove = require("luarocks.remove")
16 16
17util.add_run_function(purge)
17purge.help_summary = "Remove all installed rocks from a tree." 18purge.help_summary = "Remove all installed rocks from a tree."
18purge.help_arguments = "--tree=<tree> [--old-versions]" 19purge.help_arguments = "--tree=<tree> [--old-versions]"
19purge.help = [[ 20purge.help = [[
@@ -30,9 +31,7 @@ assume a default tree.
30 overridden with the flag --force. 31 overridden with the flag --force.
31]] 32]]
32 33
33function purge.run(...) 34function purge.command(flags)
34 local flags = util.parse_flags(...)
35
36 local tree = flags["tree"] 35 local tree = flags["tree"]
37 36
38 if type(tree) ~= "string" then 37 if type(tree) ~= "string" then
diff --git a/src/luarocks/refresh_cache.lua b/src/luarocks/refresh_cache.lua
index cc5b9c33..bbfd1f4d 100644
--- a/src/luarocks/refresh_cache.lua
+++ b/src/luarocks/refresh_cache.lua
@@ -7,6 +7,7 @@ local util = require("luarocks.util")
7local cfg = require("luarocks.cfg") 7local cfg = require("luarocks.cfg")
8local cache = require("luarocks.cache") 8local cache = require("luarocks.cache")
9 9
10util.add_run_function(refresh_cache)
10refresh_cache.help_summary = "Refresh local cache of a remote rocks server." 11refresh_cache.help_summary = "Refresh local cache of a remote rocks server."
11refresh_cache.help_arguments = "[--from=<server>]" 12refresh_cache.help_arguments = "[--from=<server>]"
12refresh_cache.help = [[ 13refresh_cache.help = [[
@@ -15,8 +16,7 @@ If not given, the default server set in the upload_server variable
15from the configuration file is used instead. 16from the configuration file is used instead.
16]] 17]]
17 18
18function refresh_cache.run(...) 19function refresh_cache.command(flags)
19 local flags = util.parse_flags(...)
20 local server, upload_server = cache.get_upload_server(flags["server"]) 20 local server, upload_server = cache.get_upload_server(flags["server"])
21 if not server then return nil, upload_server end 21 if not server then return nil, upload_server end
22 local download_url = cache.get_server_urls(server, upload_server) 22 local download_url = cache.get_server_urls(server, upload_server)
diff --git a/src/luarocks/remove.lua b/src/luarocks/remove.lua
index 7a559f8f..38851d36 100644
--- a/src/luarocks/remove.lua
+++ b/src/luarocks/remove.lua
@@ -14,6 +14,7 @@ local cfg = require("luarocks.cfg")
14local manif = require("luarocks.manif") 14local manif = require("luarocks.manif")
15local fs = require("luarocks.fs") 15local fs = require("luarocks.fs")
16 16
17util.add_run_function(remove)
17remove.help_summary = "Uninstall a rock." 18remove.help_summary = "Uninstall a rock."
18remove.help_arguments = "[--force|--force-fast] <name> [<version>]" 19remove.help_arguments = "[--force|--force-fast] <name> [<version>]"
19remove.help = [[ 20remove.help = [[
@@ -136,9 +137,7 @@ end
136-- may also be given. 137-- may also be given.
137-- @return boolean or (nil, string, exitcode): True if removal was 138-- @return boolean or (nil, string, exitcode): True if removal was
138-- successful, nil and an error message otherwise. exitcode is optionally returned. 139-- successful, nil and an error message otherwise. exitcode is optionally returned.
139function remove.run(...) 140function remove.command(flags, name, version)
140 local flags, name, version = util.parse_flags(...)
141
142 if type(name) ~= "string" then 141 if type(name) ~= "string" then
143 return nil, "Argument missing, see help." 142 return nil, "Argument missing, see help."
144 end 143 end
diff --git a/src/luarocks/search.lua b/src/luarocks/search.lua
index 4ec0c65e..eaa321d5 100644
--- a/src/luarocks/search.lua
+++ b/src/luarocks/search.lua
@@ -11,6 +11,7 @@ local deps = require("luarocks.deps")
11local cfg = require("luarocks.cfg") 11local cfg = require("luarocks.cfg")
12local util = require("luarocks.util") 12local util = require("luarocks.util")
13 13
14util.add_run_function(search)
14search.help_summary = "Query the LuaRocks servers." 15search.help_summary = "Query the LuaRocks servers."
15search.help_arguments = "[--source] [--binary] { <name> [<version>] | --all }" 16search.help_arguments = "[--source] [--binary] { <name> [<version>] | --all }"
16search.help = [[ 17search.help = [[
@@ -420,9 +421,7 @@ end
420-- @param version string or nil: a version may also be passed. 421-- @param version string or nil: a version may also be passed.
421-- @return boolean or (nil, string): True if build was successful; nil and an 422-- @return boolean or (nil, string): True if build was successful; nil and an
422-- error message otherwise. 423-- error message otherwise.
423function search.run(...) 424function search.command(flags, name, version)
424 local flags, name, version = util.parse_flags(...)
425
426 if flags["all"] then 425 if flags["all"] then
427 name, version = "", nil 426 name, version = "", nil
428 end 427 end
diff --git a/src/luarocks/show.lua b/src/luarocks/show.lua
index 1dc01cc0..01860e78 100644
--- a/src/luarocks/show.lua
+++ b/src/luarocks/show.lua
@@ -10,6 +10,8 @@ local path = require("luarocks.path")
10local deps = require("luarocks.deps") 10local deps = require("luarocks.deps")
11local fetch = require("luarocks.fetch") 11local fetch = require("luarocks.fetch")
12local manif = require("luarocks.manif") 12local manif = require("luarocks.manif")
13
14util.add_run_function(show)
13show.help_summary = "Show information about an installed rock." 15show.help_summary = "Show information about an installed rock."
14 16
15show.help = [[ 17show.help = [[
@@ -103,8 +105,7 @@ end
103-- @param name or nil: an existing package name. 105-- @param name or nil: an existing package name.
104-- @param version string or nil: a version may also be passed. 106-- @param version string or nil: a version may also be passed.
105-- @return boolean: True if succeeded, nil on errors. 107-- @return boolean: True if succeeded, nil on errors.
106function show.run(...) 108function show.command(flags, name, version)
107 local flags, name, version = util.parse_flags(...)
108 if not name then 109 if not name then
109 return nil, "Argument missing. "..util.see_help("show") 110 return nil, "Argument missing. "..util.see_help("show")
110 end 111 end
diff --git a/src/luarocks/unpack.lua b/src/luarocks/unpack.lua
index 4afe4547..2face005 100644
--- a/src/luarocks/unpack.lua
+++ b/src/luarocks/unpack.lua
@@ -11,6 +11,7 @@ local build = require("luarocks.build")
11local dir = require("luarocks.dir") 11local dir = require("luarocks.dir")
12local cfg = require("luarocks.cfg") 12local cfg = require("luarocks.cfg")
13 13
14util.add_run_function(unpack)
14unpack.help_summary = "Unpack the contents of a rock." 15unpack.help_summary = "Unpack the contents of a rock."
15unpack.help_arguments = "[--force] {<rock>|<name> [<version>]}" 16unpack.help_arguments = "[--force] {<rock>|<name> [<version>]}"
16unpack.help = [[ 17unpack.help = [[
@@ -149,9 +150,7 @@ end
149-- version may also be passed. 150-- version may also be passed.
150-- @return boolean or (nil, string): true if successful or nil followed 151-- @return boolean or (nil, string): true if successful or nil followed
151-- by an error message. 152-- by an error message.
152function unpack.run(...) 153function unpack.command(flags, name, version)
153 local flags, name, version = util.parse_flags(...)
154
155 assert(type(version) == "string" or not version) 154 assert(type(version) == "string" or not version)
156 if type(name) ~= "string" then 155 if type(name) ~= "string" then
157 return nil, "Argument missing. "..util.see_help("unpack") 156 return nil, "Argument missing. "..util.see_help("unpack")
diff --git a/src/luarocks/upload.lua b/src/luarocks/upload.lua
index 19ddee8d..3adc1704 100644
--- a/src/luarocks/upload.lua
+++ b/src/luarocks/upload.lua
@@ -7,6 +7,7 @@ local pack = require("luarocks.pack")
7local cfg = require("luarocks.cfg") 7local cfg = require("luarocks.cfg")
8local Api = require("luarocks.upload.api") 8local Api = require("luarocks.upload.api")
9 9
10util.add_run_function(upload)
10upload.help_summary = "Upload a rockspec to the public rocks repository." 11upload.help_summary = "Upload a rockspec to the public rocks repository."
11upload.help_arguments = "[--skip-pack] [--api-key=<key>] [--force] <rockspec>" 12upload.help_arguments = "[--skip-pack] [--api-key=<key>] [--force] <rockspec>"
12upload.help = [[ 13upload.help = [[
@@ -20,8 +21,7 @@ upload.help = [[
20 increment the revision number instead. 21 increment the revision number instead.
21]] 22]]
22 23
23function upload.run(...) 24function upload.command(flags, fname)
24 local flags, fname = util.parse_flags(...)
25 if not fname then 25 if not fname then
26 return nil, "Missing rockspec. "..util.see_help("upload") 26 return nil, "Missing rockspec. "..util.see_help("upload")
27 end 27 end
diff --git a/src/luarocks/util.lua b/src/luarocks/util.lua
index f7e9b1ae..6aff5324 100644
--- a/src/luarocks/util.lua
+++ b/src/luarocks/util.lua
@@ -228,6 +228,13 @@ function util.forward_flags(flags, ...)
228 return unpack(out) 228 return unpack(out)
229end 229end
230 230
231-- Adds legacy 'run' function to a command module.
232-- @param command table: command module with 'command' function,
233-- the added 'run' function calls it after parseing command-line arguments.
234function util.add_run_function(command)
235 command.run = function(...) return command.command(util.parse_flags(...)) end
236end
237
231--- Merges contents of src on top of dst's contents. 238--- Merges contents of src on top of dst's contents.
232-- @param dst Destination table, which will receive src's contents. 239-- @param dst Destination table, which will receive src's contents.
233-- @param src Table which provides new contents to dst. 240-- @param src Table which provides new contents to dst.
diff --git a/src/luarocks/validate.lua b/src/luarocks/validate.lua
index 24c6f835..c4570aa4 100644
--- a/src/luarocks/validate.lua
+++ b/src/luarocks/validate.lua
@@ -11,6 +11,7 @@ local build = require("luarocks.build")
11local install = require("luarocks.install") 11local install = require("luarocks.install")
12local util = require("luarocks.util") 12local util = require("luarocks.util")
13 13
14util.add_run_function(validate)
14validate.help_summary = "Sandboxed test of build/install of all packages in a repository." 15validate.help_summary = "Sandboxed test of build/install of all packages in a repository."
15 16
16validate.help = [[ 17validate.help = [[
@@ -74,8 +75,7 @@ local function validate_rock(file)
74 return ok, err, errcode 75 return ok, err, errcode
75end 76end
76 77
77function validate.run(...) 78function validate.command(flags, repo)
78 local flags, repo = util.parse_flags(...)
79 repo = repo or cfg.rocks_dir 79 repo = repo or cfg.rocks_dir
80 80
81 util.printout("Verifying contents of "..repo) 81 util.printout("Verifying contents of "..repo)
diff --git a/src/luarocks/write_rockspec.lua b/src/luarocks/write_rockspec.lua
index 79902bb4..33edeb1b 100644
--- a/src/luarocks/write_rockspec.lua
+++ b/src/luarocks/write_rockspec.lua
@@ -11,6 +11,7 @@ local persist = require("luarocks.persist")
11local type_check = require("luarocks.type_check") 11local type_check = require("luarocks.type_check")
12local util = require("luarocks.util") 12local util = require("luarocks.util")
13 13
14util.add_run_function(write_rockspec)
14write_rockspec.help_summary = "Write a template for a rockspec file." 15write_rockspec.help_summary = "Write a template for a rockspec file."
15write_rockspec.help_arguments = "[--output=<file> ...] [<name>] [<version>] [<url>|<path>]" 16write_rockspec.help_arguments = "[--output=<file> ...] [<name>] [<version>] [<url>|<path>]"
16write_rockspec.help = [[ 17write_rockspec.help = [[
@@ -224,9 +225,7 @@ local function rockspec_cleanup(rockspec)
224 rockspec.name = nil 225 rockspec.name = nil
225end 226end
226 227
227function write_rockspec.run(...) 228function write_rockspec.command(flags, name, version, url_or_dir)
228 local flags, name, version, url_or_dir = util.parse_flags(...)
229
230 if not name then 229 if not name then
231 url_or_dir = "." 230 url_or_dir = "."
232 elseif not version then 231 elseif not version then