Stream: beginners

Topic: how to use Cmd from basi Cli


view this post on Zulip Artur Swiderski (Dec 24 2023 at 14:12):

hi how to use Cmd module to invoke let say "ls" command ?? I can't find example for that

view this post on Zulip Brendan Hansknecht (Dec 24 2023 at 14:33):

Some examples and the API docs here: https://www.roc-lang.org/packages/basic-cli/Cmd

view this post on Zulip Artur Swiderski (Dec 24 2023 at 14:34):

API I see. I have no idea how to use it

view this post on Zulip Brendan Hansknecht (Dec 24 2023 at 14:37):

Something like this:

output <-
    Cmd.new "ls"
    |> Cmd.arg "-l"
    |> Cmd.output
    |> Task.await

view this post on Zulip Artur Swiderski (Dec 24 2023 at 14:37):

this I figured out but I do not know what to do with output

view this post on Zulip Artur Swiderski (Dec 24 2023 at 14:38):

it is weird type I don't know how to work with

view this post on Zulip Brendan Hansknecht (Dec 24 2023 at 14:38):

Command example in the basic CLI repo: https://github.com/roc-lang/basic-cli/blob/main/examples/command.roc

view this post on Zulip Brendan Hansknecht (Dec 24 2023 at 14:40):

Output : {
    stdout : List U8,
    stderr : List U8,
}

view this post on Zulip Artur Swiderski (Dec 24 2023 at 14:42):

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

view this post on Zulip Brendan Hansknecht (Dec 24 2023 at 14:46):

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,
]

view this post on Zulip Artur Swiderski (Dec 24 2023 at 14:48):

() <-- is tuple ? ok I get that

view this post on Zulip Brendan Hansknecht (Dec 24 2023 at 14:52):

Yeah, as long as it has at least 2 elements. Roc does not have 0 or 1 element tuples.

view this post on Zulip Artur Swiderski (Dec 24 2023 at 14:54):

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  {}

view this post on Zulip Artur Swiderski (Dec 24 2023 at 14:56):

wait I forget if this is legal

view this post on Zulip Brendan Hansknecht (Dec 24 2023 at 14:58):

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  {}

view this post on Zulip Artur Swiderski (Dec 24 2023 at 15:01):

thx, task are black magic for me because they do not resemble anything I worked with in the past

view this post on Zulip Artur Swiderski (Dec 24 2023 at 15:18):

ok I finally manage : ) I need to definitely play bit more with Tasks

view this post on Zulip Brendan Hansknecht (Dec 24 2023 at 15:55):

Glad you figured it out


Last updated: Jul 06 2025 at 12:14 UTC