[英]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.现在,当我运行它时,我使用
mix run --no-halt
并且它运行得很好。
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
.我不知道如何添加参数来
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.我想要做的就是将一个值传递给 start 方法并使用它来定义子进程。
mix
voluntarily resets System.argv/1
. mix
自动重置System.argv/1
。 The --no-halt
option is an ad-hoc way of running applications; --no-halt
选项是一种运行应用程序的特殊方式; normally we assemble releases with mix release
and start them normally with ebin/my_app start
.通常我们使用
mix release
组装发布,并使用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
,但创建空文件( mix
将尝试在启动时执行它,)并调用mix
像
mix run --no-halt -- "empty.exs" 42
Now inside your Application.start/2
you can get arguments with System.argv/0
现在在您的
Application.start/2
您可以使用System.argv/0
获取参数
def start(_type, args) do
IO.inspect(System.argv())
...
Check it.核实。
mix run --no-halt -- "empty.exs" 42
#⇒ ["422"]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.