--[[
  Made by SinZ and boq
--]]
local args = {...}
if #args == 0 then
  print("usage: docs <side> (function)")
  return
end

local side = args[1]
local p = peripheral.wrap(side)

if not p then
  print("No peripheral on '" .. side .. "'")
  return
end

if not p.getAdvancedMethodsData then
 print("Peripheral '" .. peripheral.getType(side) .. "' is not OpenPeripheral(TM)")
 return
end

local info = p.getAdvancedMethodsData()

function argName(arg)
  if arg.vararg then
    return arg.name.."..."
  elseif arg.optional then
    return arg.name.."?"
  else
    return arg.name
  end
end

if #args == 1 then
  for name,data in pairs(info) do
    args = {}
    for _,arg in pairs(data.args) do
      table.insert(args, argName(arg))
    end
    print(name.."("..table.concat(args,",")..")")
  end
else --must be 2 or more
  for name,data in pairs(info) do
    if args[2]:lower() == name:lower() then
      print(name..": "..data.description)
      print("source: " .. data.source) 
      print("returns: "..string.lower(data.returnTypes))
      if #data.args > 0 then
        print("args: ")
        for _,arg in ipairs(data.args) do
          print(" - ("..arg.type:lower() .. ")"..argName(arg)..": "..arg.description)
        end
      end
    end
  end
end
