22

Common Lisp (違いがある場合は特に GNU) でコマンドライン引数を取得するにはどうすればよいですか?

4

6 に答える 6

26

http://cl-cookbook.sourceforge.net/os.htmlは洞察を提供します

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

あなたが探しているものだと思います。

于 2009-06-20T15:25:46.983 に答える
19

CLispを使用してスクリプトを作成していることを前提としています。を含むファイルを作成できます

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

実行して実行可能にする

$ chmod 755 <filename>

それを実行すると

$ ./<filename>
NIL
$ ./<filename> a b c
("a" "b" "c")
$ ./<filename> "a b c" 1 2 3
("a b c" "1" "2" "3")
于 2009-06-20T16:08:57.053 に答える
3

移植可能な方法がuiop:command-line-argumentsあります (ASDF3 で利用可能で、すべての主要な実装でデフォルトで出荷されます)。

ライブラリに関しては、各実装のメカニズムを抽象化する前述の Clon ライブラリと、より単純なunix-opts、およびクックブックのチュートリアルがあります。

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

次に、実際の解析が で行われ(opts:get-opts)、オプションと残りの自由引数の 2 つの値が返されます。

于 2018-06-19T15:27:34.137 に答える
1

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.

于 2017-01-03T21:44:11.500 に答える