Skip to content

Commit 12ab98c

Browse files
authored
docs(cli): add examples of parseArgs (#6283)
1 parent 08fe910 commit 12ab98c

File tree

1 file changed

+113
-3
lines changed

1 file changed

+113
-3
lines changed

cli/parse_args.ts

Lines changed: 113 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,69 @@
55
* Command line arguments parser based on
66
* {@link https://github.com/minimistjs/minimist | minimist}.
77
*
8+
* See {@linkcode parseArgs} for more information.
9+
*
810
* @example Usage
911
* ```ts
1012
* import { parseArgs } from "@std/cli/parse-args";
13+
* import { assertEquals } from "@std/assert/equals";
14+
*
15+
* // For proper use, one should use `parseArgs(Deno.args)`
16+
* assertEquals(parseArgs(["--foo", "--bar=baz", "./quux.txt"]), {
17+
* foo: true,
18+
* bar: "baz",
19+
* _: ["./quux.txt"],
20+
* });
21+
* ```
22+
*
23+
* @example `string` and `boolean` options
24+
*
25+
* Use `string` and `boolean` options to specify the type of the argument.
26+
*
27+
* ```ts
28+
* import { parseArgs } from "@std/cli/parse-args";
29+
* import { assertEquals } from "@std/assert/equals";
30+
*
31+
* const args = parseArgs(["--foo", "--bar", "baz"], {
32+
* boolean: ["foo"],
33+
* string: ["bar"],
34+
* });
35+
*
36+
* assertEquals(args, { foo: true, bar: "baz", _: [] });
37+
* ```
38+
*
39+
* @example `collect` option
40+
*
41+
* `collect` option tells the parser to treat the option as an array. All
42+
* values will be collected into one array. If a non-collectable option is used
43+
* multiple times, the last value is used.
44+
*
45+
* ```ts
46+
* import { parseArgs } from "@std/cli/parse-args";
47+
* import { assertEquals } from "@std/assert/equals";
48+
*
49+
* const args = parseArgs(["--foo", "bar", "--foo", "baz"], {
50+
* collect: ["foo"],
51+
* });
52+
*
53+
* assertEquals(args, { foo: ["bar", "baz"], _: [] });
54+
* ```
55+
*
56+
* @example `negatable` option
57+
*
58+
* `negatable` option tells the parser to treat the option can be negated by
59+
* prefixing them with `--no-`, like `--no-config`.
60+
*
61+
* ```ts
62+
* import { parseArgs } from "@std/cli/parse-args";
63+
* import { assertEquals } from "@std/assert/equals";
64+
*
65+
* const args = parseArgs(["--no-foo"], {
66+
* boolean: ["foo"],
67+
* negatable: ["foo"],
68+
* });
1169
*
12-
* const args = parseArgs(Deno.args);
70+
* assertEquals(args, { foo: false, _: [] });
1371
* ```
1472
*
1573
* @module
@@ -285,7 +343,7 @@ export interface ParseOptions<
285343
* const args = parseArgs(Deno.args, { "--": false }); // args equals { _: [ "a", "arg1" ] }
286344
* ```
287345
*
288-
* @example Double dash option is true
346+
* @example Double dash option is true
289347
* ```ts
290348
* // $ deno run example.ts -- a arg1
291349
* import { parseArgs } from "@std/cli/parse-args";
@@ -455,6 +513,8 @@ function parseBooleanString(value: unknown) {
455513
* Numeric-looking arguments will be returned as numbers unless `options.string`
456514
* or `options.boolean` is set for that argument name.
457515
*
516+
* See {@linkcode ParseOptions} for more information.
517+
*
458518
* @param args An array of command line arguments.
459519
* @param options Options for the parse function.
460520
*
@@ -474,7 +534,7 @@ function parseBooleanString(value: unknown) {
474534
* @example Usage
475535
* ```ts
476536
* import { parseArgs } from "@std/cli/parse-args";
477-
* import { assertEquals } from "@std/assert";
537+
* import { assertEquals } from "@std/assert/equals";
478538
*
479539
* // For proper use, one should use `parseArgs(Deno.args)`
480540
* assertEquals(parseArgs(["--foo", "--bar=baz", "./quux.txt"]), {
@@ -483,6 +543,56 @@ function parseBooleanString(value: unknown) {
483543
* _: ["./quux.txt"],
484544
* });
485545
* ```
546+
*
547+
* @example `string` and `boolean` options
548+
*
549+
* Use `string` and `boolean` options to specify the type of the argument.
550+
*
551+
* ```ts
552+
* import { parseArgs } from "@std/cli/parse-args";
553+
* import { assertEquals } from "@std/assert/equals";
554+
*
555+
* const args = parseArgs(["--foo", "--bar", "baz"], {
556+
* boolean: ["foo"],
557+
* string: ["bar"],
558+
* });
559+
*
560+
* assertEquals(args, { foo: true, bar: "baz", _: [] });
561+
* ```
562+
*
563+
* @example `collect` option
564+
*
565+
* `collect` option tells the parser to treat the option as an array. All
566+
* values will be collected into one array. If a non-collectable option is used
567+
* multiple times, the last value is used.
568+
*
569+
* ```ts
570+
* import { parseArgs } from "@std/cli/parse-args";
571+
* import { assertEquals } from "@std/assert/equals";
572+
*
573+
* const args = parseArgs(["--foo", "bar", "--foo", "baz"], {
574+
* collect: ["foo"],
575+
* });
576+
*
577+
* assertEquals(args, { foo: ["bar", "baz"], _: [] });
578+
* ```
579+
*
580+
* @example `negatable` option
581+
*
582+
* `negatable` option tells the parser to treat the option can be negated by
583+
* prefixing them with `--no-`, like `--no-config`.
584+
*
585+
* ```ts
586+
* import { parseArgs } from "@std/cli/parse-args";
587+
* import { assertEquals } from "@std/assert/equals";
588+
*
589+
* const args = parseArgs(["--no-foo"], {
590+
* boolean: ["foo"],
591+
* negatable: ["foo"],
592+
* });
593+
*
594+
* assertEquals(args, { foo: false, _: [] });
595+
* ```
486596
*/
487597
export function parseArgs<
488598
TArgs extends Values<

0 commit comments

Comments
 (0)