# Optional parts in Cmd

**URL:** https://discourse.julialang.org/t/optional-parts-in-cmd/26911
**Category:** General Usage
**Tags:** question
**Created:** [July 28, 2019, 3:28pm UTC](https://discourse.julialang.org/t/optional-parts-in-cmd/26911 "2019-07-28T15:28:11Z")
**Posts on this page:** 4
**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, 2019, 3:28pm UTC](https://discourse.julialang.org/t/optional-parts-in-cmd/26911/1 "2019-07-28T15:28:11Z")

</div>

I would like to construct a `Cmd` with some optional parts, depending in the caller. I got this working by collecting strings and then interpolating a vector of them, and this actually works fine (with escaping, etc) but I am curious if there is some other way.

```julia
function make_cmd(file, options)
    cmd_options = Any[]
    options.debug ≠ nothing && push!(cmd_options, "--debug=$(options.debug)")
    options.foo ≠ nothing && push!(cmd_options, "--foo=true")
    options.somepath ≠ nothing && push!(cmd_options, "--somepath=$(options.somepath)")
    `executable $(file) $(cmd_options)`
end

```

output:

```julia
julia> make_cmd("file.txt",
       (debug = "/debug_output.log", foo = true,
       somepath = "pathwith\"quote.txt"))
`executable file.txt --debug=/debug_output.log --foo=true '--somepath=pathwith"quote.txt'`

```

---

<div class="post-metadata">

### Author: ![StefanKarpinski](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stefankarpinski/32/24_2.png) [@StefanKarpinski](https://discourse.julialang.org/u/StefanKarpinski)
#### Post date: [July 28, 2019, 4:51pm UTC](https://discourse.julialang.org/t/optional-parts-in-cmd/26911/2 "2019-07-28T16:51:47Z")

</div>

Cmd objects interpolate correctly into each other so this is a common pattern:

```julia
function make_cmd(file, options)
    cmd = `executable $file`
    options.debug ≠ nothing && (cmd = `$cmd --debug=$(options.debug)`)
    options.foo ≠ nothing && (cmd = `$cmd --foo=true`)
    options.somepath ≠ nothing && (cmd = `$cmd --somepath=$(options.somepath)`)
    return cmd
end

```

Also and empty cmd object splices into another cmd without changing it so there’s that approach too.

---

<div class="post-metadata">

### Author: ![rdeits](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rdeits/32/286_2.png) [@rdeits](https://discourse.julialang.org/u/rdeits)
#### Post date: [July 28, 2019, 8:34pm UTC](https://discourse.julialang.org/t/optional-parts-in-cmd/26911/3 "2019-07-28T20:34:32Z")

</div>

Should that be:

```julia
cmd = `$cmd --debug=$(options.debug)`

```

rather than:

```julia
cmd = `--debug=$(options.debug)`

```

?

---

<div class="post-metadata">

### Author: ![StefanKarpinski](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stefankarpinski/32/24_2.png) [@StefanKarpinski](https://discourse.julialang.org/u/StefanKarpinski)
#### Post date: [July 28, 2019, 10:56pm UTC](https://discourse.julialang.org/t/optional-parts-in-cmd/26911/4 "2019-07-28T22:56:24Z")

</div>

Yup. Was on a phone on a bus. Fixed now.
