I’m trying to build a Cmd
from the kwargs of a function. Basically, I want to turn eg foo("some_option"; opt1 = "thing", opt2="other_thing")
into run(`my_cli some_option --opt1 thing --opt2 other_thing`)
. Cmd()
can take a vector of strings, so I’m trying to use a comprehension to convert kwargs...
into such a vector.
Basic function signature is
function foo(c1; kwargs...)
c = ["my_cli", c1]
append!(c, <something>)
run(Cmd(c))
end
Where <something>
is currently Iterators.flatten(("--" * string(k), string(v)) for (k,v) in pairs(kwargs)))
. So basically I’m making tuples of key=>value
and then using Iterators.flatten
to press them into a sequential vectorz. This works, but I was wondering if this is the best* approach? Other things I tried that didn’t work:
[("--" * string(k), string(v))... for (k,v) in pairs(kwargs)]
["--" * string(k), string(v) for (k,v) in pairs(kwargs)]
* I realize “best” is quite subjective, and perhaps I should be fine with the flatten
approach, but it feels somehow more verbose than I was expecting. Then again, this is exactly what flatten
is for, so…