Emacs呼叫过程偶然发现了提供的ARGS内容

发布于 2025-01-30 06:19:34 字数 1187 浏览 2 评论 0原文

我对呼叫程序通话的意外行为感到困惑。

这是一个小功能:

(defun work-in-progress()
    "run ledger report and output result in current buffer.
The intention is to run following command (works perfectly from shell):
`ledger -U csv --sort date --file ledger-file child`
where ledger-file is a known and defined variable.
The problem is that the command fails with:
`Error: Illegal option -`
"
    (interactive)
    (cd ledger-dir)
    (setq child "peter")
    (setq parms "-U csv --sort date --file")
    (setq prog "ledger") ; should be found via path variable or so
    (call-process prog nil t t parms ledger-file child)
  )

我已经使用了Ledger命令选项的顺序,但是Emacs似乎总是抱怨PARMS变量中的第一个选项或所有选项:

例如

(setq parms "--sort date -U csv --file")

中的ISO结果

Error: Illegal option --sort date -U csv --file

,ISO

Error: Illegal option - 

LEDGER CLI程序并不挑剔参数序列,两个描述的选项序列在命令行中调用Ledger时都可以很好地工作。

这确实使我感到困惑。文档读取

call-process program &optional infile destination display &rest args

并设置为nil,目的地以及显示为t,那么为什么不掌握args varible的内容?

任何帮助,更正和/或建议都将是真诚地赞赏的!

I'm puzzled by unexpected behavior of a call-process call.

Here's a little function:

(defun work-in-progress()
    "run ledger report and output result in current buffer.
The intention is to run following command (works perfectly from shell):
`ledger -U csv --sort date --file ledger-file child`
where ledger-file is a known and defined variable.
The problem is that the command fails with:
`Error: Illegal option -`
"
    (interactive)
    (cd ledger-dir)
    (setq child "peter")
    (setq parms "-U csv --sort date --file")
    (setq prog "ledger") ; should be found via path variable or so
    (call-process prog nil t t parms ledger-file child)
  )

I've played around with the sequence of the ledger command options, but emacs always seems to complain about the first option or all options in the parms variable:

e.g.

(setq parms "--sort date -U csv --file")

results in

Error: Illegal option --sort date -U csv --file

iso

Error: Illegal option - 

The ledger cli program isn't fussy about arguments sequence, both described option sequences work perfectly well when calling ledger at the command line.

This truly puzzles me. The documentation reads

call-process program &optional infile destination display &rest args

and infile is set to nil, destination as well as display are t, so why doesn'it grok the content of args variable?

Any help, correction and/or suggestion would be sincerely appreciated!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

抚笙 2025-02-06 06:19:34

列表的尾巴应为字符串序列,每个字符串与一个参数相对应。

    (setq parms '("-U" "csv" "--sort" "date" "--file"))
    (setq prog "ledger")
    (apply #'call-process prog nil t t (append parms '(ledger-file child)))

您需要应用附加的结果延续到静态参数列表中,以call-process

我必须猜测什么LEDGER-FILEFILEChild是;如果它们不是字符串,则需要将它们转换为字符串。

为了简要概括shell如何解析参数,命令行

echo foo "bar $baz" 'quux $baz'

被转换为字符串数组

'("echo" "foo" "bar <<value of baz>>" "quux $baz")

(使用lispy符号)并将其作为参数传递给execlp

The tail of the list should be a sequence of strings, each corresponding to one argument.

    (setq parms '("-U" "csv" "--sort" "date" "--file"))
    (setq prog "ledger")
    (apply #'call-process prog nil t t (append parms '(ledger-file child)))

You need apply to make the result of append into a continuation of the static list of arguments to call-process.

I had to guess what ledger-file and child are; if they are not strings, you need to convert them to strings.

To briefly recapitulate how the shell parses arguments, the command line

echo foo "bar $baz" 'quux $baz'

gets turned into the string array

'("echo" "foo" "bar <<value of baz>>" "quux $baz")

(to use a Lispy notation) and passed as arguments to execlp.

欢你一世 2025-02-06 06:19:34

解决方案是将每个参数作为单独的字符串传递:

(defun work-in-progress()
    "run ledger report and output result in current buffer. "
    (interactive)
    (cd ledger-dir)
    (setq child "peter")
    (setq prog "ledger") ; should be found via path variable or so
    (call-process prog nil t t "--sort" "date" "-U" "csv" "--file" ledger-file child)
)

The solution is to pass each parameter as a separate string:

(defun work-in-progress()
    "run ledger report and output result in current buffer. "
    (interactive)
    (cd ledger-dir)
    (setq child "peter")
    (setq prog "ledger") ; should be found via path variable or so
    (call-process prog nil t t "--sort" "date" "-U" "csv" "--file" ledger-file child)
)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文