pax_global_header00006660000000000000000000000064135034226430014514gustar00rootroot0000000000000052 comment=5f9a3f9d2d62bd1aa068b3eb7b4f66443ca52d35 dargs-7.0.0/000077500000000000000000000000001350342264300126205ustar00rootroot00000000000000dargs-7.0.0/.editorconfig000066400000000000000000000002571350342264300153010ustar00rootroot00000000000000root = true [*] indent_style = tab end_of_line = lf charset = utf-8 trim_trailing_whitespace = true insert_final_newline = true [*.yml] indent_style = space indent_size = 2 dargs-7.0.0/.gitattributes000066400000000000000000000000231350342264300155060ustar00rootroot00000000000000* text=auto eol=lf dargs-7.0.0/.github/000077500000000000000000000000001350342264300141605ustar00rootroot00000000000000dargs-7.0.0/.github/funding.yml000066400000000000000000000001331350342264300163320ustar00rootroot00000000000000github: sindresorhus open_collective: sindresorhus custom: https://sindresorhus.com/donate dargs-7.0.0/.gitignore000066400000000000000000000000271350342264300146070ustar00rootroot00000000000000node_modules yarn.lock dargs-7.0.0/.npmrc000066400000000000000000000000231350342264300137330ustar00rootroot00000000000000package-lock=false dargs-7.0.0/.travis.yml000066400000000000000000000000651350342264300147320ustar00rootroot00000000000000language: node_js node_js: - '12' - '10' - '8' dargs-7.0.0/index.d.ts000066400000000000000000000072021350342264300145220ustar00rootroot00000000000000declare namespace dargs { interface Options { /** Keys or regex of keys to exclude. Takes precedence over `includes`. */ excludes?: ReadonlyArray; /** Keys or regex of keys to include. */ includes?: ReadonlyArray; /** 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`. */ aliases?: {[key: string]: string}; /** 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. @default true @example ``` import dargs = require('dargs'); console.log(dargs({foo: 'bar'}, {useEquals: false})); // [ // '--foo', 'bar' // ] ``` */ useEquals?: boolean; /** Make a single character option key `{a: true}` become a short flag `-a` instead of `--a`. @default true @example ``` import dargs = require('dargs'); console.log(dargs({a: true})); //=> ['-a'] console.log(dargs({a: true}, {shortFlag: false})); //=> ['--a'] ``` */ shortFlag?: boolean; /** Exclude `false` values. Can be useful when dealing with strict argument parsers that throw on unknown arguments like `--no-foo`. @default false */ ignoreFalse?: boolean; /** By default, camel-cased keys will be hyphenated. Enabling this will bypass the conversion process. @default false @example ``` import dargs = require('dargs'); console.log(dargs({fooBar: 'baz'})); //=> ['--foo-bar', 'baz'] console.log(dargs({fooBar: 'baz'}, {allowCamelCase: true})); //=> ['--fooBar', 'baz'] ``` */ allowCamelCase?: boolean; } } /** Reverse [`minimist`](https://github.com/substack/minimist). Convert an object of options into an array of command-line arguments. @param object - Object to convert to command-line arguments. @example ``` import 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' // ] ``` */ declare function dargs( object: { '--'?: string[]; _?: string[]; } & {[key: string]: string | boolean | number | readonly string[]}, options?: dargs.Options ): string[]; export = dargs; dargs-7.0.0/index.js000066400000000000000000000044721350342264300142740ustar00rootroot00000000000000'use strict'; const match = (array, value) => array.some(x => (x instanceof RegExp ? x.test(value) : x === value)); const dargs = (object, options) => { const arguments_ = []; let extraArguments = []; let separatedArguments = []; options = { useEquals: true, shortFlag: true, ...options }; const makeArguments = (key, value) => { const prefix = options.shortFlag && key.length === 1 ? '-' : '--'; const theKey = (options.allowCamelCase ? key : key.replace(/[A-Z]/g, '-$&').toLowerCase()); key = prefix + theKey; if (options.useEquals) { arguments_.push(key + (value ? `=${value}` : '')); } else { arguments_.push(key); if (value) { arguments_.push(value); } } }; const makeAliasArg = (key, value) => { arguments_.push(`-${key}`); if (value) { arguments_.push(value); } }; for (let [key, value] of Object.entries(object)) { let pushArguments = makeArguments; if (Array.isArray(options.excludes) && match(options.excludes, key)) { continue; } if (Array.isArray(options.includes) && !match(options.includes, key)) { continue; } if (typeof options.aliases === 'object' && options.aliases[key]) { key = options.aliases[key]; pushArguments = makeAliasArg; } if (key === '--') { if (!Array.isArray(value)) { throw new TypeError( `Expected key \`--\` to be Array, got ${typeof value}` ); } separatedArguments = value; continue; } if (key === '_') { if (!Array.isArray(value)) { throw new TypeError( `Expected key \`_\` to be Array, got ${typeof value}` ); } extraArguments = value; continue; } if (value === true) { pushArguments(key, ''); } if (value === false && !options.ignoreFalse) { pushArguments(`no-${key}`); } if (typeof value === 'string') { pushArguments(key, value); } if (typeof value === 'number' && !Number.isNaN(value)) { pushArguments(key, String(value)); } if (Array.isArray(value)) { for (const arrayValue of value) { pushArguments(key, arrayValue); } } } for (const argument of extraArguments) { arguments_.push(String(argument)); } if (separatedArguments.length > 0) { arguments_.push('--'); } for (const argument of separatedArguments) { arguments_.push(String(argument)); } return arguments_; }; module.exports = dargs; dargs-7.0.0/index.test-d.ts000066400000000000000000000014551350342264300155030ustar00rootroot00000000000000import {expectType, expectError} from 'tsd'; import dargs = require('.'); const object = { _: ['some', 'option'], foo: 'bar', hello: true, cake: false, camelCase: 5, multiple: ['value', 'value2'], pieKind: 'cherry', sad: ':(' }; const excludes = ['sad', /.*Kind$/]; const includes = ['camelCase', 'multiple', 'sad', /^pie.*/]; const aliases = {file: 'f'}; expectType(dargs(object, {excludes})); expectType(dargs(object, {includes})); expectType(dargs(object, {aliases})); expectType(dargs(object, {useEquals: false})); expectType(dargs(object, {shortFlag: true})); expectType(dargs(object, {ignoreFalse: true})); expectType(dargs(object, {allowCamelCase: true})); expectError(dargs({_: 'foo'})); expectError(dargs({'--': 'foo'})); dargs-7.0.0/license000066400000000000000000000021251350342264300141650ustar00rootroot00000000000000MIT License 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-7.0.0/package.json000066400000000000000000000014061350342264300151070ustar00rootroot00000000000000{ "name": "dargs", "version": "7.0.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": ">=8" }, "scripts": { "test": "xo && ava && tsd" }, "files": [ "index.js", "index.d.ts" ], "keywords": [ "reverse", "minimist", "options", "arguments", "args", "flags", "cli", "nopt", "commander", "binary", "command", "inverse", "opposite", "invert", "switch", "construct", "parse", "parser", "argv" ], "devDependencies": { "ava": "^2.1.0", "tsd": "^0.7.3", "xo": "^0.24.0" } } dargs-7.0.0/readme.md000066400000000000000000000070051350342264300144010ustar00rootroot00000000000000# 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 object = { _: ['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(object, {excludes})); /* [ '--foo=bar', '--hello', '--no-cake', '--camel-case=5', '--multiple=value', '--multiple=value2', 'some', 'option', '--', 'separated', 'option' ] */ console.log(dargs(object, {excludes, includes})); /* [ '--camel-case=5', '--multiple=value', '--multiple=value2' ] */ console.log(dargs(object, {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(object, options?) #### object 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 `object` 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. ```js const dargs = require('dargs'); console.log(dargs({foo: 'bar'}, {useEquals: false})); /* [ '--foo', 'bar' ] */ ``` ##### shortFlag Type: `boolean`
Default: `true` Make a single character option key `{a: true}` become a short flag `-a` instead of `--a`. ```js const dargs = require('dargs'); console.log(dargs({a: true})); //=> ['-a'] console.log(dargs({a: true}, {shortFlag: false})); //=> ['--a'] ``` ##### 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, camel-cased keys will be hyphenated. Enabling this will bypass the conversion process. ```js const dargs = require('dargs'); console.log(dargs({fooBar: 'baz'})); //=> ['--foo-bar', 'baz'] console.log(dargs({fooBar: 'baz'}, {allowCamelCase: true})); //=> ['--fooBar', 'baz'] ``` dargs-7.0.0/test.js000066400000000000000000000056101350342264300141370ustar00rootroot00000000000000import test from 'ava'; import dargs from '.'; const fixture = { _: ['some', 'option'], '--': ['other', 'args', '-z', '--camelCaseOpt'], 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(dargs(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', '--', 'other', 'args', '-z', '--camelCaseOpt' // Case unaffected for separated options ]); }); test('raises a TypeError if \'_\' value is not an Array', t => { t.throws(dargs.bind(dargs, {a: 'foo', _: 'baz'}), TypeError); }); test('raises a TypeError if \'--\' value is not an Array', t => { t.throws(dargs.bind(dargs, {a: 'foo', '--': 'baz'}), TypeError); }); test('useEquals options', t => { t.deepEqual(dargs(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', '--', 'other', 'args', '-z', '--camelCaseOpt' ]); }); test('exclude options', t => { t.deepEqual(dargs(fixture, {excludes: ['b', /^e$/, 'h', 'i']}), [ '-a=foo', '--no-c', '-d=5', '--camel-case-camel', 'some', 'option', '--', 'other', 'args', '-z', '--camelCaseOpt' ]); }); test('includes options', t => { t.deepEqual(dargs(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(dargs(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(dargs({foo: false}, {ignoreFalse: true}), []); }); test('aliases option', t => { t.deepEqual(dargs({a: 'foo', file: 'test'}, { aliases: {file: 'f'} }), [ '-a=foo', '-f', 'test' ]); }); test('includes and aliases options', t => { t.deepEqual(dargs(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(dargs(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', '--', 'other', 'args', '-z', '--camelCaseOpt' ]); }); test('shortFlag option', t => { t.deepEqual(dargs({a: 123, b: 'foo', foo: 'bar', camelCaseCamel: true}, { shortFlag: false }), [ '--a=123', '--b=foo', '--foo=bar', '--camel-case-camel' ]); });