开发者

Processing command-line-args-left?

开发者 https://www.devze.com 2022-12-19 23:57 出处:网络
I have a function in which I pass a filename followed by several integer parameters.The problem is that I now want to run my code as a Unix script, using command-line-args-left to pass parameters from

I have a function in which I pass a filename followed by several integer parameters. The problem is that I now want to run my code as a Unix script, using command-line-args-left to pass parameters from the command line. When #1 calls process-args, a list is created with all of the values. In #2, a list of a list {eg. ((1 2 3)) } is created upon entry to process-args. What is the best way to keep my code generic so I can handle both cases #1 and #2 in the same function?

(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

Here is some working sample code with which I'm testing:

#!/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))


You could try using 'apply to flatten out the last argument (the last argument to 'apply' is a list of arguments, use'funcall` if you don't have the last bit in a list).

So, version #3 above would be handled so:

(apply 'process-args (car command-line-args-left) '(1 2 3))

(The other invocations wouldn't change.)

0

精彩评论

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

关注公众号