Search code examples
nim-lang

Error: identifier expected, but got: paramStr(1)


So, I had this code:

import std/os
import system/io
echo paramStr(1)
if declared(paramStr(1)) == true:
    echo It exists
else:
    echo It doesn't exists

And then while I was compiling this error ocurred:

Error: identifier expected, but got: paramStr(1)

I don't know what could be the cause of it, maybe it could be the type of paramStr?


Solution

  • You are using declared wrongly. It's used to check if some identifier has been declared. E.g.

    import os
    
    when declared(paramStr):
      echo true
    

    Echoes true because paramStr has been declared in os. Remove the import os line and it echoes false (without throwing an import error).

    If you want to check if you have a paramStr(1), you can do it with if paramCount() > 0 instead.

    Also, you don't need to import system nor system/io, as they are automatically included in your program.

    If you find yourself coding a complex command line, take a look at the parseopt included in the batteries, or at the docopt wrapper in http://nimble.directory