mirror of
https://github.com/kennethreitz/bake.git
synced 2026-06-05 23:00:17 +00:00
cleanup
This commit is contained in:
+136
@@ -0,0 +1,136 @@
|
||||
declare namespace dargs {
|
||||
interface Options {
|
||||
/**
|
||||
Keys or regex of keys to exclude. Takes precedence over `includes`.
|
||||
*/
|
||||
excludes?: ReadonlyArray<string | RegExp>;
|
||||
|
||||
/**
|
||||
Keys or regex of keys to include.
|
||||
*/
|
||||
includes?: ReadonlyArray<string | RegExp>;
|
||||
|
||||
/**
|
||||
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
|
||||
```
|
||||
console.log(dargs({foo: 'bar'}, {useEquals: false}));
|
||||
// [
|
||||
// '--foo', 'bar'
|
||||
// ]
|
||||
```
|
||||
*/
|
||||
useEquals?: 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, camelCased keys will be hyphenated. Enabling this will bypass the conversion process.
|
||||
|
||||
@default false
|
||||
|
||||
@example
|
||||
```
|
||||
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 input - 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(
|
||||
input: {
|
||||
'--'?: string[];
|
||||
_?: string[];
|
||||
} & {[key: string]: string | boolean | number | string[]},
|
||||
options?: dargs.Options
|
||||
): string[];
|
||||
|
||||
export = dargs;
|
||||
+119
@@ -0,0 +1,119 @@
|
||||
'use strict';
|
||||
|
||||
const match = (array, value) =>
|
||||
array.some(x => (x instanceof RegExp ? x.test(value) : x === value));
|
||||
|
||||
const dargs = (input, options) => {
|
||||
const args = [];
|
||||
let extraArgs = [];
|
||||
let separatedArgs = [];
|
||||
|
||||
options = Object.assign({
|
||||
useEquals: true
|
||||
}, options);
|
||||
|
||||
const makeArg = (key, value) => {
|
||||
key =
|
||||
'--' +
|
||||
(options.allowCamelCase ?
|
||||
key :
|
||||
key.replace(/[A-Z]/g, '-$&').toLowerCase());
|
||||
|
||||
if (options.useEquals) {
|
||||
args.push(key + (value ? `=${value}` : ''));
|
||||
} else {
|
||||
args.push(key);
|
||||
|
||||
if (value) {
|
||||
args.push(value);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
const makeAliasArg = (key, value) => {
|
||||
args.push(`-${key}`);
|
||||
|
||||
if (value) {
|
||||
args.push(value);
|
||||
}
|
||||
};
|
||||
|
||||
// TODO: Use Object.entries() when targeting Node.js 8
|
||||
for (let key of Object.keys(input)) {
|
||||
const value = input[key];
|
||||
let pushArg = makeArg;
|
||||
|
||||
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];
|
||||
pushArg = makeAliasArg;
|
||||
}
|
||||
|
||||
if (key === '--') {
|
||||
if (!Array.isArray(value)) {
|
||||
throw new TypeError(
|
||||
`Expected key \`--\` to be Array, got ${typeof value}`
|
||||
);
|
||||
}
|
||||
|
||||
separatedArgs = value;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (key === '_') {
|
||||
if (!Array.isArray(value)) {
|
||||
throw new TypeError(
|
||||
`Expected key \`_\` to be Array, got ${typeof value}`
|
||||
);
|
||||
}
|
||||
|
||||
extraArgs = value;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (value === true) {
|
||||
pushArg(key, '');
|
||||
}
|
||||
|
||||
if (value === false && !options.ignoreFalse) {
|
||||
pushArg(`no-${key}`);
|
||||
}
|
||||
|
||||
if (typeof value === 'string') {
|
||||
pushArg(key, value);
|
||||
}
|
||||
|
||||
if (typeof value === 'number' && !Number.isNaN(value)) {
|
||||
pushArg(key, String(value));
|
||||
}
|
||||
|
||||
if (Array.isArray(value)) {
|
||||
for (const arrayValue of value) {
|
||||
pushArg(key, arrayValue);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for (const x of extraArgs) {
|
||||
args.push(String(x));
|
||||
}
|
||||
|
||||
if (separatedArgs.length > 0) {
|
||||
args.push('--');
|
||||
}
|
||||
|
||||
for (const x of separatedArgs) {
|
||||
args.push(String(x));
|
||||
}
|
||||
|
||||
return args;
|
||||
};
|
||||
|
||||
module.exports = dargs;
|
||||
+9
@@ -0,0 +1,9 @@
|
||||
MIT License
|
||||
|
||||
Copyright (c) Sindre Sorhus <sindresorhus@gmail.com> (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.
|
||||
+50
@@ -0,0 +1,50 @@
|
||||
{
|
||||
"name": "dargs",
|
||||
"version": "6.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": ">=6"
|
||||
},
|
||||
"scripts": {
|
||||
"test": "xo && ava && tsd"
|
||||
},
|
||||
"files": [
|
||||
"index.js",
|
||||
"index.d.ts"
|
||||
],
|
||||
"keywords": [
|
||||
"reverse",
|
||||
"minimist",
|
||||
"options",
|
||||
"arguments",
|
||||
"args",
|
||||
"flags",
|
||||
"cli",
|
||||
"nopt",
|
||||
"commander",
|
||||
"bin",
|
||||
"binary",
|
||||
"command",
|
||||
"cmd",
|
||||
"inverse",
|
||||
"opposite",
|
||||
"invert",
|
||||
"switch",
|
||||
"construct",
|
||||
"parse",
|
||||
"parser",
|
||||
"argv"
|
||||
],
|
||||
"devDependencies": {
|
||||
"ava": "^1.4.1",
|
||||
"tsd": "^0.7.2",
|
||||
"xo": "^0.24.0"
|
||||
}
|
||||
}
|
||||
+167
@@ -0,0 +1,167 @@
|
||||
# dargs [](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)
|
||||
Reference in New Issue
Block a user