Constructing a list of options for a cmd

I have a list of options to pass to an executable. For the purposes of the MWE, a call could look like

`c`
`c a=1`
`c a=1 b=2`

Whether I need to append a and b are known at runtime, and are taken from a NamedTuple.

Naively I tried generating a list of cmds and splicing it:

julia> args = [`a=1`, `b=2`]
2-element Vector{Cmd}:
 `a=1`
 `b=2`

julia> `c $(args...)`
`c a=1b=2`

Notice that the two are merged. Is this a bug?

How to do this correctly?

2 Likes

There is of course

julia> foldl((x, y) -> `$x $y`, args; init = `c`)
`c a=1 b=2`
1 Like

This seems to do the right thing:

julia> args = ["a=1", "b=2"]
2-element Vector{String}:
 "a=1"
 "b=2"

julia> `c $args`
`c a=1 b=2`

This does the same merging thing:

julia> args = ["a=1", "b=2"]
2-element Vector{String}:
 "a=1"
 "b=2"

julia> `c $(args...)`
`c a=1b=2`

This seems to have extra quotes:

julia> args = [`a=1`, `b=2`]
2-element Vector{Cmd}:
 `a=1`
 `b=2`

julia> `c $args`
`c \`a=1\` \`b=2\``

I am not really sure why and what the rules are but have always wondered.

1 Like

There is a constructor that takes a Vector{String}. You can also manipulate that Vector{String} via the field exec:

julia> command = Cmd(["c", "a=1", "b=1"])
`c a=1 b=1`

julia> command = `c`
`c`

julia> push!(command.exec, "a=1")
2-element Vector{String}:
 "c"
 "a=1"

julia> push!(command.exec, "b=1")
3-element Vector{String}:
 "c"
 "a=1"
 "b=1"
1 Like

Note that neither of these solutions is exposed API. The only Cmd method that is documented is the one that takes a Cmd.

Just to clarify the question: I am looking for solution(s) via the API, and also want to know if the mentioned example is a bug.