-
-
Notifications
You must be signed in to change notification settings - Fork 23
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add TypeScript definition #38
Merged
sindresorhus
merged 2 commits into
sindresorhus:master
from
BendingBender:typescript-defs
Apr 11, 2019
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since it will be a major release anyway, you can change to
readonly
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Didn't we want to wrap complex types in
Array
/ReadonlyArray
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So
readonly Array<string | RegExp>
doesn't work?