Stream: beginners

Topic: ✔ basic-cli Opt/Param docs


view this post on Zulip Martin Janiczek (Oct 02 2024 at 07:25):

Hello all! I'm trying to write a small CLI tool in Roc. Using basic-cli.

I'm looking at the docs and am missing anything about Opt or Param, or even SubCmd.

Is that a documentation bug, or a me-problem? Is my best option to read the library source code for now?

view this post on Zulip Luke Boswell (Oct 02 2024 at 07:47):

What is Opt, Param or SubCmd?

view this post on Zulip Luke Boswell (Oct 02 2024 at 07:47):

I'm not familiar with these names

view this post on Zulip Luke Boswell (Oct 02 2024 at 07:49):

I'm guessing this is an Elm thing... but its been so long I've fogotten

view this post on Zulip Nathan Kramer (Oct 02 2024 at 07:49):

I can see those names in this directory of basic-cli

https://github.com/roc-lang/basic-cli/tree/main/platform/Arg

view this post on Zulip Luke Boswell (Oct 02 2024 at 07:49):

Ahk... right

view this post on Zulip Nathan Kramer (Oct 02 2024 at 07:49):

I guess they're just undocumented as you say Martin

view this post on Zulip Luke Boswell (Oct 02 2024 at 07:50):

I think this is the bug I was talking about earlier today https://roc.zulipchat.com/#narrow/stream/306878-off-topic/topic/POC.3A.20AI.20agent.20writing.20Roc/near/474144956

view this post on Zulip Nathan Kramer (Oct 02 2024 at 07:50):

ah, interesting

view this post on Zulip Luke Boswell (Oct 02 2024 at 07:50):

Is that a documentation bug, or a me-problem? Is my best option to read the library source code for now?

Yeah, I think it's a bug in roc docs generation... so best to read the source for now.

view this post on Zulip Luke Boswell (Oct 02 2024 at 07:51):

Checkout https://github.com/smores56/weaver

view this post on Zulip Luke Boswell (Oct 02 2024 at 07:51):

Docs here https://smores56.github.io/weaver/Opt/

view this post on Zulip Luke Boswell (Oct 02 2024 at 07:52):

The implementation in basic-cli was copied from weaver...

view this post on Zulip Luke Boswell (Oct 02 2024 at 07:52):

I think the plan is to use weaver instead of having it in the platform long term

view this post on Zulip Martin Janiczek (Oct 02 2024 at 08:25):

Thank you both!

view this post on Zulip Notification Bot (Oct 02 2024 at 08:25):

Martin Janiczek has marked this topic as resolved.

view this post on Zulip Martin Janiczek (Oct 02 2024 at 08:28):

Actually, maybe one more question. Is there a pattern for non-empty list in either basic-cli or weaver? I ended up using Opt.strList and a followup custom logic:

    when Cli.parseOrDisplayMessage cli args is
        Ok input ->
            if List.isEmpty input.versions then
                Stderr.line! "No versions to benchmark"
                Task.err (Exit 1 "")
            else
                Bench.run input

        Err message ->
            Stderr.line! message
            Task.err (Exit 1 "")

view this post on Zulip Martin Janiczek (Oct 02 2024 at 08:29):

I guess if there's no Opt.nonemptyStrList my best option is to extract the above length check into some validateInput function that I use with Result.try?

view this post on Zulip Martin Janiczek (Oct 02 2024 at 08:30):

as in, when args |> Cli.parseOrDisplayMessage cli |> Result.try validateInput is ...

view this post on Zulip Sam Mohr (Oct 02 2024 at 08:47):

@Martin Janiczek I'd say you have a couple options:

First is to ask for two fields, the first being a single value and the second being a list:

cliParser =
    { Cli.weave <-
        file: Param.str { name: "file", help: "The file to process." },
        files: Param.strList { name: "files", help: "The rest of the files." },
    }

parsed = parseArgsWith! cliParser args

allFiles = List.concat [file] files

Second is to validate the list value, which still requires post-parsing handling:

cliParser =
    { Cli.weave <-
        # other fields...
        files: Param.strList { name: "files" }
            |> Cli.map \fs -> if List.isEmpty fs then Err EmptyFiles else Ok fs
    }

parsed = parseArgsWith! cliParser args

files = parsed.files?

Last updated: Jul 06 2025 at 12:14 UTC