Skip to content

Function: resolveArgs() ​

Call Signature ​

ts
export declare function resolveArgs<A extends Args>(args: A, tokens: ArgToken[], resolveArgs?: ResolveArgs & {
  passthrough?: false;
}): {
  values: ArgValues<A>;
  positionals: string[];
  rest: string[];
  error: AggregateError | undefined;
  explicit: ArgExplicitlyProvided<A>;
}

Resolve command line arguments.

Type Parameters ​

NameDescription
A extends ArgsArguments, which is an object that defines the command line arguments.

Parameters ​

NameTypeDescription
argsAAn arguments that contains arguments schema.
tokensArgToken[]An array of tokens.
resolveArgsResolveArgs & { passthrough?: false }An arguments that contains resolve arguments. (optional)

Returns ​

object — An object that contains the values of the arguments, positional arguments, rest arguments, validation errors, and explicit provision status.

error ​

ts
error: AggregateError | undefined;

explicit ​

ts
explicit: ArgExplicitlyProvided<A>;

positionals ​

ts
positionals: string[];

rest ​

ts
rest: string[];

values ​

ts
values: ArgValues<A>;

Examples ​

typescript
// passed tokens: --port 3000

const { values, explicit } = resolveArgs({
  port: {
    type: 'number',
    default: 8080
  },
  host: {
    type: 'string',
    default: 'localhost'
  }
}, parsedTokens)

values.port // 3000
values.host // 'localhost'

explicit.port // true (explicitly provided)
explicit.host // false (not provided, fallback to default)

Call Signature ​

ts
export declare function resolveArgs<A extends Args>(args: A, tokens: ArgToken[], resolveArgs: ResolveArgs & {
  passthrough: true;
}): {
  values: ArgValues<A>;
  positionals: string[];
  rest: string[];
  error: AggregateError | undefined;
  explicit: ArgExplicitlyProvided<A>;
  /**
   * The indexes, in the arguments given to `parseArgs()`, of the arguments that the schema does not
   * take, in ascending order. See {@link ResolveArgs.passthrough} for which arguments they are.
   */
  passthroughIndices: number[];
}

Resolve command line arguments, and give the indexes of the arguments that the schema does not take, with passthrough.

Type Parameters ​

NameDescription
A extends ArgsArguments, which is an object that defines the command line arguments.

Parameters ​

NameTypeDescription
argsAAn arguments that contains arguments schema.
tokensArgToken[]An array of tokens.
resolveArgsResolveArgs & { passthrough: true }An arguments that contains resolve arguments, with passthrough: true.

Returns ​

object — An object that contains the values of the arguments, positional arguments, rest arguments, validation errors, explicit provision status, and the indexes of the arguments that the schema does not take.

error ​

ts
error: AggregateError | undefined;

explicit ​

ts
explicit: ArgExplicitlyProvided<A>;

passthroughIndices ​

ts
passthroughIndices: number[];

The indexes, in the arguments given to parseArgs(), of the arguments that the schema does not take, in ascending order. See ResolveArgs.passthrough for which arguments they are.

positionals ​

ts
positionals: string[];

rest ​

ts
rest: string[];

values ​

ts
values: ArgValues<A>;

Examples ​

typescript
// passed tokens: exec run.js --watch src

const { values, passthroughIndices } = resolveArgs(
  { script: { type: 'positional' } },
  parsedTokens,
  { skipPositional: 0, passthrough: true }
)

values.script // 'run.js'
passthroughIndices // [2, 3], the indexes of `--watch` and `src`

Call Signature ​

ts
export declare function resolveArgs<A extends Args>(args: A, tokens: ArgToken[], resolveArgs?: ResolveArgs): {
  values: ArgValues<A>;
  positionals: string[];
  rest: string[];
  error: AggregateError | undefined;
  explicit: ArgExplicitlyProvided<A>;
  /**
   * Only with {@link ResolveArgs.passthrough | passthrough}.
   */
  passthroughIndices?: number[];
}

Resolve command line arguments, with passthrough that may be true or not, such as a boolean.

Type Parameters ​

NameDescription
A extends ArgsArguments, which is an object that defines the command line arguments.

Parameters ​

NameTypeDescription
argsAAn arguments that contains arguments schema.
tokensArgToken[]An array of tokens.
resolveArgsResolveArgsAn arguments that contains resolve arguments. (optional)

Returns ​

object — The result of one of the overloads above, by whether passthrough is true.

error ​

ts
error: AggregateError | undefined;

explicit ​

ts
explicit: ArgExplicitlyProvided<A>;

passthroughIndices ​

ts
passthroughIndices?: number[];

Only with passthrough.

positionals ​

ts
positionals: string[];

rest ​

ts
rest: string[];

values ​

ts
values: ArgValues<A>;

Released under the MIT License.