在 Elixir 中,您可以使用 iex
shell 与您的应用程序的上下文使用以下命令...
iex -S mix
这个命令非常方便,让我们可以在交互式 shell 中使用应用程序功能。
我想知道是否可以使用参数启动交互式 shell,或者是否可以让您的应用程序在用户与 shell 交互之前调用一些函数。
例如,假设我有一个名为
MyApp
的应用程序。我从 iex -S mix
开始.不必手动将命令输入 shell,例如iex()> import MyApp.Module
iex()> alias MyApp.Module.OtheName
iex()> value = OtherName.function(1234)
有一种方法可以让这种情况自动发生。
最佳答案
您可以创建文件 .iex.exs
在您的应用程序目录中,包含要在启动时运行的代码。来自 iex
的手册页:
After iex starts, it seeks the file .iex.exs and, in a case of success, executes the code from the file in the context of the shell. At first the search starts in the current working directory, then, if necessary, it continues in the home directory.
但请注意,如果您将特定于应用程序的代码放在此文件中,则只有在您执行
iex -S mix
时它才会正确运行。 ,但当你只执行 iex
时不会.
关于elixir - 是否可以启动交互式 elixir shell 并立即调用函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55559141/