Getting command line arguments in Common Lisp
Asked Answered
I

7

23

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

Inkling answered 20/6, 2009 at 14:50 Comment(1)
For next googlers: there's the mentioned CLON library, now also the simpler unix-opts, and a tutorial.Yaupon
U
19

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")
Unintelligible answered 20/6, 2009 at 16:8 Comment(2)
the link in this post is no longer viable. :(Calva
this is super cool. but how can i make data from the strings? my function is used to inputs like this (unique '(a b c c d d d e f)) and it doesn't work with strings :O .. i tried to (mapcar #'make-symbol args) but that doesn't seem to do the jobHarim
P
27

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.

(edit): see the newer Cookbook: https://lispcookbook.github.io/cl-cookbook/scripting.html

Pinfish answered 20/6, 2009 at 15:25 Comment(3)
(or FOO nil) is equivalent to FOO, is it not?Cynth
Yes, but when you have your FOO conditionally read, it's, possibly, good to have a fall-back. But, then, the value of (or) is, not entirely surprising, NIL (just as the value of (and) is T).Duyne
For some reason this doesn't work for clisp for me: *** - OR: variable *ARGS* has no valueCrinose
U
19

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")
Unintelligible answered 20/6, 2009 at 16:8 Comment(2)
the link in this post is no longer viable. :(Calva
this is super cool. but how can i make data from the strings? my function is used to inputs like this (unique '(a b c c d d d e f)) and it doesn't work with strings :O .. i tried to (mapcar #'make-symbol args) but that doesn't seem to do the jobHarim
P
4

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

Philomena answered 20/6, 2009 at 17:23 Comment(0)
E
3

In SBCL,we can use sb-ext:*posix-argv* to get the argv from a Common Lisp script. sb-ext:*posix-argv* is a list holding all the arguments, the first member of the list is the script filename.

Elide answered 18/11, 2013 at 13:14 Comment(2)
I'm not sure this works anymore. I get the error message 'Symbol "POSIX-ARGV" not found in the SB-EXT package.'Ephrayim
should be sb-ext:*posix-argv*, in the old text, the *something* worked as a style. I'm sorry.Elide
Y
3

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.

Yaupon answered 19/6, 2018 at 15:27 Comment(0)
S
2

As seen in https://mcmap.net/q/556807/-getting-command-line-arguments-in-common-lisp, 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.

Seamanship answered 3/1, 2017 at 21:44 Comment(0)
C
0

Just go for it on SBCL (2.1.11.debian):

(defun main ()
    (let (
        (args sb-ext:*posix-argv*))

    (format t "args = ~a~%" args)))

(main)
Chromogen answered 15/1 at 1:10 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.