Navigation:  Programming Cookbook > Creating Console Applications >

Accessing Command Line Arguments

Previous pageReturn to chapter overviewNext page

The parameters passed to a console application on the command line can be accessed by sending the message #argv to the session manager. This answers an array of strings in the same form as the argv argument to C's main() function. There is also an #argc message to independently access the number of arguments available.

As an example we might write an equivalent of the DOS type (or Unix cat) utility as follows:


  "Dump the contents of all the files listed as arguments to stdout"

  | args |

  args := self argv.

  args from: 2 to: args size do: [:f | | in |

      in := FileStream read: f.

      [in atEnd] whileFalse: [

          self stdout nextPutAll: (in nextAvailable: 1024)]].

  self stdout flush


Note: We ignore the first argument, since this will be the name of the executable.

Access to the raw command line is also possible by sending the #getCommandLine message to the session manager.