简体   繁体   中英

How to pass command line arguements to mix run --no-halt

So I have an Application module which follows this layout:

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

Now when i run this i use mix run --no-halt and it runs perfectly.

I want to replace the "argument" with a value that I pass in the command line? I cannot figure out how do I add arguments to mix run --no-halt .

All I want to do is pass a value to the start method and use it to define the child process.

mix voluntarily resets System.argv/1 . The --no-halt option is an ad-hoc way of running applications; normally we assemble releases with mix release and start them normally with ebin/my_app start .

While you still want to resort to mix run --no-halt , create the empty file ( mix will attempt to execute it upon start,) and call mix like

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

Now inside your Application.start/2 you can get arguments with System.argv/0

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

  ...

Check it.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM