smon

smon

What is the correct way to pass command line options?

I tried something like this in my mix.exs:

def application do
  [
    applications: [:logger, :httpoison, :poison],
    mod: {Test, []}
  ]
end

In my Test.ex:

defmodule Test do
  use Application

  def start(_mode, _args) do
    System.argv
    |> Test.CLI.main
  end
end

But how do I pass arguments to the start function without “confusing” the mix run script? If I try to mix run --type marc, I get

** (Mix) Could not invoke task "run": 1 error found! --type : Unknown option

Ok, I kind of get that. But then I noticed that if I add some random argument in between like mix run what --type marc, it kind of works: The what gets lost on the way, meaning it is missing from System.argv, which leaves ["--type", "marc"].

You may have noticed: I am somewhat confused. :wink:

What is the correct way to do this besides turning to escript?

Marked As Solved

benwilson512

benwilson512

Author of Craft GraphQL APIs in Elixir with Absinthe

It sounds like you want a mix task or an escript.

Also Liked

sger

sger

Hello,

You need escript for this job for example in your mix.exs you must pass

escript: [main_module: MyApp]

then in your module define something like this:

defmodule MyApp do
 def main(args) do
  args |> parse_args |> process
 end

def process([]) do
 IO.puts "No arguments given"
end

def process(options) do
 IO.puts "Hello #{options[:name]}"
end

 defp parse_args(args) do
  {options, _, _} = OptionParser.parse(args, switches: [name: :string])
  options
 end
end

finally from the command line:

mix escript.build
$ ./my_app --name=Elixir
Oliver

Oliver

Hello.

I was looking for a thread similar to my original post which I can no longer reply to (Google group got closed): Redirecting to Google Groups

My original solution was indeed based on escript, but in order to enable “-mode embedded” for code preloading I had to switch away from it and over to building my software with distillery.

distillery builds you scripts to start your application like this: bin/myapp foreground

There is no escript involved.

And I found a solution for commandline arguments: init — OTP 29.0.2 (erts 17.0.2)

I filtered, joined, and processed the results and I had all the commandline arguments I needed. Finally solved my problem with commandline arguments.

My final commandline looks like this: bin/myapp foreground --config "something". (You have other options to start the application, of course, like detached or with an attached console.) I then put the parsed results in my applications environment with Application.put_env(:myapp, <key, <value>) and retrieve them later when needed easily.

smon

smon

My idea was to avoid the mix escript.build step. Looking at mix tasks, it seems like I would have to start all dependencies manually - so maybe an escript is still the more elegant solution. Thank you for your example!

Where Next?

Popular in Questions Top

sergio
In Ruby, I can go: User.find_by(email: "foobar@email.com").update(email: "hello@email.com") How can I do something similar in Elixir? ...
New
hariharasudhan94
lets say i have a sample like a = 20; b = 10; if (a &gt; b) do {:ok, "a"} end if (a &lt; b) do {:ok, b} end if (a == b) do {:ok, "equa...
New
New
Emily
I have VueJS GUIs with the project generated using Webpack. I have Elixir modules that will need to be used by the VueJS GUIs. I forese...
New
electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID&lt;0.412.0&gt; terminating ** (Postgrex.Error) FATAL...
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New

Other popular topics Top

malloryerik
Hi, this is for people who, like me, have had some friction using .html.heex templates in VSCode. The solution seems to be, in a hyphena...
New
TunkShif
This post is an instruction guide to help you setup your Neovim for Elixir development from scratch. It includes general information on h...
274 42158 114
New
9mm
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
New
AstonJ
Posting this to see if we can make things easier for people to get into Neovim. If you use Neovim and have a favourite distro please let ...
New
baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New
skosch
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New

We're in Beta

About us Mission Statement