# command_line

Definition: | sequence res = command_line(bool bRaw=false)
Description: | Returns a sequence of strings, where each string is a word from the command-line that started your program. The first word will be the path to either the Phix executable (p.exe, pw.exe, or p), or to your executable file, or "p2js". The next word is either the name of your Phix main source/htm file, or (again) the path to your executable file. After that will come any extra words typed by the user. You can use these words in your program.
pwa/p2js: | Supported, after a fashion, see technicalia (ditto bRaw).
Comments: | The Phix interpreter itself uses very few command-line options, which must occur before the main source file. Options after the main source file can be used by your own program. The user can put quotes around a series of words to make them into a single argument. If you compile your program you will find that all command-line arguments remain the same, except for the first two, even though your user no longer types "p" on the command-line (see examples below). In the special case of "p p test" (and "p p p test" etc) command_line() returns the same results as "p test", since doing otherwise would just hamper testing, cause general confusion, and gain nothing.
Example 1: |
    -- The user types:  p myprog myfile.dat 12345 "The end"
    cmd = command_line()
    -- cmd will be:
          {`C:\Program Files (x86)\Phix\p.exe`,
           `C:\Projects\myprog\myprog.exw`,
           `myfile.dat`,
           `12345`,
           `The end`}
    -- Note the capitalisation of myprog[.exw] may reflect
    -- reality, whereas myfile would always be "as typed".
Example 2: |
    -- Your program is compiled with the name "myprog.exe"
    -- and is stored in the directory C:\MyFiles\.
    -- The user types:  myprog myfile.dat 12345 "The end"
    cmd = command_line()
    -- cmd will be:
           {`C:\MyFiles\myprog.exe`,
            `C:\MyFiles\myprog.exe`, -- (spacer)
            `myfile.dat`,
            `12345`,
            `The end`
            }
    -- Note that all arguments remain the same as example 1
    -- except for the first two. The second argument is always
    -- the same as the first and is inserted to keep the numbering
    -- of the subsequent arguments the same, whether your program
    -- is compiled to an executable .exe file or not.
Example 3: |
    string cl2 = command_line()[2]
    ?get_file_name(cl2) -- eg "test.exw" or "test.exe" or "test.htm"
Implementation: | See builtins\VM\pcmdlnN.e (an autoinclude), and/or pwa\p2js.js (ie core), for details of the actual implementation.
See Also: | getenv, get_interpreter
Technicalia | Implemented in pwa/p2js as return {"p2js",document.location.href} - and never anything extra. Expect res[2] to be something like `file:///C:/Program%20Files%20(x86)/Phix/pwa/test.htm` or `http://phix.x10.mx/p2js/test.htm`. There may be cases where the simplest way to effect a config or functional change is to simply rename a web page, or perhaps copy the exact same code to different names, so for instance `http://phix.x10.mx/login.htm` and `http://phix.x10.mx/logout.htm` might do different things, but could actually be identical. Should the url contain a scrap such as "?action=login", that is nothing whatsoever to do with me or command_line(), and yours alone to deal with, at least as things stand. Normally command_line() takes great pains to ensure the first two elements are delivered consistently and fully canonicalised. The bRaw parameter allows you to obtain whatever the user actually typed which would normally be much trickier to deal with, for instance ../teST\..\pW.eXe - and apart from that throwaway quip I shall make no promises about what else it may or may not do. One specific case is that test_summary() wants to check for a "-nopause" option, which is suppressed in the normal command_line() output, before it displays a prompt and pauses. Of course there is no hitch of any kind in the same program calling command_line() both ways. Has no effect whatsoever under p2js.