簡體   English   中英

將 arguments 傳遞給 elisp 腳本。 再次

[英]Passing arguments to elisp script. Again

如何通過 arguments -q -d -Q -t -L -fg -bg --color 等?

執行類似emacs --script -Q <scriptname> <arguments>之類的操作肯定不會通過 arguments,它們在 emacs 中使用。 那么該怎么做呢?

根據您對 Rafael Ibraim 答案的評論,我添加了第二個答案,因為我認為我的第一個答案也誤解了您的問題(如果是這樣,您可能希望編輯問題以提供說明)。

您可以使用“空”參數的常用方法阻止 Emacs 處理命令行 arguments: --

所以如果你運行這個:

emacs --script (filename) -- -Q

Emacs 不會吃掉-Q參數(或--實際上),它可以用於您的腳本。 您可以使用以下腳本輕松驗證這一點:

(print argv)

如果您在腳本模式下運行 emacs,我建議在腳本末尾將“argv”變量重置為 nil,否則 emacs 將在腳本完成后嘗試解釋“argv”。

假設您有一個名為“test-emacs-script.el”的文件,其內容如下:

#!/usr/bin/emacs --script
(print argv)
(setq argv nil)

嘗試將此腳本作為“./test-emacs-script.el -a”運行。 如果您在不重置“argv”(腳本中的最后一行)的情況下運行此腳本,則 output 將是:

("-a")
Unknown option `-a'

重置“argv”擺脫“未知選項”錯誤消息

我認為您在這里有兩個選擇:

  • 創建你自己的論點,就像這里解釋的那樣。
  • 使用變量command-line-args 唯一的問題是這個變量可能不完全符合您的要求(來自手冊):

Most options specify how to initialize Emacs, or set parameters for the Emacs session. We call them initial options. A few options specify things to do, such as loading libraries or calling Lisp functions. These are called action options. These and file names together are called action arguments. The action arguments are stored as a list of strings in the variable command-line-args. (Actually, when Emacs starts up, command-line-args contains all the arguments passed from the command line; during initialization, the initial arguments are removed from this list when they are processed, leaving only the action arguments.)

換句話說,只有動作 arguments 將在command-line-args中可用,這對你沒有多大幫助。

如果您在 shell 手動鍵入命令,則應該沒有問題(除了在您的示例中您的腳本名稱是-Q ),因此我假設您正在嘗試使用 emacs 作為 shebang 命令創建可執行腳本?

這是我見過的創建可移植可執行 elisp 腳本的最佳解決方案,其中包括額外的 arguments:

Emacs shell 腳本 - 如何將初始選項放入腳本中?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM