hi how to use Cmd module to invoke let say "ls" command ?? I can't find example for that
Some examples and the API docs here: https://www.roc-lang.org/packages/basic-cli/Cmd
API I see. I have no idea how to use it
Something like this:
output <-
Cmd.new "ls"
|> Cmd.arg "-l"
|> Cmd.output
|> Task.await
this I figured out but I do not know what to do with output
it is weird type I don't know how to work with
Command example in the basic CLI repo: https://github.com/roc-lang/basic-cli/blob/main/examples/command.roc
Output : {
stdout : List U8,
stderr : List U8,
}
thx I will try, "Task Output (Output, Error)" <--- I couldn't figure out this notation what does it mean is different than usual Task {} * or something along those lines
Task Output (Output, Error)
:
On success, returns the output type I pasted above.
On failure, returns a tuple of the output with the error.
Error is:
CommandErr : [
ExitCode I32,
KilledBySignal,
IOError Str,
]
() <-- is tuple ? ok I get that
Yeah, as long as it has at least 2 elements. Roc does not have 0 or 1 element tuples.
but still I can't pattern match this
cmdResult <-Cmd.output command |> Task.await
when cmdResult is
val ->
{stderr : err, stdout : out } = val
Task.ok {}
(val,err) ->
Task.ok {}
wait I forget if this is legal
Task.await
just gives you the success case and delays the error handling. Task.attempt
will give you a result with the full type.
So this should work:
cmdResult <-Cmd.output command |> Task.attempt
when cmdResult is
Ok val ->
{stderr : err, stdout : out } = val
Task.ok {}
Err (val,err) ->
Task.ok {}
thx, task are black magic for me because they do not resemble anything I worked with in the past
ok I finally manage : ) I need to definitely play bit more with Tasks
Glad you figured it out
Last updated: Jul 06 2025 at 12:14 UTC