2018-12-19 13:06:31 -05:00
|
|
|
# flags
|
|
|
|
|
|
|
|
Command line arguments parser for Deno based on minimist
|
|
|
|
|
|
|
|
# Example
|
|
|
|
|
2018-12-24 10:28:01 -05:00
|
|
|
```ts
|
2019-02-26 00:35:50 -05:00
|
|
|
const { args } = Deno;
|
2019-03-06 10:24:53 -05:00
|
|
|
import { parse } from "https://deno.land/std/flags/mod.ts";
|
2018-12-19 13:06:31 -05:00
|
|
|
|
2018-12-21 13:02:52 -05:00
|
|
|
console.dir(parse(args));
|
2018-12-19 13:06:31 -05:00
|
|
|
```
|
|
|
|
|
|
|
|
```
|
|
|
|
$ 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
|
|
|
|
|
2018-12-21 13:02:52 -05:00
|
|
|
## const parsedArgs = parse(args, options = {});
|
2018-12-19 13:06:31 -05:00
|
|
|
|
2018-12-24 10:28:01 -05:00
|
|
|
`parsedArgs._` contains all the arguments that didn't have an option associated
|
|
|
|
with them.
|
2018-12-19 13:06:31 -05:00
|
|
|
|
|
|
|
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:
|
|
|
|
|
2018-12-24 10:28:01 -05:00
|
|
|
- `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:
|
2019-04-21 08:59:38 -04:00
|
|
|
```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" ] }
|
|
|
|
```
|
2018-12-24 10:28:01 -05:00
|
|
|
- `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`.
|