Project

General

Profile

1
# minimist
2

    
3
parse argument options
4

    
5
This module is the guts of optimist's argument parser without all the
6
fanciful decoration.
7

    
8
[![browser support](https://ci.testling.com/substack/minimist.png)](http://ci.testling.com/substack/minimist)
9

    
10
[![build status](https://secure.travis-ci.org/substack/minimist.png)](http://travis-ci.org/substack/minimist)
11

    
12
# example
13

    
14
``` js
15
var argv = require('minimist')(process.argv.slice(2));
16
console.dir(argv);
17
```
18

    
19
```
20
$ node example/parse.js -a beep -b boop
21
{ _: [], a: 'beep', b: 'boop' }
22
```
23

    
24
```
25
$ node example/parse.js -x 3 -y 4 -n5 -abc --beep=boop foo bar baz
26
{ _: [ 'foo', 'bar', 'baz' ],
27
  x: 3,
28
  y: 4,
29
  n: 5,
30
  a: true,
31
  b: true,
32
  c: true,
33
  beep: 'boop' }
34
```
35

    
36
# methods
37

    
38
``` js
39
var parseArgs = require('minimist')
40
```
41

    
42
## var argv = parseArgs(args, opts={})
43

    
44
Return an argument object `argv` populated with the array arguments from `args`.
45

    
46
`argv._` contains all the arguments that didn't have an option associated with
47
them.
48

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

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

    
54
options can be:
55

    
56
* `opts.string` - a string or array of strings argument names to always treat as
57
strings
58
* `opts.boolean` - a boolean, string or array of strings to always treat as
59
booleans. if `true` will treat all double hyphenated arguments without equal signs
60
as boolean (e.g. affects `--foo`, not `-f` or `--foo=bar`)
61
* `opts.alias` - an object mapping string names to strings or arrays of string
62
argument names to use as aliases
63
* `opts.default` - an object mapping string argument names to default values
64
* `opts.stopEarly` - when true, populate `argv._` with everything after the
65
first non-option
66
* `opts['--']` - when true, populate `argv._` with everything before the `--`
67
and `argv['--']` with everything after the `--`. Here's an example:
68
* `opts.unknown` - a function which is invoked with a command line parameter not
69
defined in the `opts` configuration object. If the function returns `false`, the
70
unknown option is not added to `argv`.
71

    
72
```
73
> require('./')('one two three -- four five --six'.split(' '), { '--': true })
74
{ _: [ 'one', 'two', 'three' ],
75
  '--': [ 'four', 'five', '--six' ] }
76
```
77

    
78
Note that with `opts['--']` set, parsing for arguments still stops after the
79
`--`.
80

    
81
# install
82

    
83
With [npm](https://npmjs.org) do:
84

    
85
```
86
npm install minimist
87
```
88

    
89
# license
90

    
91
MIT
(5-5/5)