Search code examples
modulefilenamesfactor-lang

Programmatically access module/filename in Factor?


The program name specified in command line usage, e.g. "Usage: rot13.factor [OPTIONS]".

Erlang has a macro called ?FILE to access the source code's own filename. Does Factor have something similar?

Rosetta Code


Solution

  • Factor's command-line vocabulary has a word script which either returns the script name or f when Factor is in interpreted mode.

    #! /usr/bin/env factor
    
    USING: namespaces io command-line ;
    IN: scriptname
    
    : main ( -- ) script get print ;
    
    MAIN: main
    

    Example:

    $ ./scriptname.factor 
    Program: ./scriptname.factor
    
    $ factor
    ( scratchpad ) USING: command-line ;
    ( scratchpad ) script get .
    f