From my understanding, writing a custom Mix task requires this implementation:
def run(arg) do
...
end
Without this above implementation, Mix raises:
** (Mix) The task "xyz" does not export run/1
So studying, mix test
module, it implemented the same function but I could safely run mix test
in my project without command-line option. Why is that?
Thanks!
NobbZ
October 18, 2024, 1:21pm
2
It implements run/1
and then parses the options, with an option parser that knows what to do when args
is empty.
2 Likes
cevado
October 18, 2024, 1:23pm
3
the arg is whatever comes after the name, as a string, so the arg for mix test
is just ""
an empty string. the arity of the function doesn’t define if you have args or not, it’s a behaviour the expected arity is always 1 no matter what.
what you can do is just implement it as
def run(_arg) do
...
end
you can find more details here:
https://hexdocs.pm/mix/Mix.Task.html
2 Likes
Thanks for the clarification! @cevado @NobbZ
It makes so much sense now!
1 Like