Skip to content

createOption

Creates an option with a single parsed value from a DataParser schema.

Example

ts
import { 
SC
} from "@duplojs/server-utils";
import {
DP
, type
ExpectType
} from "@duplojs/utils";
const
command
=
SC
.
create
(
"serve", {
options
: [
SC
.
createOption
("host",
DP
.
string
()),
SC
.
createOption
(
"port",
DP
.
coerce
.
number
(),
{
required
: true },
),
SC
.
createOption
(
"environment",
DP
.
literal
(["dev", "prod"]),
), ], }, ({
options
}) => {
type
check
=
ExpectType
<
typeof
options
,
{
host
: string | undefined;
port
: number;
environment
: "dev" | "prod" | undefined;
}, "strict" >; }, ); await
command
.
execute
([
"--host", "0.0.0.0", "--port=8080", "--environment=prod", ]);

Syntax

typescript
function createOption<
  GenericName extends string,
  GenericSchema extends EligibleDataParser
>(
  name: GenericName,
  schema: GenericSchema,
  params: {
    description?: string
    aliases?: readonly string[]
    required: true
  }
): Option<GenericName, DP.Output<GenericSchema>>

function createOption<
  GenericName extends string,
  GenericSchema extends EligibleDataParser
>(
  name: GenericName,
  schema: GenericSchema,
  params?: {
    description?: string
    aliases?: readonly string[]
  }
): Option<GenericName, DP.Output<GenericSchema> | undefined>

Parameters

  • name (string) : option name used as --name.
  • schema (EligibleDataParser) : parser 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, DP.Output<GenericSchema>> when required: true.
  • Option<GenericName, DP.Output<GenericSchema> | undefined> otherwise.

See also

Released under the MIT license.