-
-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
9265099
commit 24f4dd1
Showing
6 changed files
with
244 additions
and
56 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import {expectType, expectError} from 'tsd'; | ||
import dargs = require('.'); | ||
|
||
const input = { | ||
_: ['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<string[]>(dargs(input, {excludes})); | ||
expectType<string[]>(dargs(input, {includes})); | ||
expectType<string[]>(dargs(input, {aliases})); | ||
expectType<string[]>(dargs(input, {useEquals: false})); | ||
expectType<string[]>(dargs(input, {ignoreFalse: true})); | ||
expectType<string[]>(dargs(input, {allowCamelCase: true})); | ||
|
||
expectError(dargs({_: 'foo'})); | ||
expectError(dargs({'--': 'foo'})); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.