summaryrefslogtreecommitdiff
path: root/std/flags/README.md
blob: 41692b619dcc08f359faaa3aca6ce0faf9069b47 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# flags

Command line arguments parser for Deno based on minimist

# Example

```ts
const { args } = Deno;
import { parse } from "https://deno.land/std/flags/mod.ts";

console.dir(parse(args));
```

```
$ deno example.ts -a beep -b boop
{ _: [], a: 'beep', b: 'boop' }
```

```
$ deno example.ts -x 3 -y 4 -n5 -abc --beep=boop foo bar baz
{ _: [ 'foo', 'bar', 'baz' ],
  x: 3,
  y: 4,
  n: 5,
  a: true,
  b: true,
  c: true,
  beep: 'boop' }
```

# API

## const parsedArgs = parse(args, options = {});

`parsedArgs._` contains all the arguments that didn't have an option associated
with them.

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

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

options can be:

- `options.string` - a string or array of strings argument names to always treat
  as strings
- `options.boolean` - a boolean, string or array of strings to always treat as
  booleans. if `true` will treat all double hyphenated arguments without equal
  signs as boolean (e.g. affects `--foo`, not `-f` or `--foo=bar`)
- `options.alias` - an object mapping string names to strings or arrays of
  string argument names to use as aliases
- `options.default` - an object mapping string argument names to default values
- `options.stopEarly` - when true, populate `parsedArgs._` with everything after
  the first non-option
- `options['--']` - when true, populate `parsedArgs._` with everything before
  the `--` and `parsedArgs['--']` with everything after the `--`. Here's an
  example:
  ```ts
  const { args } = Deno;
  import { parse } from "https://deno.land/std/flags/mod.ts";
  // options['--'] is now set to false
  console.dir(parse(args, { "--": false }));
  // $ deno example.ts -- a arg1
  // output: { _: [ "example.ts", "a", "arg1" ] }
  // options['--'] is now set to true
  console.dir(parse(args, { "--": true }));
  // $ deno example.ts -- a arg1
  // output: { _: [ "example.ts" ], --: [ "a", "arg1" ] }
  ```
- `options.unknown` - a function which is invoked with a command line parameter
  not defined in the `options` configuration object. If the function returns
  `false`, the unknown option is not added to `parsedArgs`.