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 prompt
or 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
Given:
defmodule Mix.Tasks.Echo do
use Mix.Task
@impl Mix.Task
def run(args) do
IO.puts("running!")
Mix.shell().info(Enum.join(args, " "))
IO.inspect(args, label: "Received args")
end
end
Running that gives you:
$ mix echo foo.bar 123
running!
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.
1 Like
ah, thank you. I also came across OptionParser.parse
to work with --flags
2 Likes