Skip to main content

@std/cli@0.215.0

latest
Works with
It is unknown whether this package works with Browsers, Deno, Node.js, Cloudflare Workers, Bun
It is unknown whether this package works with Browsers
It is unknown whether this package works with Deno
It is unknown whether this package works with Node.js
It is unknown whether this package works with Cloudflare Workers
It is unknown whether this package works with Bun
JSR Score64%
Published2 years ago (0.215.0)
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[],
unnamed 1?: 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, the flags module tries to determine the type of all arguments automatically and the return type of the parseArgs method 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 the parseArgs method 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.

Examples

Example 1

import { parseArgs } from "@std/cli/parse_args";
const parsedArgs = parseArgs(Deno.args);

Example 2

import { parseArgs } from "@std/cli/parse_args";
const parsedArgs = parseArgs(["--foo", "--bar=baz", "./quux.txt"]);
// parsedArgs: { foo: true, bar: "baz", _: ["./quux.txt"] }

Type Parameters

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

Parameters

Return Type

Report package

Please provide a reason for reporting this package. We will review your report and take appropriate action.

Please review the JSR usage policy before submitting a report.

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";