首页 > 解决方案 > 如何通过命令行参数来混合运行 --no-halt

问题描述

所以我有一个遵循这个布局的应用程序模块:

defmodule Project.Application do


  use Application

  def start(_type, _args) do
    children = [
      randomchild1,
      randomchild2,
      {Project.runapp, "argument" }
    ]

    opts = [strategy: :one_for_all, name: Project.Supervisor]
    Supervisor.start_link(children, opts)
  end
end

现在,当我运行它时,我使用mix run --no-halt它并且它运行完美。

我想用我在命令行中传递的值替换“参数”?我不知道如何向mix run --no-halt.

我要做的就是将一个值传递给 start 方法并使用它来定义子进程。

标签: elixirelixir-mixerlang-supervisorelixir-iex

解决方案


mix 自愿重置 System.argv/1。该--no-halt选项是一种运行应用程序的临时方式;通常我们用 组装发布mix release并正常启动它们ebin/my_app start

当您仍然想求助时mix run --no-halt,创建空文件(mix将尝试在启动时执行它)并mix调用

mix run --no-halt -- "empty.exs" 42

现在在你的内部Application.start/2你可以得到参数System.argv/0

def start(_type, args) do
  IO.inspect(System.argv())

  ...

核实。

mix run --no-halt -- "empty.exs" 42
#⇒ ["422"]    

推荐阅读