[racket] command prompt

From: Ryan Culpepper (ryanc at ccs.neu.edu)
Date: Mon Sep 13 04:40:02 EDT 2010

Isaiah Gilliland wrote:
> I've been trying to figure out how to create a cli app. My first issue is to
> create a prompt for the user to enter text so I can use  what they enter.
> I've somewhat found out how to make outside commands to other programs, but
> I've ben scouring the documentation for some time and I can't find what I'm
> looking for. Is there anyone out there with an example I can use or the
> right documentation I've missed. Loving Racket alot, take care.

The Racket Guide has a section on "Input and Output". It doesn't have 
the example you're looking for, but it has useful background info.


When you run a command-line Racket program, (current-input-port) gets 
input from the terminal. You can use display or printf to emit a prompt, 
then use read-line to get the user input. Here's a little example. Save 
it in a file and then run "racket <filename>" in the shell.

   #lang racket
   (define (loop counter)
     (printf "~a# " counter)
     (let ([command (read-line (current-input-port) 'any)])
       (cond [(or (eof-object? command) (equal? command "quit"))
              (printf "Good-bye!\n")]
             [(string? command)
              (printf "You said, ~s\n" command)
              (printf "Say quit to end.\n")
              (loop (add1 counter))])))
   (loop 1)

To parse the commands, you can use regular expressions (eg regexp-match) 
or an LALR(1) grammar (search for "parser tools").


Posted on the users mailing list.