Skip to content

Sharing options and arguments

defineOptions and defineArguments let you write a set of definitions once and reuse it across subcommands and the main CLI. They only exist to keep the types intact when you pull the definitions out of the command that they belong to.

shared.ts
import { coerce, defineArguments, defineOptions } from "@staticbolt/args-parser";
import * as z from "zod";
export const sharedOptions = defineOptions({
verbose: {
aliases: ["V"],
schema: z.boolean().optional(),
coerce: coerce.boolean,
meta: {
description: "Print more detail while running.",
},
},
});
export const sharedArguments = defineArguments({
// ...
});

Spread them into any command that needs them:

commands/add-items.ts
import { defineSubcommand } from "@staticbolt/args-parser";
import { sharedArguments, sharedOptions } from "../shared.ts";
export const addItemsCommand = defineSubcommand({
name: "add-items",
options: {
list: {/* ... */},
...sharedOptions,
},
arguments: {
...sharedArguments,
},
});
Warning

Spreading a shared option next to one with the same name silently replaces it, the way any object spread does. There is no error, so keep names unique inside each command.

Order matters for arguments. Spread sharedArguments where you want those arguments to sit in the sequence.