create
create declares a CLI command. You provide a name and an execute function, and you can also add options, positional arguments, or child 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[],
GenericSubjects extends Subjects
>(
name: string,
params: CreateCommandParams<GenericOptions, GenericSubjects>,
execute: (
params: CreateCommandExecuteParams<GenericOptions, GenericSubjects>
) => 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.subjects(Argument[] | Command[], optional) : either a list of positional arguments built withcreateArgument, or a list of child commands.execute: command handler. Receives typedoptionsand, when positional arguments are declared, typedargs.
Return value
Command: command definition object meant to be attached to a command tree and executed throughexec.
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 },
),
],
subjects: [
SC.createArgument(
"version",
DP.union([
DP.templateLiteral([
"v",
DP.number(),
".",
DP.number(),
".",
DP.number(),
]),
DP.literal("latest"),
]),
),
],
},
({ options: { environment }, args: { version } }) => {
type check = ExpectType<
typeof version,
`v${number}.${number}.${number}` | "latest",
"strict"
>;
type checkOption = ExpectType<
typeof environment,
"staging" | "prod",
"strict"
>;
},
);
const releaseCommand = SC.create(
"release",
{
subjects: [deployCommand],
},
() => {
console.log("select a release sub-command");
},
);See also
exec- Runs a root command from process arguments.createArgument- Builds a positional argument used insubjects.createOption- Builds a single-value option.createArrayOption- Builds an array option.
