【问题标题】:How to pass command line arguements to mix run --no-halt如何传递命令行参数以混合运行--no-halt
【发布时间】:2020-01-09 23:38:31
【问题描述】:

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

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 方法并使用它来定义子进程。

【问题讨论】:

标签: elixir elixir-mix erlang-supervisor elixir-iex


【解决方案1】:

mixvoluntarily resetsSystem.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"]    

【讨论】:

    猜你喜欢
    • 2016-03-25
    • 1970-01-01
    • 2018-03-28
    • 2017-08-17
    • 1970-01-01
    • 1970-01-01
    • 2012-04-23
    • 1970-01-01
    • 2011-12-18
    相关资源
    最近更新 更多