A minimal CLI arguments parser in Julia
24 October 2025
With the advent of CLI app support for Julia modules since version 1.12, I hope that we will see many cool CLI apps built in Julia. If you haven’t seen it before, you can now run
julia -m MyCoolApp
and it will execute the @main function of the module MyCoolApp. Additionally, you can now also create proper apps which allows you to write simply
my-cool-app
to the same effect.
Now what if you want to give your app some arguments? Let’s keep it very simple and assume we only want to use key=value pairs, i.e.
my-cool-app alpha=10 beta=3.14
and we also want to have default values for not specified keys (maybe as simulation parameters or so).
There are many Julia packages for parsing command line arguments out there, such as ArgParse, ArgMacros, or GetOpt. But for our constrained use case, we can build our own parser with very little effort:
module MyCoolApp
function (@main)(args)
config = Dict(split.(args, '='))
alpha = parse(Int, get(config, "alpha", "10"))
beta = parse(Float32, get(config, "beta", "10"))
gamma = first(get(config, "gamma", "A"))
println("I computed $(beta^alpha) and gamma was $gamma")
return
end
end
which would print:
I computed 93174.41 and gamma was A
It works like this: The shell handles splitting the input alpha=10 beta=3.14 into two strings "alpha=10" and "beta=3.14" for us. This is what is given to us in the args argument of our @main function, i.e.
args == ["alpha=10", "beta=3.14"]
Then, we split each argument at the = sign, which gives us an array of arrays of strings:
split.(args, '=') == [
["alpha", "10"],
["beta", "3.14"],
]
Since the Dict constructor interprets collections of two-entry collections as key-value pairs, it happily provides us with the data structure we need for extracting the values. We now have
Dict(split.(args, '=')) == Dict("alpha" => "10", "beta" => "3.14")
Finally, the get function allows us to query the Dict for our keys of interest and specify a default value as the third argument. All that is left is to parse the value from a string to whatever we need, typically using parse(Type, value).
That’s it!