# Constructing a list of options for a cmd

**URL:** https://discourse.julialang.org/t/constructing-a-list-of-options-for-a-cmd/84910
**Category:** General Usage
**Tags:** question
**Created:** [July 28, 2022, 9:50am UTC](https://discourse.julialang.org/t/constructing-a-list-of-options-for-a-cmd/84910 "2022-07-28T09:50:09Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)
#### Post date: [July 28, 2022, 9:50am UTC](https://discourse.julialang.org/t/constructing-a-list-of-options-for-a-cmd/84910/1 "2022-07-28T09:50:09Z")

</div>

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

```julia
`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
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?

---

<div class="post-metadata">

### Author: ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)
#### Post date: [July 28, 2022, 9:52am UTC](https://discourse.julialang.org/t/constructing-a-list-of-options-for-a-cmd/84910/2 "2022-07-28T09:52:42Z")

</div>

There is of course

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

```

---

<div class="post-metadata">

### Author: ![ericphanson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ericphanson/32/215186_2.png) [@ericphanson](https://discourse.julialang.org/u/ericphanson)
#### Post date: [July 28, 2022, 9:59am UTC](https://discourse.julialang.org/t/constructing-a-list-of-options-for-a-cmd/84910/3 "2022-07-28T09:59:57Z")

</div>

This seems to do the right thing:

```julia
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
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
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.

---

<div class="post-metadata">

### Author: ![mkitti](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkitti/32/12459_2.png) [@mkitti](https://discourse.julialang.org/u/mkitti)
#### Post date: [July 28, 2022, 10:02am UTC](https://discourse.julialang.org/t/constructing-a-list-of-options-for-a-cmd/84910/4 "2022-07-28T10:02:36Z")

</div>

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

```julia
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"

```

---

<div class="post-metadata">

### Author: ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)
#### Post date: [July 28, 2022, 10:08am UTC](https://discourse.julialang.org/t/constructing-a-list-of-options-for-a-cmd/84910/5 "2022-07-28T10:08:25Z")

</div>

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.
