[plt-scheme] how to call scheme program with arguments

From: Ryan Culpepper (ryanc at ccs.neu.edu)
Date: Sun Dec 28 05:29:34 EST 2008

Sigrid Keydana wrote:
> Hi,
> this feels like a very stupid question, but I'm not clear about how to
> call a scheme program from the command line with arguments. I did not
> really find much in the PLT reference, and from the mzscheme man page I
> seem to understand I have to have a  'main' function that will
> automatically be called with the command-line arguments; on the other
> hand these might be packed into an 'argv' variable, still there seems to
> be a 'current-command-line-arguments' function...

The new PLT Scheme Guide contains a section on writing Unix-style 
scripts, accessing command-line arguments, etc, if you are using PLT 
Scheme 4. Here are links to the appropriate sections of the Guide:

Creating Unix-style scripts:

The whole section on running programs, including other ways of doing it:

The Guide was written for PLT Scheme 4. If you are using an earlier 
version of PLT Scheme (like 371, say), much of what the Guide says does 
not apply, and the examples will not work. See more specific answers below.

> It would be great if someone could point out to me the "standard way" to
> do all this (perhaps with an example calling command line that also
> shows the necessary mzscheme options for this):
> - do I need the 'main' function as entry or are there other ways

No, you do not need a 'main' function. If you define a 'main' function, 
it is not called automatically when you run the script. You need to call 
it explicitly (example below).

> - how to get the arguments

Call the 'current-command-line-arguments' procedure. It returns a vector 
of strings (example below).

> - whether to just have a one-liner on the command line or a whole
> shell-script-with-embedded-scheme like the one in the mzscheme man page
> (which btw does not print anything to stdout when I try it...?):
> #! /bin/sh
> #|
> exec mzscheme -qr "$0" ${1+"$@"}
> |#
> (display "Hello, world!")
> (newline)

I just tried the script above using both PLT Scheme 371 and PLT Scheme, and it worked in both of those systems. I put the code above in 
a file called "test1.sh", made it executable ("chmod 755 test1.sh"), and 
ran it ("./test1.sh" from the directory containing the script). It 
printed out "Hello, world!" as expected.

The script above doesn't work for you? Does it print an error message 
instead, or does it terminate silently?

(Beware if you've named the script "test" that there is a standard Unix 
command named "test" that might be getting called instead.)


Here's a slightly larger example that demonstrates how to get the 
command-line arguments and how to define and call functions in the script:

   #! /bin/sh

   exec mzscheme -qr "$0" ${1+"$@"} 

   (display "Hello, world!")

   ;; main : (vectorof string) -> void
   ;; Print out args one per line (or usage note if none given)
   (define (main argv)
     (when (zero? (vector-length argv))
       (printf "please supply some arguments!\n")
       (exit 1))
     (for-each (lambda (arg) (printf "arg: ~s\n" arg))
               (vector->list argv)))

   (main (current-command-line-arguments))
   (printf "done!\n")

Note the explicit call to the 'main' function. BTW, the name of the 
function isn't important. There's no special significance to the name 


> _________________________________________________
>   For list-related administrative tasks:
>   http://list.cs.brown.edu/mailman/listinfo/plt-scheme

Posted on the users mailing list.