gpt4 book ai didi

erlang - 尝试指定 :env in Elixir Port. 打开时出现参数错误

转载 作者:行者123 更新时间:2023-12-02 08:12:30 24 4
gpt4 key购买 nike

我尝试在使用 Port.open 运行 shell 命令时设置环境变量。无论我传入什么,它都会引发错误 ArgumentError

port = Port.open({ :spawn_executable, "/usr/local/bin/cool" },
[:stream, :in, :binary, :eof, :hide,
{:env, [{"COOL_ENV", "cool"}]},
{ :args, ["test.js"] }])

根据 Port.open 的 Elixir 文档,它应该接受 Erlang 函数 :erlang.open_port 接受的任何参数。 Erlang 文档提到:

{env, Env} Only valid for {spawn, Command}, and {spawn_executable, FileName}. The environment of the started process is extended using the environment specifications in Env.

Env is to be a list of tuples {Name, Val}, where Name is the name of an environment variable, and Val is the value it is to have in the spawned port process. Both Name and Val must be strings. The one exception is Val being the atom false (in analogy with os:getenv/1, which removes the environment variable. (http://erlang.org/doc/man/erlang.html#open_port-2)

即使我尝试像这样直接调用 erlang 函数也会出现此错误:

:erlang.open_port({:spawn_executable, "/usr/local/bin/cool"}, [:stream, :in, :binary, :eof, :hide, {:env, [{"COOL", "cool"}] },{:args, ["test.js"]}])

如果我删除 {:env, [{...}] 参数,错误就会消失。

最佳答案

:env 选项要求名称和值都是 Erlang 字符串,这在 Elixir 中称为 charlist,并且使用单引号而不是双引号构造。

Opt =
...
{env, Env :: [{Name :: string(), Val :: string() | false}]} |
...

Source

以下应修复错误:

port = Port.open({ :spawn_executable, "/usr/local/bin/cool" },
[:stream, :in, :binary, :eof, :hide,
{:env, [{'COOL_ENV', 'cool'}]},
{ :args, ["test.js"] }])

:args 接受 Erlang 字符串和 Erlang 二进制文件(Elixir 字符串),因此您无需更改它。

关于erlang - 尝试指定 :env in Elixir Port. 打开时出现参数错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45222418/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com