Get CLI arguments for custom Mix task

I have a custom mix task that I want to use like mix 123 where 123 is a command-line argument. How do I get that? I’m striking out on Google… 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


defmodule Mix.Tasks.Echo do
  use Mix.Task

  @impl Mix.Task
  def run(args) do
    IO.puts("running!"), " "))
    IO.inspect(args, label: "Received args")

Running that gives you:

$ mix echo 123
running! 123
Received args: ["", "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