The task is to programmatically obtain the name used to invoke the program. (For example determine whether the user ran "python hello.py", or "python hellocaller.py", a program importing the code from "hello.py".)

Sometimes a multiline shebang is necessary in order to provide the script name to a language's internal ARGV.

See also Command-line arguments

Examples from GitHub.

 
nSize = _MAX_PATH + 2
lpFilename$ = space$(nSize); chr$(0)
 
    calldll #kernel32, "GetModuleFileNameA", _
        hModule     as ulong, _
        lpFilename$ as ptr, _
        nSize       as ulong, _
        result      as ulong
lpFilename$ = left$(lpFilename$,result)
 
print "Path to LB exe"
print lpFilename$
print "current program file (:last one on LRU list)"
print getLastLRU$(lbPath$)
end
 
Function getLastLRU$(lbPath$)
    open lbPath$+"lbasic404.ini" for input as #1
        while not(eof(#1))
            line input #1, a$
            if instr(a$, "recent files")<>0 then [readRecentFiles]
        wend
        getLastLRU$ = "* Failed: Recent files section not found *"
        close #1
        exit function
 
[readRecentFiles]
        nRecent = val(word$(a$,1))
        'print "nRecentFiles", nRecent
        for i = 1 to nRecent
            if eof(#1) then
                getLastLRU$ = "* Failed: File ended while in recent files section *"
                close #1
                exit function
            end if
            line input #1, a$
            'print i, a$
        next
    close #1
    getLastLRU$ = a$
end function
 
 