From 99dcdc22319f38051087ab6929cfec170d784f0c Mon Sep 17 00:00:00 2001 From: Hisham Muhammad Date: Thu, 18 Apr 2013 00:19:21 -0300 Subject: Improve "see help" messages and always use the name by which the luarocks program was launched. --- src/bin/luarocks | 1 - src/bin/luarocks-admin | 1 - src/luarocks/add.lua | 2 +- src/luarocks/admin_remove.lua | 2 +- src/luarocks/build.lua | 2 +- src/luarocks/command_line.lua | 4 +++- src/luarocks/help.lua | 17 ++++++++++------- src/luarocks/install.lua | 2 +- src/luarocks/lint.lua | 2 +- src/luarocks/make.lua | 2 +- src/luarocks/new_version.lua | 2 +- src/luarocks/pack.lua | 2 +- src/luarocks/purge.lua | 2 +- src/luarocks/search.lua | 2 +- src/luarocks/show.lua | 2 +- src/luarocks/unpack.lua | 2 +- src/luarocks/util.lua | 18 ++++++++++++++++++ 17 files changed, 43 insertions(+), 22 deletions(-) diff --git a/src/bin/luarocks b/src/bin/luarocks index aaafb905..b64051d4 100755 --- a/src/bin/luarocks +++ b/src/bin/luarocks @@ -2,7 +2,6 @@ local command_line = require("luarocks.command_line") -program_name = "luarocks" program_description = "LuaRocks main command-line interface" commands = {} diff --git a/src/bin/luarocks-admin b/src/bin/luarocks-admin index 69814459..4c5613ca 100755 --- a/src/bin/luarocks-admin +++ b/src/bin/luarocks-admin @@ -2,7 +2,6 @@ local command_line = require("luarocks.command_line") -program_name = "luarocks-admin" program_description = "LuaRocks repository administration interface" commands = { diff --git a/src/luarocks/add.lua b/src/luarocks/add.lua index 48b9964c..30db9a48 100644 --- a/src/luarocks/add.lua +++ b/src/luarocks/add.lua @@ -99,7 +99,7 @@ function run(...) local files = { util.parse_flags(...) } local flags = table.remove(files, 1) if #files < 1 then - return nil, "Argument missing, see help." + return nil, "Argument missing. "..util.see_help("add", "luarocks-admin") end local server, server_table = cache.get_upload_server(flags["server"]) if not server then return nil, server_table end diff --git a/src/luarocks/admin_remove.lua b/src/luarocks/admin_remove.lua index dc36e16e..c235776f 100644 --- a/src/luarocks/admin_remove.lua +++ b/src/luarocks/admin_remove.lua @@ -78,7 +78,7 @@ function run(...) local files = { util.parse_flags(...) } local flags = table.remove(files, 1) if #files < 1 then - return nil, "Argument missing, see help." + return nil, "Argument missing. "..util.see_help("remove", "luarocks-admin") end local server, server_table = cache.get_upload_server(flags["server"]) if not server then return nil, server_table end diff --git a/src/luarocks/build.lua b/src/luarocks/build.lua index d61d6611..83abbf8d 100644 --- a/src/luarocks/build.lua +++ b/src/luarocks/build.lua @@ -321,7 +321,7 @@ end function run(...) local flags, name, version = util.parse_flags(...) if type(name) ~= "string" then - return nil, "Argument missing, see help." + return nil, "Argument missing. "..util.see_help("build") end assert(type(version) == "string" or not version) diff --git a/src/luarocks/command_line.lua b/src/luarocks/command_line.lua index 9e499fc2..d16ef8e2 100644 --- a/src/luarocks/command_line.lua +++ b/src/luarocks/command_line.lua @@ -8,6 +8,8 @@ local path = require("luarocks.path") local dir = require("luarocks.dir") local deps = require("luarocks.deps") +local program = util.this_program("luarocks") + --- Display an error message and exit. -- @param message string: The error message. local function die(message) @@ -60,7 +62,7 @@ function run_command(...) local command if flags["version"] then - util.printout(program_name.." "..cfg.program_version) + util.printout(program.." "..cfg.program_version) util.printout(program_description) util.printout() os.exit(0) diff --git a/src/luarocks/help.lua b/src/luarocks/help.lua index 19cd7914..45fc3bb4 100644 --- a/src/luarocks/help.lua +++ b/src/luarocks/help.lua @@ -9,7 +9,9 @@ module("luarocks.help", package.seeall) local util = require("luarocks.util") local cfg = require("luarocks.cfg") -help_summary = "Help on commands. Type '"..program_name.." help ' for more." +local program = util.this_program("luarocks") + +help_summary = "Help on commands. Type '"..program.." help ' for more." help_arguments = "[]" help = [[ @@ -46,9 +48,9 @@ function run(...) local sys_file, sys_ok, home_file, home_ok = cfg.which_config() print_banner() print_section("NAME") - util.printout("\t"..program_name..[[ - ]]..program_description) + util.printout("\t"..program..[[ - ]]..program_description) print_section("SYNOPSIS") - util.printout("\t"..program_name..[[ [--from= | --only-from=] [--to=] [VAR=VALUE]... [] ]]) + util.printout("\t"..program..[[ [--from= | --only-from=] [--to=] [VAR=VALUE]... [] ]]) print_section("GENERAL OPTIONS") util.printout([[ These apply to all commands, as appropriate: @@ -60,7 +62,8 @@ function run(...) --only-sources= Restrict downloads to paths matching the given URL. --tree= Which tree to operate on. - --local Use the tree in the user's home directory.]]) + --local Use the tree in the user's home directory. + To enable it, see ']]..program..[[ help path'.]]) print_section("VARIABLES") util.printout([[ Variables from the "variables" table of the configuration file @@ -86,13 +89,13 @@ function run(...) local arguments = commands[command].help_arguments or "" print_banner() print_section("NAME") - util.printout("\t"..program_name.." "..command.." - "..commands[command].help_summary) + util.printout("\t"..program.." "..command.." - "..commands[command].help_summary) print_section("SYNOPSIS") - util.printout("\t"..program_name.." "..command.." "..arguments) + util.printout("\t"..program.." "..command.." "..arguments) print_section("DESCRIPTION") util.printout("",(commands[command].help:gsub("\n","\n\t"):gsub("\n\t$",""))) print_section("SEE ALSO") - util.printout("","'luarocks help' for general options and configuration.\n") + util.printout("","'"..program.." help' for general options and configuration.\n") else return nil, "Unknown command '"..command.."'" end diff --git a/src/luarocks/install.lua b/src/luarocks/install.lua index e633ea23..c2a54482 100644 --- a/src/luarocks/install.lua +++ b/src/luarocks/install.lua @@ -118,7 +118,7 @@ end function run(...) local flags, name, version = util.parse_flags(...) if type(name) ~= "string" then - return nil, "Argument missing, see help." + return nil, "Argument missing. "..util.see_help("install") end local ok, err = fs.check_command_permissions(flags) diff --git a/src/luarocks/lint.lua b/src/luarocks/lint.lua index 61388af6..62ced64d 100644 --- a/src/luarocks/lint.lua +++ b/src/luarocks/lint.lua @@ -20,7 +20,7 @@ function run(...) local flags, input = util.parse_flags(...) if not input then - return nil, "Argument missing, see help." + return nil, "Argument missing. "..util.see_help("lint") end local filename = input diff --git a/src/luarocks/make.lua b/src/luarocks/make.lua index 69c2162f..027b9fa6 100644 --- a/src/luarocks/make.lua +++ b/src/luarocks/make.lua @@ -55,7 +55,7 @@ function run(...) end end if not rockspec:match("rockspec$") then - return nil, "Invalid argument: 'make' takes a rockspec as a parameter. See help." + return nil, "Invalid argument: 'make' takes a rockspec as a parameter. "..util.see_help("make") end if flags["pack-binary-rock"] then diff --git a/src/luarocks/new_version.lua b/src/luarocks/new_version.lua index 66802f0b..b91f8aed 100644 --- a/src/luarocks/new_version.lua +++ b/src/luarocks/new_version.lua @@ -80,7 +80,7 @@ end function run(...) local flags, input, version, url = util.parse_flags(...) if not input then - return nil, "Missing arguments: expected program or rockspec. See help." + return nil, "Missing arguments: expected program or rockspec. "..util.see_help("new_version") end assert(type(input) == "string") diff --git a/src/luarocks/pack.lua b/src/luarocks/pack.lua index d8f0b2ef..eaa0e165 100644 --- a/src/luarocks/pack.lua +++ b/src/luarocks/pack.lua @@ -187,7 +187,7 @@ function run(...) local flags, arg, version = util.parse_flags(...) assert(type(version) == "string" or not version) if type(arg) ~= "string" then - return nil, "Argument missing, see help." + return nil, "Argument missing. "..util.see_help("pack") end local file, err diff --git a/src/luarocks/purge.lua b/src/luarocks/purge.lua index ca682f7d..6b094061 100644 --- a/src/luarocks/purge.lua +++ b/src/luarocks/purge.lua @@ -27,7 +27,7 @@ function run(...) local tree = flags["tree"] if type(tree) ~= "string" then - return nil, "The --tree argument is mandatory, see help." + return nil, "The --tree argument is mandatory. "..util.see_help("purge") end local results = {} diff --git a/src/luarocks/search.lua b/src/luarocks/search.lua index 7db88d74..1fc1f9a1 100644 --- a/src/luarocks/search.lua +++ b/src/luarocks/search.lua @@ -365,7 +365,7 @@ function run(...) end if type(name) ~= "string" and not flags["all"] then - return nil, "Enter name and version or use --all; see help." + return nil, "Enter name and version or use --all. "..util.see_help("search") end local query = make_query(name:lower(), version) diff --git a/src/luarocks/show.lua b/src/luarocks/show.lua index a6a53d9a..536085a7 100644 --- a/src/luarocks/show.lua +++ b/src/luarocks/show.lua @@ -73,7 +73,7 @@ end function run(...) local flags, name, version = util.parse_flags(...) if not name then - return nil, "Argument missing, see help." + return nil, "Argument missing. "..util.see_help("show") end local results = {} local query = search.make_query(name, version) diff --git a/src/luarocks/unpack.lua b/src/luarocks/unpack.lua index 4920d381..1db66bbb 100644 --- a/src/luarocks/unpack.lua +++ b/src/luarocks/unpack.lua @@ -139,7 +139,7 @@ function run(...) assert(type(version) == "string" or not version) if type(name) ~= "string" then - return nil, "Argument missing, see help." + return nil, "Argument missing. "..util.see_help("unpack") end if name:match(".*%.rock") or name:match(".*%.rockspec") then diff --git a/src/luarocks/util.lua b/src/luarocks/util.lua index c44dc73d..c5095675 100644 --- a/src/luarocks/util.lua +++ b/src/luarocks/util.lua @@ -9,6 +9,7 @@ local global_env = _G module("luarocks.util", package.seeall) local scheduled_functions = {} +local debug = require("debug") --- Schedule a function to be executed upon program termination. -- This is useful for actions such as deleting temporary directories @@ -316,6 +317,23 @@ function title(msg, porcelain, underline) printout() end +function this_program(default) + local i = 1 + local last, cur = default, default + while i do + local dbg = debug.getinfo(i,"S") + if not dbg then break end + last = cur + cur = dbg.source + i=i+1 + end + return last:sub(2) +end + +function see_help(command, program) + return "See '"..this_program(program or "luarocks")..' help '..command.."'." +end + -- from http://lua-users.org/wiki/SplitJoin -- by PhilippeLhoste function split_string(str, delim, maxNb) -- cgit v1.2.3-55-g6feb