Skip to content

createArgument

Creates a positional argument parser to use in command subjects.

Example

ts
import { 
SC
} from "@duplojs/server-utils";
import {
DP
} from "@duplojs/utils";
const
sourceArgument
=
SC
.
createArgument
(
"source",
DP
.
string
(),
{
description
: "Source file path",
}, ); const
targetArgument
=
SC
.
createArgument
(
"target",
DP
.
string
(),
{
description
: "Target file path",
optional
: true,
}, );
SC
.
create
(
"copy", {
subjects
: [
sourceArgument
,
targetArgument
],
}, ({
args
}) => {
console
.
log
("copy from",
args
.
source
, "to",
args
.
target
?? "(stdout)");
}, );

Syntax

typescript
function createArgument<
  GenericName extends string,
  GenericSpec extends EligibleSpec
>(
  name: GenericName,
  spec: GenericSpec,
  params?: {
    description?: string
    optional?: false
  }
): Argument<GenericName, EligibleSpecOutput<GenericSpec>>

function createArgument<
  GenericName extends string,
  GenericSpec extends EligibleSpec
>(
  name: GenericName,
  spec: GenericSpec,
  params?: {
    description?: string
    optional: boolean
  }
): Argument<GenericName, EligibleSpecOutput<GenericSpec> | undefined>

Parameters

  • name (string) : argument key exposed in args.
  • spec (EligibleSpec) : parser/clean spec used to parse and validate the received positional value.
  • params (optional) : argument metadata.
  • params.description (string, optional) : help description.
  • params.optional (boolean, default false) : allows the argument to be omitted.

Return value

  • Argument : argument definition to place inside subjects in create or exec.

Notes

  • Positional arguments are consumed in declaration order.
  • In execute handlers, parsed values are available as args.<name>.
  • Use only EligibleSpec values (primitive-like specs, literals, unions, pipes/transforms, file, clean specs).

See also

  • create - Builds a command node using subjects.
  • exec - Runs a root command from process arguments.

Released under the MIT license.