开发者

How to pass arguments to saved SBCL core?

开发者 https://www.devze.com 2023-02-11 10:29 出处:网络
I have a Lisp (SBCL 1.0.40.0.debian) application (myfitnessdata), which uses the following code to process command line arguments:

I have a Lisp (SBCL 1.0.40.0.debian) application (myfitnessdata), which uses the following code to process command line arguments:

(:use :common-lisp)
(:export #:main))

(in-package :myfitnessdata)

(require :sb-posix)

;; snip

(开发者_运维技巧defun main ()
  (if (= (length sb-ext:*posix-argv*) 3)
    (let ((username (nth 0 sb-ext:*posix-argv*))
      (password (nth 1 sb-ext:*posix-argv*))
      (path (nth 2 sb-ext:*posix-argv*)))
      (scrape (username password path)))
    (show-usage)))

I am compiling this application using the following Lisp code:

(load "myfitnessdata.lisp")
(save-lisp-and-die "myfitnessdata.bin" :executable t :toplevel 'myfitnessdata:main)

This produces an executable that I can run. However, the sb-ext:*posix-argv* list is always empty. If I run the following:

./myfitnessdata.bin myusername mypassword /home/me/data

... then all I get is the usage instructions produced by (show-usage).

I think I must be missing something simple again - could someone please let me know how to (save-lisp-and-die) such that the resultant executable takes command line arguments?


The solution, as suggested by the good folks on sbcl-help, is to use a tool like buildapp to compile the app. You can specify an entry point, which is assumed to be a function with one argument. At run-time, that function is called with a list of command-line parameters.

Using buildapp, my main function is now:

(defun main (args)
  (if (= (length args) 4)
      (let ((username (nth 1 args))
           (password (nth 2 args))
           (path (nth 3 args)))
    (scrape username password path))
    (show-usage)))
0

精彩评论

暂无评论...
验证码 换一张
取 消