繁体   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