【问题标题】:Passing arguments to elisp script. Again将参数传递给 elisp 脚本。再次
【发布时间】:2011-10-09 05:33:18
【问题描述】:

如何传递参数 -q -d -Q -t -L -fg -bg --color 等?

执行类似emacs --script -Q <scriptname> <arguments> 的操作不会传递参数,这些参数在 emacs 中使用。那么该怎么做呢?

【问题讨论】:

    标签: emacs elisp


    【解决方案1】:

    根据您对 Rafael Ibraim 的回答comment,我添加了第二个答案,因为我认为我的第一个答案也误解了您的问题(如果是这样,您可能希望编辑问题以提供说明)。

    您可以使用“空”参数的常用方法来阻止 Emacs 处理命令行参数:--

    所以如果你运行这个:

    emacs --script (filename) -- -Q

    Emacs 不会吃掉-Q 参数(或者实际上是--),它可以用于您的脚本。您可以使用以下脚本轻松验证这一点:

    (print argv)
    

    【讨论】:

    • 谢谢!谢谢!这正是我想要的。现在我写了":"; exec emacs --no-site-file --script "$0" -- "$@" # -*-emacs-lisp-*-,所以这件事做得很好。再次感谢。
    • 事实证明,您需要这个和 Jurijs Oniscuks 的答案。需要-- 以防止 Emacs 在脚本执行之前处理 option 参数,而在脚本退出之前需要(setq argv nil) 以防止 Emacs 处理 非选项 参数作为文件名,and visiting them.
    • 实际上,比清除argv 更好的是在脚本末尾使用(kill-emacs 0) 显式退出(假设0 是此时的适当状态),这确保(a ) Emacs 不做任何其他事情(处理剩余的参数,或其他); (b) 你保证你的脚本返回哪个退出状态(这是你在任何情况下都应该做的事情)。参考lunaryorn.com/2014/08/12/emacs-script-pitfalls.html
    【解决方案2】:

    如果您在脚本模式下运行 emacs,我建议您在脚本末尾将“argv”变量重置为 nil,否则 emacs 将在脚本完成后尝试解释“argv”。

    假设您有一个名为“test-emacs-script.el”的文件,其内容如下:

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

    尝试将此脚本作为“./test-emacs-script.el -a”运行。如果您在不重置“argv”(脚本中的最后一行)的情况下运行此脚本,则输出将是:

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

    重置“argv”会消除“未知选项”错误消息

    【讨论】:

    • 这里的问题是 Emacs 仍然会在它们到达你的脚本之前吃掉它识别的参数。例如,./test-emacs-script.el -Q 示例的输出为 nil。使用 -- 参数的技巧可以避免该问题以及您正在解决的问题,因此我相信只有在您实际上不希望这些参数到达您的脚本时才推荐您的方法。
    • 事实证明这两种方法都需要。需要-- 以防止 Emacs 在脚本执行之前处理 option 参数,而在脚本退出之前需要(setq argv nil) 以防止 Emacs 处理 non-option 参数作为文件名,and visiting them.
    • 实际上,比清除argv 更好的是在脚本末尾使用(kill-emacs 0) 显式退出(假设0 是此时的适当状态),这确保(a ) Emacs 不做任何其他事情(处理剩余的参数,或其他); (b) 您保证您的脚本返回哪个退出状态(这是您在任何情况下都应该做的事情)。参考lunaryorn.com/2014/08/12/emacs-script-pitfalls.html
    • 在运行 Emacs 28 的 2021 年,这似乎不再是真的了。
    【解决方案3】:

    我认为你有两个选择:

    • 按照here 的解释创建您自己的论点。
    • 使用变量command-line-args。唯一的问题是这个变量可能不完全符合您的要求(来自manual):

    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.)

    换句话说,command-line-args 中只有动作参数可用,这对你没有多大帮助。

    【讨论】:

    • 我对向 Emacs 传递参数不感兴趣。我对不将其传递给 emacs 而是将其传递给 emacs 脚本很感兴趣。并且当使用“emacs --script ”调用emacs脚本时,并且是某种东西,emacs中使用的东西(例如--color),脚本不能按预期工作(预期将 --color 传递给脚本)。
    【解决方案4】:

    如果您在 shell 中手动输入命令应该没有问题(除了在您的示例中您的脚本名称是 -Q),所以我假设您正在尝试使用 emacs 创建可执行脚本作为 shebang 命令?

    这是我见过的用于创建包含附加参数的可移植可执行 elisp 脚本的最佳解决方案:

    Emacs shell scripts - how to put initial options into the script?

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-03-15
      • 2019-11-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多