autoit

Is it possible to get the Line number in a function call?


I am aware that you can get the current line number in an AutoIT script using @ScriptLineNumber.

I tried to wrap that in a function for debugging:

Func dbug($data)
    ConsoleWrite(@ScriptLineNumber & ': $data$' & @CRLF)
EndFunc

dbug('hello')       ;   <-  want the line number here

The problem is that @ScriptLineNumber yields the line number in the function, not the line number where the function was called.

I have managed in other languages, such as PHP and Python to get the line number of the calling code, but I can’t work it out for AutoIT.

I might be able to do it if AutoIT were to support inline macros, but I don’t think it does.


Solution

  • Couldn't you just pass the line number as an argument to your function?

    I'm guessing something like this should work for your case:

    Func dbug($data, $lineNumber = @ScriptLineNumber)
        ConsoleWrite($lineNumber & ': ' & $data & @CRLF)
    EndFunc
    
    dbug('hello')