RobStallion
RobStallion

Reputation: 1703

Is it possible to launch an interactive elixir shell and call a function immediately?

In elixir you can use the iex shell with the context of your application with the following command...

iex -S mix

This command is super handy and lets us use an application functions in the interactive shell.

I am wondering if it is possible to start the interactive shell with arguments, or if it is possible to have your application call some functions before the user can interact with the shell.

For example, let's say I have an application called MyApp and I start it with iex -S mix. Instead of having to manually enter commands into the shell, e.g.

iex()> import MyApp.Module

iex()> alias MyApp.Module.OtheName

iex()> value = OtherName.function(1234)

there would be a way to have this happen automatically.

Upvotes: 4

Views: 6132

Answers (1)

zwippie
zwippie

Reputation: 15515

You can create a file .iex.exs in your application directory with the code to run at startup. From the man page of 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.

But beware that if you put application specific code in this file, it will only run correct if you execute iex -S mix, but not when you execute just iex.

Upvotes: 6

Related Questions