PageRenderTime 50ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

/src/luarocks/help.lua

http://github.com/keplerproject/luarocks
Lua | 118 lines | 90 code | 11 blank | 17 comment | 11 complexity | 7e5b035fea78b1990d0955e4008679c1 MD5 | raw file
Possible License(s): Unlicense
  1. --- Module implementing the LuaRocks "help" command.
  2. -- This is a generic help display module, which
  3. -- uses a global table called "commands" to find commands
  4. -- to show help for; each command should be represented by a
  5. -- table containing "help" and "help_summary" fields.
  6. local help = {}
  7. local util = require("luarocks.util")
  8. local cfg = require("luarocks.cfg")
  9. local dir = require("luarocks.dir")
  10. local program = util.this_program("luarocks")
  11. util.add_run_function(help)
  12. help.help_summary = "Help on commands. Type '"..program.." help <command>' for more."
  13. help.help_arguments = "[<command>]"
  14. help.help = [[
  15. <command> is the command to show help for.
  16. ]]
  17. local function print_banner()
  18. util.printout("\nLuaRocks "..cfg.program_version..", a module deployment system for Lua")
  19. end
  20. local function print_section(section)
  21. util.printout("\n"..section)
  22. end
  23. local function get_status(status)
  24. if status then
  25. return "ok"
  26. else
  27. return "not found"
  28. end
  29. end
  30. --- Driver function for the "help" command.
  31. -- @param command string or nil: command to show help for; if not
  32. -- given, help summaries for all commands are shown.
  33. -- @return boolean or (nil, string): true if there were no errors
  34. -- or nil and an error message if an invalid command was requested.
  35. function help.command(flags, command)
  36. if not command then
  37. local conf = cfg.which_config()
  38. print_banner()
  39. print_section("NAME")
  40. util.printout("\t"..program..[[ - ]]..program_description)
  41. print_section("SYNOPSIS")
  42. util.printout("\t"..program..[[ [--from=<server> | --only-from=<server>] [--to=<tree>] [VAR=VALUE]... <command> [<argument>] ]])
  43. print_section("GENERAL OPTIONS")
  44. util.printout([[
  45. These apply to all commands, as appropriate:
  46. --server=<server> Fetch rocks/rockspecs from this server
  47. (takes priority over config file)
  48. --only-server=<server> Fetch rocks/rockspecs from this server only
  49. (overrides any entries in the config file)
  50. --only-sources=<url> Restrict downloads to paths matching the
  51. given URL.
  52. --tree=<tree> Which tree to operate on.
  53. --local Use the tree in the user's home directory.
  54. To enable it, see ']]..program..[[ help path'.
  55. --verbose Display verbose output of commands executed.
  56. --timeout=<seconds> Timeout on network operations, in seconds.
  57. 0 means no timeout (wait forever).
  58. Default is ]]..tostring(cfg.connection_timeout)..[[.]])
  59. print_section("VARIABLES")
  60. util.printout([[
  61. Variables from the "variables" table of the configuration file
  62. can be overriden with VAR=VALUE assignments.]])
  63. print_section("COMMANDS")
  64. for name, command in util.sortedpairs(commands) do
  65. local cmd = require(command)
  66. util.printout("", name)
  67. util.printout("\t", cmd.help_summary)
  68. end
  69. print_section("CONFIGURATION")
  70. util.printout("\tLua version: " .. cfg.lua_version)
  71. util.printout("\tConfiguration files:")
  72. util.printout("\t\tSystem: ".. dir.normalize(conf.system.file) .. " (" .. get_status(conf.system.ok) ..")")
  73. if conf.user.file then
  74. util.printout("\t\tUser : ".. dir.normalize(conf.user.file) .. " (" .. get_status(conf.user.ok) ..")\n")
  75. else
  76. util.printout("\t\tUser : disabled in this LuaRocks installation.\n")
  77. end
  78. util.printout("\tRocks trees in use: ")
  79. for _, tree in ipairs(cfg.rocks_trees) do
  80. if type(tree) == "string" then
  81. util.printout("\t\t"..dir.normalize(tree))
  82. else
  83. local name = tree.name and " (\""..tree.name.."\")" or ""
  84. util.printout("\t\t"..dir.normalize(tree.root)..name)
  85. end
  86. end
  87. else
  88. command = command:gsub("-", "_")
  89. local cmd = commands[command] and require(commands[command])
  90. if cmd then
  91. local arguments = cmd.help_arguments or "<argument>"
  92. print_banner()
  93. print_section("NAME")
  94. util.printout("\t"..program.." "..command.." - "..cmd.help_summary)
  95. print_section("SYNOPSIS")
  96. util.printout("\t"..program.." "..command.." "..arguments)
  97. print_section("DESCRIPTION")
  98. util.printout("",(cmd.help:gsub("\n","\n\t"):gsub("\n\t$","")))
  99. print_section("SEE ALSO")
  100. util.printout("","'"..program.." help' for general options and configuration.\n")
  101. else
  102. return nil, "Unknown command: "..command
  103. end
  104. end
  105. return true
  106. end
  107. return help