333
schangxiang@126.com
2025-09-19 18966e02fb573c7e2bb0c6426ed792b38b910940
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# dargs [![Build Status](https://travis-ci.org/sindresorhus/dargs.svg?branch=master)](https://travis-ci.org/sindresorhus/dargs)
 
> Reverse [`minimist`](https://github.com/substack/minimist). Convert an object of options into an array of command-line arguments.
 
Useful when spawning command-line tools.
 
 
## Install
 
```
$ npm install dargs
```
 
 
## Usage
 
```js
const dargs = require('dargs');
 
const input = {
    _: ['some', 'option'],          // Values in '_' will be appended to the end of the generated argument list
    '--': ['separated', 'option'],  // Values in '--' will be put at the very end of the argument list after the escape option (`--`)
    foo: 'bar',
    hello: true,                    // Results in only the key being used
    cake: false,                    // Prepends `no-` before the key
    camelCase: 5,                   // CamelCase is slugged to `camel-case`
    multiple: ['value', 'value2'],  // Converted to multiple arguments
    pieKind: 'cherry',
    sad: ':('
};
 
const excludes = ['sad', /.*Kind$/];  // Excludes and includes accept regular expressions
const includes = ['camelCase', 'multiple', 'sad', /^pie.*/];
const aliases = {file: 'f'};
 
console.log(dargs(input, {excludes}));
/*
[
    '--foo=bar',
    '--hello',
    '--no-cake',
    '--camel-case=5',
    '--multiple=value',
    '--multiple=value2',
    'some',
    'option',
    '--',
    'separated',
    'option'
]
*/
 
console.log(dargs(input, {excludes, includes}));
/*
[
    '--camel-case=5',
    '--multiple=value',
    '--multiple=value2'
]
*/
 
 
console.log(dargs(input, {includes}));
/*
[
    '--camel-case=5',
    '--multiple=value',
    '--multiple=value2',
    '--pie-kind=cherry',
    '--sad=:('
]
*/
 
 
console.log(dargs({
    foo: 'bar',
    hello: true,
    file: 'baz'
}, {aliases}));
/*
[
    '--foo=bar',
    '--hello',
    '-f', 'baz'
]
*/
```
 
 
## API
 
### dargs(input, [options])
 
#### input
 
Type: `Object`
 
Object to convert to command-line arguments.
 
#### options
 
Type: `Object`
 
##### excludes
 
Type: `Array`
 
Keys or regex of keys to exclude. Takes precedence over `includes`.
 
##### includes
 
Type: `Array`
 
Keys or regex of keys to include.
 
##### aliases
 
Type: `Object`
 
Maps keys in `input` to an aliased name. Matching keys are converted to arguments with a single dash (`-`) in front of the aliased key and the value in a separate array item. Keys are still affected by `includes` and `excludes`.
 
##### useEquals
 
Type: `boolean`<br>
Default: `true`
 
Setting this to `false` makes it return the key and value as separate array items instead of using a `=` separator in one item. This can be useful for tools that doesn't support `--foo=bar` style flags.
 
###### Example
 
```js
console.log(dargs({foo: 'bar'}, {useEquals: false}));
/*
[
    '--foo', 'bar'
]
*/
```
 
##### ignoreFalse
 
Type: `boolean`<br>
Default: `false`
 
Exclude `false` values. Can be useful when dealing with strict argument parsers that throw on unknown arguments like `--no-foo`.
 
##### allowCamelCase
 
Type: `boolean`<br>
Default: `false`
 
By default, camelCased keys will be hyphenated. Enabling this will bypass the conversion process.
 
###### Example
 
```js
console.log(dargs({fooBar: 'baz'}));
//=> ['--foo-bar', 'baz']
 
console.log(dargs({fooBar: 'baz'}, {allowCamelCase: true}));
//=> ['--fooBar', 'baz']
```
 
 
## License
 
MIT © [Sindre Sorhus](https://sindresorhus.com)