I have a custom mix task that I want to use like
mix foo.bar 123 where 123 is a command-line argument. How do I get that? I’m striking out on Google… https://hexdocs.pm/mix/Mix.Task.html doesn’t seem to show anything other than
yes?, which allow for an interactive interface, but what about command line arguments?
Thanks for pointers!
I think you’re just looking for the
args that the
run/1 function receives
defmodule Mix.Tasks.Echo do
def run(args) do
Mix.shell().info(Enum.join(args, " "))
IO.inspect(args, label: "Received args")
Running that gives you:
$ mix echo foo.bar 123
Received args: ["foo.bar", "123"]
Although at least with the umbrella project setup I’m using it’s not picking up changes to the mix task without running
mix.compile, but generally you should be good to go.
ah, thank you. I also came across
OptionParser.parse to work with