Stream: beginners

Topic: Output.stdout


view this post on Zulip Sylvain Brunerie (Apr 03 2024 at 11:34):

Hey! I’m trying to write my first Roc program which is a CLI tool. It needs to run a command and read its output. I’m defining the command as:

Cmd.new "git"
    |> Cmd.args ["log", "--pretty=format:\"%H %aI\""]

And running it with Task.await (Cmd.output gitLogCmd), getting the actual Output and turning it into a string with Str.fromUtf8. It works, but my problem here is that each line from the output is enclosed in double quotes:

"4df174c59ec48c135a5e7369d3deeca56497056a 2024-04-03T10:11:49+02:00"
"b620b1a55428862e44cc5cf06703de043bd46ed1 2024-04-03T10:11:11+02:00"

Is that expected? Also the definition of the Output type was quite hard to find, it doesn’t seem to appear in the docs?

view this post on Zulip Anton (Apr 03 2024 at 11:39):

Hi @Sylvain Brunerie,
Can you share your full code?

view this post on Zulip Anton (Apr 03 2024 at 11:51):

Also the definition of the Output type was quite hard to find, it doesn’t seem to appear in the docs?

I'll make an issue for that :)

view this post on Zulip Anton (Apr 03 2024 at 11:55):

#6626

view this post on Zulip Sylvain Brunerie (Apr 03 2024 at 12:00):

Oh so it looks like it’s just about the argument to git log. Cmd.args ["log", "--pretty=format:\"%H %aI\""] is actually adding the surrounding quotes. So it works perfectly with Cmd.args ["log", "--pretty=format:%H %aI"] but I can’t run the command this way in the shell. That means I have to change my command from the shell to Roc. Feels weird but maybe it makes sense.

view this post on Zulip Hristo (Apr 03 2024 at 12:02):

@Anton this is reproducible outside the context of Roc via:

git log --pretty=format:"\"%H %aI\""

It seems that in @Sylvain Brunerie's case, \" is interpreted by Roc with an equivalent result (which doesn't seem intuitive as an interpretation; the first \" seems to be interpreted differently from the second instance, e.g., as if it's a closing and opening quotation marks), whilst the desired output is probably something like:

git log --pretty=format:"%H %aI"

view this post on Zulip Sylvain Brunerie (Apr 03 2024 at 12:16):

This might be relevant: https://doc.rust-lang.org/std/process/struct.Command.html#method.args

Note that the arguments are not passed through a shell, but given literally to the program. This means that shell syntax like quotes, escaped characters, word splitting, glob patterns, variable substitution, etc. have no effect.

view this post on Zulip Sylvain Brunerie (Apr 03 2024 at 12:17):

That’s the function called by the underlying basic-cli platform, so I guess the behaviour is expected. For now, I find it very confusing, but maybe it’s for the best, and there should just be a similar warning in the platform docs.

view this post on Zulip Anton (Apr 03 2024 at 13:20):

and there should just be a similar warning in the platform docs.

I'll do that :)


Last updated: Jul 05 2025 at 12:14 UTC