Built and signed on GitHub ActionsBuilt and signed on GitHub Actions
Tools for creating interactive command line tools
Examples
Usage
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
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
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
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, _: [] });
Functions
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.
Interfaces
Options for parseArgs.
- --: TDoubleDash
When
true, populate the result_with everything before the--and the result['--']with everything after the--. - alias: TAliases
An object mapping string names to strings or arrays of string argument names to use as aliases.
- boolean: TBooleans | ReadonlyArray<Extract<TBooleans, string>>
A boolean, string or array of strings to always treat as booleans. If
truewill treat all double hyphenated arguments without equal signs asboolean(e.g. affects--foo, not-for--foo=bar). Allbooleanarguments will be set tofalseby default. - collect: TCollectable | ReadonlyArray<Extract<TCollectable, string>>
A string or array of strings argument names to always treat as arrays. Collectable options can be used multiple times. All values will be collected into one array. If a non-collectable option is used multiple times, the last value is used.
- default: TDefault & Defaults<TBooleans, TStrings>
An object mapping string argument names to default values.
- negatable: TNegatable | ReadonlyArray<Extract<TNegatable, string>>
A string or array of strings argument names which can be negated by prefixing them with
--no-, like--no-config. - stopEarly: boolean
When
true, populate the result_with everything after the first non-option. - string: TStrings | ReadonlyArray<Extract<TStrings, string>>
A string or array of strings argument names to always treat as strings.
- unknown: () => unknownarg: string,key?: string,value?: unknown
A function which is invoked with a command line parameter not defined in the
optionsconfiguration object. If the function returnsfalse, the unknown option is not added toparsedArgs.
Type Aliases
& { _: Array<string | number>; }
& (boolean extends TDoubleDash ? DoubleDash : true extends TDoubleDash ? Required<DoubleDash> : Record<never, never>)
The value returned from parseArgs.