Smalltalk, take command line argument as filename

474 Views Asked by At

I am new to Smalltalk and am trying to figure out how to take in a command line argument as a filename. I've seen this following snippet online:

f := FileStream open: 'fileName' mode: FileStream read

But I want to know how to modify that based on a user's command line input for the fileName. The following is how this project would be compiled and ran:

gst file1.st file2.st file3.st file4.st -f mainFile.st readThisFile.dat addiotnalArg

So how can I pull the name of the user specified file in Smalltalk?

1

There are 1 best solutions below

0
JayK On

According to https://www.gnu.org/software/smalltalk/manual/gst.html#Invocation you can access the command line arguments that are not interpreted by GNU Smalltalk itself with Smalltalk arguments. It will be an array that contains those arguments as Strings.

-a
--smalltalk-args

Treat all options afterward as arguments to be given to Smalltalk code retrievable with Smalltalk arguments, ignoring them as arguments to GNU Smalltalk itself. [...]

-f
--file

The following two command lines are equivalent:

gst -f file args...
gst -q file -a args...

Since your readThisFile.dat is the first argument, you can access it with Smalltalk arguments at: 1 and put that into your FileStream constructor message:

f := FileStream open: (Smalltalk arguments at: 1) mode: FileStream read

You can also use first

f := FileStream open: Smalltalk arguments first mode: FileStream read.