This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
Admicos's profile picture

bad argument #1: value expected error on pcall

Started by Admicos, 19 September 2014 - 04:53 PM
Admicos #1
Posted 19 September 2014 - 06:53 PM
I made a program that uses pcalls (because why not?) but when i try to get the program working, i just get the error on the title.

Snippet of the code that i think needs a fix:

function main()
  if not args[1] == nil then
	if args[1] == "test" then
	 print("Hello world")
	  print(getFileInfo(var1, arg[2]))
	end
  end
end
local ok, err = pcall(main())
if not ok then
  error(err) --Bunu yapmamda amaç neydi?
else
  print("Sorunsuz!")  --Buydu oke
end

Note: getFileInfo is a function from a api i'm working on
Edited on 19 September 2014 - 04:53 PM
Dog #2
Posted 19 September 2014 - 07:05 PM
Hey Admicos - I'm not 100% sure this is the issue, but…how did you load your API? Like this?

os.loadAPI("myCustomAPI") --# or whatever your API is named

In the above example, you'd access the getFileInfo function of your API by doing this

print(myCustomAPI.getFileInfo(var1, arg[2]))

If your API is named getFileInfo, then you load it and access it's functions by name like so…

os.loadAPI("getFileInfo")
--# other stuff
print(getFileInfo.myCustomFunction(var1, arg[2]))

Without seeing your API and how you are loading it, that's the first thing that stands out to me. For reference - os.loadAPI

Although I've used pcall, I'm no expert on its use and I have a question for a more seasoned pro…does Admicos main() function need to return a boolean and/or error for the pcall or is that handled automatically based on whether or not main() errors out?

EDIT: question has been answered by the resolution of this thread - the function does NOT need to return anything as the pcall does this
Edited on 19 September 2014 - 07:50 PM
Admicos #3
Posted 19 September 2014 - 07:15 PM
Hey Admicos - I'm not 100% sure this is the issue, but…how did you load your API? Like this?

os.loadAPI("myCustomAPI") --# or whatever your API is named

In the above example, you'd access the getFileInfo function of your API by doing this

print(myCustomAPI.getFileInfo(var1, arg[2]))

If your API is named getFileInfo, then you load it and access it's functions by name like so…

os.loadAPI("getFileInfo")
--# other stuff
print(getFileInfo.myCustomFunction(var1, arg[2]))

Without seeing your API and how you are loading it, that's the first thing that stands out to me. For reference - os.loadAPI

Although I've used pcall, I'm no expert on its use and I have a question for a more seasoned pro…does Admicos main() function need to return a boolean and/or error for the pcall or is that handled automatically based on whether or not main() errors out?
Actually, I Tinkered with the code a bit and this still dont work.

Heres full code:

filesDir = "/AdmiGetFiles/"
defaultRepo = "--a web site url here" --Ilk kullanilan repo
args = { ... }
os.loadAPI(filesDir .. "admi-get-api")
function main()
  if not args[1] == nil then
	if args[1] == "-i" then
	 print("Hello world")
	  print(admi-get-api.getFileInfo(defaultRepo, arg[2]))
	end
  end
end
local ok, err = pcall(main()) --Şimdi bunda ne yanlış?
if not ok then
  error(err) --Bunu yapmamda amaç neydi?
else
  print("Sorunsuz!")  --Buydu oke
end

EDIT: Here's the api BTW:

function getFileInfo(repo, file)
local fileInfo = http.get(repo .. file .. ".ainf")
  info = fileInfo.readAll()
  print(info)
  fileInfo.close()
end
Yes. Just has this one function. I add other functions later
Edited on 19 September 2014 - 05:17 PM
Dog #4
Posted 19 September 2014 - 07:22 PM
The next thing I see is how you're invoking pcall. You shouldn't have the parentheses/brackets in the function name being called. Like so…

local ok, err = pcall(main)

Try that and see what happens (keep the changes you made to the API stuff as well)
Edited on 19 September 2014 - 05:23 PM
Admicos #5
Posted 19 September 2014 - 07:26 PM
The next thing I see is how you're invoking pcall. You shouldn't have the parentheses/brackets in the function name being called. Like so…

local ok, err = pcall(main)

Try that and see what happens (keep the changes you made to the API stuff as well)
That works. Thanks :)/>