Skip to content

createOption

Creates an option with a single parsed value from a DataParser or clean contract.

Example

ts
import { 
SC
} from "@duplojs/server-utils";
import {
C
,
DP
} from "@duplojs/utils";
const
UserId
=
C
.
createNewType
("user-id",
DP
.
number
(),
C
.
Positive
);
await
SC
.
exec
({
options
: [
SC
.
createOption
("host",
DP
.
string
()),
SC
.
createOption
(
"port",
DP
.
number
(),
{
required
: true },
),
SC
.
createOption
(
"environment",
DP
.
literal
(["dev", "prod"]),
),
SC
.
createOption
("email",
C
.
Email
),
SC
.
createOption
("userId",
UserId
),
], }, ({
options
}) => {
});

Syntax

typescript
function createOption<
  GenericName extends string,
  GenericSpec extends EligibleSpec,
  GenericOutput extends ComputeOptionSpec<GenericSpec> = ComputeOptionSpec<GenericSpec>
>(
  name: GenericName,
  spec: GenericSpec,
  params: {
    description?: string
    aliases?: readonly string[]
    required: true
  }
): Option<GenericName, GenericOutput>

function createOption<
  GenericName extends string,
  GenericSpec extends EligibleSpec,
  GenericOutput extends ComputeOptionSpec<GenericSpec> = ComputeOptionSpec<GenericSpec>
>(
  name: GenericName,
  spec: GenericSpec,
  params?: {
    description?: string
    aliases?: readonly string[]
  }
): Option<GenericName, GenericOutput | undefined>

Parameters

  • name (string) : option name used as --name.
  • spec (EligibleSpec) : parser or clean spec used to validate/transform the value.
  • params (optional) : option metadata and requirement behavior.
  • params.required (true, optional) : throws when option is missing.
  • params.description (string, optional) : help description.
  • params.aliases (string[], optional) : short aliases.

Return value

  • Option<GenericName, GenericOutput> when required: true.
  • Option<GenericName, GenericOutput | undefined> otherwise.

Notes

  • Primitive parsers and clean primitive contracts are coerced from CLI string input automatically.

See also

Released under the MIT license.