pax_global_header00006660000000000000000000000064127710102140014505gustar00rootroot0000000000000052 comment=6f796ea830d0a6b1d75592f6c28ac619339f2965 dargs-5.1.0/000077500000000000000000000000001277101021400126105ustar00rootroot00000000000000dargs-5.1.0/.editorconfig000066400000000000000000000002761277101021400152720ustar00rootroot00000000000000root = true [*] indent_style = tab end_of_line = lf charset = utf-8 trim_trailing_whitespace = true insert_final_newline = true [{package.json,*.yml}] indent_style = space indent_size = 2 dargs-5.1.0/.gitattributes000066400000000000000000000000351277101021400155010ustar00rootroot00000000000000* text=auto *.js text eol=lf dargs-5.1.0/.gitignore000066400000000000000000000000151277101021400145740ustar00rootroot00000000000000node_modules dargs-5.1.0/.travis.yml000066400000000000000000000000671277101021400147240ustar00rootroot00000000000000sudo: false language: node_js node_js: - '6' - '4' dargs-5.1.0/index.js000066400000000000000000000032131277101021400142540ustar00rootroot00000000000000'use strict'; const match = (arr, val) => arr.some(x => x instanceof RegExp ? x.test(val) : x === val); module.exports = (input, opts) => { const args = []; let extraArgs = []; opts = Object.assign({ useEquals: true }, opts); const makeArg = (key, val) => { key = '--' + (opts.allowCamelCase ? key : key.replace(/[A-Z]/g, '-$&').toLowerCase()); if (opts.useEquals) { args.push(key + (val ? `=${val}` : '')); } else { args.push(key); if (val) { args.push(val); } } }; const makeAliasArg = (key, val) => { args.push(`-${key}`); if (val) { args.push(val); } }; // TODO: use for-of loop and Object.entries when targeting Node.js 6 Object.keys(input).forEach(key => { const val = input[key]; let pushArg = makeArg; if (Array.isArray(opts.excludes) && match(opts.excludes, key)) { return; } if (Array.isArray(opts.includes) && !match(opts.includes, key)) { return; } if (typeof opts.aliases === 'object' && opts.aliases[key]) { key = opts.aliases[key]; pushArg = makeAliasArg; } if (key === '_') { if (!Array.isArray(val)) { throw new TypeError(`Expected key \`_\` to be Array, got ${typeof val}`); } extraArgs = val; return; } if (val === true) { pushArg(key, ''); } if (val === false && !opts.ignoreFalse) { pushArg(`no-${key}`); } if (typeof val === 'string') { pushArg(key, val); } if (typeof val === 'number' && !Number.isNaN(val)) { pushArg(key, String(val)); } if (Array.isArray(val)) { val.forEach(arrVal => { pushArg(key, arrVal); }); } }); for (const x of extraArgs) { args.push(String(x)); } return args; }; dargs-5.1.0/license000066400000000000000000000021371277101021400141600ustar00rootroot00000000000000The MIT License (MIT) Copyright (c) Sindre Sorhus (sindresorhus.com) Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. dargs-5.1.0/package.json000066400000000000000000000015201277101021400150740ustar00rootroot00000000000000{ "name": "dargs", "version": "5.1.0", "description": "Reverse minimist. Convert an object of options into an array of command-line arguments.", "license": "MIT", "repository": "sindresorhus/dargs", "author": { "name": "Sindre Sorhus", "email": "sindresorhus@gmail.com", "url": "sindresorhus.com" }, "engines": { "node": ">=4" }, "scripts": { "test": "xo && ava" }, "files": [ "index.js" ], "keywords": [ "reverse", "minimist", "options", "arguments", "args", "flags", "cli", "nopt", "commander", "bin", "binary", "command", "cmd", "inverse", "opposite", "invert", "switch", "construct", "parse", "parser", "argv" ], "devDependencies": { "ava": "*", "xo": "*" }, "xo": { "esnext": true } } dargs-5.1.0/readme.md000066400000000000000000000060641277101021400143750ustar00rootroot00000000000000# 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 --save dargs ``` ## Usage ```js 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' ] */ ``` ## 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`
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`
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`
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) dargs-5.1.0/test.js000066400000000000000000000043221277101021400141260ustar00rootroot00000000000000import test from 'ava'; import m from './'; const fixture = { _: ['some', 'option'], a: 'foo', b: true, c: false, d: 5, e: ['foo', 'bar'], f: null, g: undefined, h: 'with a space', i: 'let\'s try quotes', camelCaseCamel: true }; test('convert options to cli flags', t => { t.deepEqual(m(fixture), [ '--a=foo', '--b', '--no-c', '--d=5', '--e=foo', '--e=bar', '--h=with a space', '--i=let\'s try quotes', '--camel-case-camel', 'some', 'option' ]); }); test('raises a TypeError if \'_\' value is not an Array', t => { t.throws(m.bind(m, {a: 'foo', _: 'baz'}), TypeError); }); test('useEquals options', t => { t.deepEqual(m(fixture, { useEquals: false }), [ '--a', 'foo', '--b', '--no-c', '--d', '5', '--e', 'foo', '--e', 'bar', '--h', 'with a space', '--i', 'let\'s try quotes', '--camel-case-camel', 'some', 'option' ]); }); test('exclude options', t => { t.deepEqual(m(fixture, {excludes: ['b', /^e$/, 'h', 'i']}), [ '--a=foo', '--no-c', '--d=5', '--camel-case-camel', 'some', 'option' ]); }); test('includes options', t => { t.deepEqual(m(fixture, {includes: ['a', 'c', 'd', 'e', /^camelCase.*/]}), [ '--a=foo', '--no-c', '--d=5', '--e=foo', '--e=bar', '--camel-case-camel' ]); }); test('excludes and includes options', t => { t.deepEqual(m(fixture, { excludes: ['a', 'd'], includes: ['a', 'c', /^[de]$/, 'camelCaseCamel'] }), [ '--no-c', '--e=foo', '--e=bar', '--camel-case-camel' ]); }); test('option to ignore false values', t => { t.deepEqual(m({foo: false}, {ignoreFalse: true}), []); }); test('aliases option', t => { t.deepEqual(m({a: 'foo', file: 'test'}, { aliases: {file: 'f'} }), [ '--a=foo', '-f', 'test' ]); }); test('includes and aliases options', t => { t.deepEqual(m(fixture, { includes: ['a', 'c', 'd', 'e', 'camelCaseCamel'], aliases: {a: 'a'} }), [ '-a', 'foo', '--no-c', '--d=5', '--e=foo', '--e=bar', '--camel-case-camel' ]); }); test('camelCase option', t => { t.deepEqual(m(fixture, { allowCamelCase: true }), [ '--a=foo', '--b', '--no-c', '--d=5', '--e=foo', '--e=bar', '--h=with a space', '--i=let\'s try quotes', '--camelCaseCamel', 'some', 'option' ]); });