index.d.ts 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. declare namespace dargs {
  2. interface Options {
  3. /**
  4. Keys or regex of keys to exclude. Takes precedence over `includes`.
  5. */
  6. excludes?: ReadonlyArray<string | RegExp>;
  7. /**
  8. Keys or regex of keys to include.
  9. */
  10. includes?: ReadonlyArray<string | RegExp>;
  11. /**
  12. 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`.
  13. */
  14. aliases?: {[key: string]: string};
  15. /**
  16. 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.
  17. @default true
  18. @example
  19. ```
  20. console.log(dargs({foo: 'bar'}, {useEquals: false}));
  21. // [
  22. // '--foo', 'bar'
  23. // ]
  24. ```
  25. */
  26. useEquals?: boolean;
  27. /**
  28. Exclude `false` values. Can be useful when dealing with strict argument parsers that throw on unknown arguments like `--no-foo`.
  29. @default false
  30. */
  31. ignoreFalse?: boolean;
  32. /**
  33. By default, camelCased keys will be hyphenated. Enabling this will bypass the conversion process.
  34. @default false
  35. @example
  36. ```
  37. console.log(dargs({fooBar: 'baz'}));
  38. //=> ['--foo-bar', 'baz']
  39. console.log(dargs({fooBar: 'baz'}, {allowCamelCase: true}));
  40. //=> ['--fooBar', 'baz']
  41. ```
  42. */
  43. allowCamelCase?: boolean;
  44. }
  45. }
  46. /**
  47. Reverse [`minimist`](https://github.com/substack/minimist). Convert an object of options into an array of command-line arguments.
  48. @param input - Object to convert to command-line arguments.
  49. @example
  50. ```
  51. import dargs = require('dargs');
  52. const input = {
  53. _: ['some', 'option'], // Values in '_' will be appended to the end of the generated argument list
  54. '--': ['separated', 'option'], // Values in '--' will be put at the very end of the argument list after the escape option (`--`)
  55. foo: 'bar',
  56. hello: true, // Results in only the key being used
  57. cake: false, // Prepends `no-` before the key
  58. camelCase: 5, // CamelCase is slugged to `camel-case`
  59. multiple: ['value', 'value2'], // Converted to multiple arguments
  60. pieKind: 'cherry',
  61. sad: ':('
  62. };
  63. const excludes = ['sad', /.*Kind$/]; // Excludes and includes accept regular expressions
  64. const includes = ['camelCase', 'multiple', 'sad', /^pie.+/];
  65. const aliases = {file: 'f'};
  66. console.log(dargs(input, {excludes}));
  67. // [
  68. // '--foo=bar',
  69. // '--hello',
  70. // '--no-cake',
  71. // '--camel-case=5',
  72. // '--multiple=value',
  73. // '--multiple=value2',
  74. // 'some',
  75. // 'option',
  76. // '--',
  77. // 'separated',
  78. // 'option'
  79. // ]
  80. console.log(dargs(input, {excludes, includes}));
  81. // [
  82. // '--camel-case=5',
  83. // '--multiple=value',
  84. // '--multiple=value2'
  85. // ]
  86. console.log(dargs(input, {includes}));
  87. // [
  88. // '--camel-case=5',
  89. // '--multiple=value',
  90. // '--multiple=value2',
  91. // '--pie-kind=cherry',
  92. // '--sad=:('
  93. // ]
  94. console.log(dargs({
  95. foo: 'bar',
  96. hello: true,
  97. file: 'baz'
  98. }, {aliases}));
  99. // [
  100. // '--foo=bar',
  101. // '--hello',
  102. // '-f', 'baz'
  103. // ]
  104. ```
  105. */
  106. declare function dargs(
  107. input: {
  108. '--'?: string[];
  109. _?: string[];
  110. } & {[key: string]: string | boolean | number | string[]},
  111. options?: dargs.Options
  112. ): string[];
  113. export = dargs;