Skip to content

exec

exec lance votre CLI. Il lit les arguments passés au programme, choisit la bonne commande, puis exécute votre callback. Si l'utilisateur passe --help, le help correspondant est affiché automatiquement.

Exemple

ts
import { 
SC
} from "@duplojs/server-utils";
import {
DP
} from "@duplojs/utils";
await
SC
.
exec
(
{
options
: [
SC
.
createBooleanOption
("version")],
subject
:
DP
.
tuple
([
DP
.
string
(),
DP
.
string
()]),
}, ({
subject
,
options
}) => {
if (
options
.
version
) {
console
.
log
("1.0.0");
return; } const [
sourcePath
,
targetPath
] =
subject
;
console
.
log
(`copy ${
sourcePath
} to ${
targetPath
}`);
}, ); // Try with: // cpCli file.txt file-copy.txt // output: copy file.txt to file-copy.txt // Other: // cpCli --version // output: 1.0.0

Syntaxe

typescript
function exec(
  execute: () => void
): Promise<void>

function exec<
  GenericOptions extends readonly Option[],
  GenericSubject extends Subject
>(
  params: CreateCommandParams<GenericOptions, GenericSubject>,
  execute: (
    params: CreateCommandExecuteParams<GenericOptions, GenericSubject>
  ) => MaybePromise<void>
): Promise<void>

Paramètres

  • execute : handler racine appelé quand aucun objet params n'est fourni.
  • params (CreateCommandParams) : configuration de la commande racine.
  • params.description (string, optionnel) : affiché dans le rendu du help.
  • params.options (Option[], optionnel) : définitions d'options parsées avant exécution.
  • params.subject (Subject | Command[], optionnel) : sujet DataParser ou liste de sous-commandes.
  • execute (surcharge 2) : reçoit options typées et subject typé optionnel.

Valeur de retour

  • Promise<void> : résolue une fois le dispatch et l'exécution terminés.

Autres exemples

Commande racine minimale

ts
import { 
SC
} from "@duplojs/server-utils";
await
SC
.
exec
(
{
options
: [
SC
.
createBooleanOption
(
"verbose", {
aliases
: ["v"] },
), ], }, ({
options
}) => {
if (
options
.
verbose
) {
console
.
log
("Verbose output enabled");
} }, );

Avancé

ts
import { 
SC
} from "@duplojs/server-utils";
import {
DP
} from "@duplojs/utils";
const
migrateCommand
=
SC
.
create
(
"migrate", {
description
: "Run database migrations",
options
: [
SC
.
createBooleanOption
(
"dryRun", {
description
: "Preview SQL statements without applying changes",
aliases
: ["dr"],
}, ),
SC
.
createOption
(
"environment",
DP
.
literal
(["dev", "staging", "prod"]),
{
required
: true },
), ],
subject
:
DP
.
union
([
DP
.
templateLiteral
([
"v",
DP
.
number
(),
".",
DP
.
number
(),
".",
DP
.
number
(),
]),
DP
.
literal
("latest"),
]), }, ({
options
: {
dryRun
,
environment
},
subject
:
targetVersion
}) => {
const
mode
=
dryRun
? "preview" : "apply";
console
.
log
(
`[${
environment
}] ${
mode
} migration to ${
targetVersion
}`,
); }, ); const
seedCommand
=
SC
.
create
(
"seed", {
description
: "Populate default data",
options
: [
SC
.
createArrayOption
(
"tags",
DP
.
string
(),
{
separator
: "," },
), ], }, ({
options
: {
tags
} }) => {
console
.
log
("seeding with tags:",
tags
?? []);
}, ); await
SC
.
exec
(
{
description
: "Project CLI entrypoint",
options
: [
SC
.
createBooleanOption
("verbose", {
aliases
: ["v"] })],
subject
: [
migrateCommand
,
seedCommand
],
}, ({
options
}) => {
if (
options
.
verbose
) {
console
.
log
("verbose mode enabled");
} }, ); // Try with: // cli migrate --environment=dev 2026-01-10 // cli seed --tags=users,roles // cli --help

Voir aussi

Diffusé sous licence MIT.