Getting command line arguments in Common Lisp

http://cl-cookbook.sourceforge.net/os.html provides some insight

  (defun my-command-line ()
  (or 
   #+CLISP *args*
   #+SBCL *posix-argv*  
   #+LISPWORKS system:*line-arguments-list*
   #+CMU extensions:*command-line-words*
   nil))

is what you are looking for, I think.


A portable way is uiop:command-line-arguments (available in ASDF3, shipped by default in all major implementations).

Libraries-wise, there is the mentioned Clon library that abstracts mechanisms for each implementation, and now also the simpler unix-opts, and a tutorial on the Cookbook.

(ql:quickload "unix-opts")

(opts:define-opts
    (:name :help
       :description "print this help text"
       :short #\h
       :long "help")
    (:name :nb
       :description "here we want a number argument"
       :short #\n
       :long "nb"
       :arg-parser #'parse-integer) ;; <- takes an argument
    (:name :info
       :description "info"
       :short #\i
       :long "info"))

Then actual parsing is done with (opts:get-opts), which returns two values: the options, and the remaining free arguments.


Are you talking about Clisp or GCL? Seems like in GCL the command line arguments get passed in si::*command-args*.


I'm assuming that you are scripting with CLisp. You can create a file containing

#! /usr/local/bin/clisp
(format t "~&~S~&" *args*)

Make it executable by running

$ chmod 755 <filename>

Running it gives

$ ./<filename>
NIL
$ ./<filename> a b c
("a" "b" "c")
$ ./<filename> "a b c" 1 2 3
("a b c" "1" "2" "3")