drarkayl
drarkayl

Reputation: 1017

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.

Upvotes: 4

Views: 2338

Answers (1)

Aleksei Matiushkin
Aleksei Matiushkin

Reputation: 121010

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"]    

Upvotes: 6

Related Questions