Question

How can I get the command line arguments in (specifically in GNU, if there are any differences) Common Lisp?

Was it helpful?

Solution

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")

OTHER TIPS

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.

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

In SBCL,we can use sb-ext:*posix-argv* to get the argv from common lisp script. The sb-ext:*posix-argv* is a list hold all arguments, the first arg is the script filname.

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.

As seen in https://stackoverflow.com/a/1021843/31615, each implementation has its own mechanism. The usual way to deal with this is to use a wrapper library that presents a unified interface to you.

Such a library can provide further assistance in not only reading things in, but also converting them and giving helpful output to the user. A quite complete package is CLON (not to be confused with CLON or CLON, sorry), the Command Line Options Nuker, which also brings extensive documentation. There are others, though, should your needs be more lightweight, for example, command-line-arguments and apply-argv.

The packages in quicklisp for these are named net.didierverna.clon, command-line-arguments, and apply-argv, respectively.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top