ExpandCollapsePrev Next Index

+ 3.1 Command line arguments

Another way to change the name that displays on our hello world program is to use command line arguments. These arguments are supplied when the program is run from the command line, as follows:

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 Joachim will be welcomed (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 (or System::argv 0) always contains the executable name. But if you try this you'll notice something funny. System::argv 0 does not include the flx driver. This happens because after compiling the program, flx is out of the picture, it's just your app that runs.