Skip to main content

@std/cli@1.0.10
Built and signed on GitHub Actions

Tools for creating interactive command line tools

This package works with DenoIt is unknown whether this package works with Bun
This package works with Deno
It is unknown whether this package works with Bun
JSR Score
94%
Published
3 days ago (1.0.10)
function parseArgs
parseArgs<
TArgs extends Values<TBooleans, TStrings, TCollectable, TNegatable, TDefaults, TAliases>,
TDoubleDash extends boolean | undefined = undefined,
TBooleans extends BooleanType = undefined,
TStrings extends StringType = undefined,
TCollectable extends Collectable = undefined,
TNegatable extends Negatable = undefined,
TDefaults extends Record<string, unknown> | undefined = undefined,
TAliases extends Aliases<TAliasArgNames, TAliasNames> | undefined = undefined,
TAliasArgNames extends string = string,
TAliasNames extends string = string,
>
(
args: string[],
options?: ParseOptions<TBooleans, TStrings, TCollectable, TNegatable, TDefaults, TAliases, TDoubleDash>,
): Args<TArgs, TDoubleDash>

Take a set of command line arguments, optionally with a set of options, and return an object representing the flags found in the passed arguments.

By default, any arguments starting with - or -- are considered boolean flags. If the argument name is followed by an equal sign (=) it is considered a key-value pair. Any arguments which could not be parsed are available in the _ property of the returned object.

By default, this module tries to determine the type of all arguments automatically and the return type of this function will have an index signature with any as value ({ [x: string]: any }).

If the string, boolean or collect option is set, the return value of this function will be fully typed and the index signature of the return type will change to { [x: string]: unknown }.

Any arguments after '--' will not be parsed and will end up in parsedArgs._.

Numeric-looking arguments will be returned as numbers unless options.string or options.boolean is set for that argument name.

See ParseOptions for more information.

Examples

Usage

import { parseArgs } from "@std/cli/parse-args";
import { assertEquals } from "@std/assert/equals";

// For proper use, one should use `parseArgs(Deno.args)`
assertEquals(parseArgs(["--foo", "--bar=baz", "./quux.txt"]), {
  foo: true,
  bar: "baz",
  _: ["./quux.txt"],
});

string and boolean options

Use string and boolean options to specify the type of the argument.

import { parseArgs } from "@std/cli/parse-args";
import { assertEquals } from "@std/assert/equals";

const args = parseArgs(["--foo", "--bar", "baz"], {
  boolean: ["foo"],
  string: ["bar"],
});

assertEquals(args, { foo: true, bar: "baz", _: [] });

collect option

collect option tells the parser to treat the option as an array. All values will be collected into one array. If a non-collectable option is used multiple times, the last value is used.

import { parseArgs } from "@std/cli/parse-args";
import { assertEquals } from "@std/assert/equals";

const args = parseArgs(["--foo", "bar", "--foo", "baz"], {
 collect: ["foo"],
});

assertEquals(args, { foo: ["bar", "baz"], _: [] });

negatable option

negatable option tells the parser to treat the option can be negated by prefixing them with --no-, like --no-config.

import { parseArgs } from "@std/cli/parse-args";
import { assertEquals } from "@std/assert/equals";

const args = parseArgs(["--no-foo"], {
  boolean: ["foo"],
  negatable: ["foo"],
});

assertEquals(args, { foo: false, _: [] });

Type Parameters

TDoubleDash extends boolean | undefined = undefined

Used by TArgs for the result.

TBooleans extends BooleanType = undefined

Used by TArgs for the result.

TStrings extends StringType = undefined

Used by TArgs for the result.

TCollectable extends Collectable = undefined

Used by TArgs for the result.

TNegatable extends Negatable = undefined

Used by TArgs for the result.

TDefaults extends Record<string, unknown> | undefined = undefined

Used by TArgs for the result.

TAliases extends Aliases<TAliasArgNames, TAliasNames> | undefined = undefined

Used by TArgs for the result.

TAliasArgNames extends string = string

Used by TArgs for the result.

TAliasNames extends string = string

Used by TArgs for the result.

Parameters

args: string[]

An array of command line arguments.

Options for the parse function.

Return Type

The parsed arguments.

Add Package

deno add jsr:@std/cli

Import symbol

import { parseArgs } from "@std/cli/parse-args";

---- OR ----

Import directly with a jsr specifier

import { parseArgs } from "jsr:@std/cli/parse-args";

Add Package

bunx jsr add @std/cli

Import symbol

import { parseArgs } from "@std/cli/parse-args";