with-command-line-arguments

Macro

Package: system

Arguments: (optstring &rest varlist) (restvar &key command-line-arguments usage) &body body

This macro makes the task of parsing command line arguments easier, in the style similar to the UNIX getopt(3) and getopt (for use in shell scripts).

The main task of with-command-line-arguments is to bind LISP variables to the given command line arguments. optstring is a getopt(3) style option string describing the expected options. varlist is a list of symbols to which values are bound -- the order of the options in optstring is the order in which the variables are bound. The keyword command-line-arguments is used to initialize the command line arguments -- if not given, then the function command-line-arguments is used to retrieve the actual command line arguments given to the invoked image.

usage, if specified, should be a string. This string is printed when errors are found in the given command line arguments (if, for example, they do not match the specification in optstring).

body is a list of forms.

For example,

(with-command-line-arguments 
  ("o:bc" output-file bflag cflag) 
  (rest) ...) 

will bind output-file to the argument following -o, bflag to t or nil depending on whether or not -b is present, and cflag to t or nil depending on whether or not -c is present.

Command line arguments, when given to a vanilla LISP are not all that interesting. When developing an application, however, there are often many uses for command line arguments.

EXAMPLES

;; If the form below is invoked in an image with a restart function, it 
;; would view the command line in this way: 1) -m binds the variable 
;; mail, 2) -r binds the variable real, 3) -d causes *debug* to be set 
;; and a break loop entered rather than calling the function 
;; SCHEDULE, and 4) if no arguments are given, then it is assumed that 
;; debugging is desired and a break loop is entered in this case as well.  
;; 
(sys:with-command-line-arguments
  ("mrd" mail real debug) 
  (arguments) ;; don't bind it, because of multiprocessing...  
  (setq *debug* debug) 
  (when (cdr arguments) 
    (bye 1 "extra arguments:~{ ~a~}" (cdr arguments))) 
  (when (null arguments) 
    (setq *debug* t) 
    (break "debug away...")) 
    (schedule (car arguments) :real real :mail mail) 
    (bye))

The documentation is described in introduction.htm and the index is in index.htm.

Copyright (c) 1998-2000, Franz Inc. Berkeley, CA., USA. All rights reserved.

Created 2000.10.5.