create
create is used to declare a CLI command. You provide a name and an execute function. You can also add options, a subject (positional arguments), or sub-commands.
Example
ts
import { SC } from "@duplojs/server-utils";
const helloCommand = SC.create(
"hello",
() => {
console.log("hello");
},
);Syntax
typescript
function create(
name: string,
execute: () => void
): Command
function create<
GenericOptions extends readonly Option[],
GenericSubject extends Subject
>(
name: string,
params: CreateCommandParams<GenericOptions, GenericSubject>,
execute: (
params: CreateCommandExecuteParams<GenericOptions, GenericSubject>
) => MaybePromise<void>
): CommandParameters
name(string) : command name used for matching and help rendering.params(CreateCommandParams, optional) : command configuration.params.description(string, optional) : help description.params.options(Option[], optional) : option parsers.params.subject(Subject | Command[], optional) : parser for positional data or sub-commands list.execute: command handler. Receives typedoptionsand optional typedsubject.
Return value
Command: executable command object withexecute(args).
Others examples
Advanced
ts
import { SC } from "@duplojs/server-utils";
import { DP, type ExpectType } from "@duplojs/utils";
const deployCommand = SC.create(
"deploy",
{
description: "Deploy the current release",
options: [
SC.createOption(
"environment",
DP.literal(["staging", "prod"]),
{ required: true },
),
],
subject: DP.union([
DP.templateLiteral([
"v",
DP.number(),
".",
DP.number(),
".",
DP.number(),
]),
DP.literal("latest"),
]),
},
({ options: { environment }, subject }) => {
type check = ExpectType<
typeof subject,
`v${number}.${number}.${number}` | "latest",
"strict"
>;
type checkOption = ExpectType<
typeof environment,
"staging" | "prod",
"strict"
>;
},
);
const releaseCommand = SC.create(
"release",
{
subject: [deployCommand],
},
() => {
console.log("select a release sub-command");
},
);See also
exec- Runs a root command from process arguments.createOption- Builds a single-value option.createArrayOption- Builds an array option.
