123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158 |
- 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
- ```
- 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;
|