3.1 Command line arguments
Another way to change the name that displays on our hello world program is to use command line arguments. Instead of providing the data ourselves, we can rely on the user supplying it when the program runs from the command line. The following shows what the user could enter:
flx hello v1 v2 v3 v4
With v1
having an index of 1, v2
of 2, and so on, counting up by one.
We can grab these values by passing the index value (position) into System::argv
as we see here:
println$ "Hello " + System::argv 1;
This reads the first command line argument (index 1), and returns it to the caller. For example if you type:
flx hello Joachim
then the program houtputs the text (i.e. "Hello Joachim").
-
The function
System::argv
applied to an integer n returns the n'th command line argument. If the argument exists it will return it (as a string), or an empty string otherwise. -
Note: In Felix, indexes start at 0 (just like C++, Java, and other Algol derived languages).
Index 0 of
argv
(orSystem::argv 0
) always contains the executable name. But if you try this you'll notice something funny.System::argv 0
does not include theflx
driver. This happens because after compiling the program,flx
is out of the picture, it's just your app that runs.