OS.get_cmdline_args()[0] invalid get index 0

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Mutaz

im trying to use OS.get_cmdline_args() to get the file that i opened using my program but it returns a poolstringarray path im trying to convert it or getting the index alone but its not working


if !OS.get_cmdline_args().empty():
    var f = File.new()

    f.open(str(OS.get_cmdline_args()[0])), File.WRITE)

    $TextEdit.text = str(f.get_as_text())

I don’t see any obvious way that reference to index 0 could go wrong in the above. Are you sure the problem is in this code? If so, what happens if you simply print the entire array? So…


And, assuming you get the results you expect, what happens if you then try to print first element?


jgodfrey | 2023-06-10 15:48

i removed the str() from OS.get_cmdline_args() and its printing the path correctly but when i use it the program stops responding and freeze at the splash screen

Mutaz | 2023-06-10 16:26

i fixed it, it was soo obvious i wrote File.write instead of File.read lmao
thanks anyway

Mutaz | 2023-06-10 16:58

:bust_in_silhouette: Reply From: Mutaz

i wrote File.write instead of File.read

Hmmm… The difference between File.read and File.write is not the cause of the error you originally reported (in the post title), but glad you have things working…

jgodfrey | 2023-06-10 18:48