Skip to content

Repeatable options

An option can be passed more than once when its coerce produces an array or a set. Each occurrence is coerced on its own, so the separator still applies. The results are merged before the schema validates the final value.

import { coerce, defineCLI } from "@staticbolt/args-parser";
import * as z from "zod";
const cli = defineCLI({
cliName: "listy",
options: {
items: {
schema: z.array(z.string()).min(1),
coerce: coerce.stringArray(","),
},
tags: {
schema: z.set(z.string()).optional(),
coerce: coerce.stringSet("|"),
},
},
});
Terminal window
listy --items clean # ["clean"]
listy --items clean,cook # ["clean", "cook"]
listy --items clean --items cook # ["clean", "cook"]
listy --items clean,cook --items "wash dishes" # ["clean", "cook", "wash dishes"]
listy --items clean --tags "work|home" --tags work # tags: Set { "work", "home" }

Two things follow from that:

  • Schema rules such as .min() apply to the merged value, not to each occurrence.
  • Repeating an option that is not coerced to an array or a set fails with ValidationErrorCode.OptionNotRepeatable.

Raw occurrences

The strings exactly as typed are kept in the context, one per occurrence:

cli.onExecute(({ context }) => {
console.log(context.options.items.stringValues); // ["clean,cook", "wash dishes"]
});

See Context for the rest of what is recorded.