我有一个函数,我传递一个文件名后跟几个整数参数。问题是我现在想要将我的代码作为Unix脚本运行,使用command-line-args-left从命令行传递参数。当#1 调用process-args时,会创建一个包含所有值的列表。在#2 中,列表列表{例如。 ((1 2 3))}是在进入process-args时创建的。保持我的代码通用的最佳方法是什么,以便我可以在同一个函数中处理#1 和#2 两种情况?
(defun process-args (filename &rest cols) ...) (process-args filename 1 2 3); #1 (process-args (car command-line-args-left) (cdr command-line-args-left)); #2
以下是我正在测试的一些工作示例代码:
#!/usr/bin/emacs --script (defun process-args (filename &rest cols) (princ (concat "Script Name: " file "\n")) (princ (concat "File parameter: " filename "\n")) (princ "Other arg values: ") (princ cols) (princ "\nIs list: ") (princ (listp cols)) (princ "\n----------\n") (while cols (princ (car cols)) (princ "...") (setq cols (cdr cols))) (princ "\n")) (print "===== Version #1: Base case - becomes (1 2 3) =====") (process-args (car command-line-args-left) 1 2 3) (print "===== Version #2: Passing cdr of list as one string =====") (process-args (car command-line-args-left) (mapconcat 'identity (cdr command-line-args-left) " ")); (print "===== Version #3: Test of list of list - becomes ((1 2 3)) =====") (process-args (car command-line-args-left) '(1 2 3))
答案 0 :(得分:0)
如果您没有列表中的最后一位,您可以尝试使用'apply
展平最后一个参数('apply' is a list of arguments, use
'funcall`的最后一个参数。)
因此,上面的版本#3将被处理:
(apply 'process-args (car command-line-args-left) '(1 2 3))
(其他调用不会改变。)