vlad 6f123ff03e 18.07 | 6 年之前 | |
---|---|---|
.. | ||
index.js | 6 年之前 | |
license | 6 年之前 | |
package.json | 6 年之前 | |
readme.md | 6 年之前 |
Reverse
minimist
. Convert an object of options into an array of command-line arguments.
Useful when spawning command-line tools.
$ npm install --save dargs
const dargs = require('dargs');
const input = {
_: ['some', 'option'], // values in '_' will be appended to the end of the generated argument list
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'
]
*/
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'
]
*/
Type: Object
Object to convert to command-line arguments.
Type: Object
Type: Array
Keys or regex of keys to exclude. Takes precedence over includes
.
Type: Array
Keys or regex of keys to include.
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
.
Type: boolean
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.
console.log(dargs({foo: 'bar'}, {useEquals: false}));
/*
[
'--foo', 'bar'
]
*/
Type: boolean
Default: false
Exclude false
values. Can be useful when dealing with strict argument parsers that throw on unknown arguments like --no-foo
.
Type: boolean
Default: false
By default, camelCased keys will be hyphenated. Enabling this will bypass the conversion process.
console.log(dargs({fooBar: 'baz'}));
//=> ['--foo-bar', 'baz']
console.log(dargs({fooBar: 'baz'}, {allowCamelCase: true}));
//=> ['--fooBar', 'baz']
MIT © Sindre Sorhus