3.8 Using commandline arguments

A host program can also transfer an array of strings to the 4tH environment. Usually, commandline arguments will be transferred this way, although any string array with the correct format can be used. If so, you will probably find it in the documentation of the host program.

If you are familiar with C, the concept is probably quite easy to understand. There are two words, 'ARGN' and 'ARGS'. 'ARGN' will leave the number of commandline arguments on the stack. The commandline arguments itself are numbered from 0 to (ARGN - 1), e.g.

     argn 0>                         / test if there are
     if                              / any arguments
           argn 0 do                 / loop through them
                i args count type cr / print them

First, we test if there are any commandline arguments. Second, if that is the case we loop through them with 'ARGN' as upper limit. Why? Since "ARGN 1- ARGS" is always the last valid commandline argument!

Third, when 'ARGS' executes, it takes a number from the stack as index. Then it leaves the address of the Character Segment (where it is temporarily stored, usually PAD) on the stack.

Using the expression "COUNT TYPE CR" we can print that string. Because it is already stored in the Character Segment we can treat it like any other string. Remember, that if you don't save it anywhere else it won't last long!