Jump to content




bad argument #1: value expected error on pcall


  • You cannot reply to this topic
4 replies to this topic

#1 Admicos

  • Members
  • 207 posts
  • LocationTurkey

Posted 19 September 2014 - 05: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 by Admicos, 19 September 2014 - 05:53 PM.


#2 Dog

  • Members
  • 1,179 posts
  • LocationEarth orbit

Posted 19 September 2014 - 06: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 by Dog, 19 September 2014 - 08:50 PM.


#3 Admicos

  • Members
  • 207 posts
  • LocationTurkey

Posted 19 September 2014 - 06:15 PM

View PostDog, on 19 September 2014 - 06:05 PM, said:

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 by Admicos, 19 September 2014 - 06:17 PM.


#4 Dog

  • Members
  • 1,179 posts
  • LocationEarth orbit

Posted 19 September 2014 - 06: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 by Dog, 19 September 2014 - 06:23 PM.


#5 Admicos

  • Members
  • 207 posts
  • LocationTurkey

Posted 19 September 2014 - 06:26 PM

View PostDog, on 19 September 2014 - 06:22 PM, said:

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 :)





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users