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"]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With