index.d.ts 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. import {PackageJson} from 'type-fest';
  2. declare namespace meow {
  3. type FlagType = 'string' | 'boolean' | 'number';
  4. /**
  5. Callback function to determine if a flag is required during runtime.
  6. @param flags - Contains the flags converted to camel-case excluding aliases.
  7. @param input - Contains the non-flag arguments.
  8. @returns True if the flag is required, otherwise false.
  9. */
  10. type IsRequiredPredicate = (flags: Readonly<AnyFlags>, input: readonly string[]) => boolean;
  11. interface Flag<Type extends FlagType, Default> {
  12. readonly type?: Type;
  13. readonly alias?: string;
  14. readonly default?: Default;
  15. readonly isRequired?: boolean | IsRequiredPredicate;
  16. readonly isMultiple?: boolean;
  17. }
  18. type StringFlag = Flag<'string', string>;
  19. type BooleanFlag = Flag<'boolean', boolean>;
  20. type NumberFlag = Flag<'number', number>;
  21. type AnyFlag = StringFlag | BooleanFlag | NumberFlag;
  22. type AnyFlags = {[key: string]: AnyFlag};
  23. interface Options<Flags extends AnyFlags> {
  24. /**
  25. Define argument flags.
  26. The key is the flag name and the value is an object with any of:
  27. - `type`: Type of value. (Possible values: `string` `boolean` `number`)
  28. - `alias`: Usually used to define a short flag alias.
  29. - `default`: Default value when the flag is not specified.
  30. - `isRequired`: Determine if the flag is required.
  31. If it's only known at runtime whether the flag is required or not you can pass a Function instead of a boolean, which based on the given flags and other non-flag arguments should decide if the flag is required.
  32. - `isMultiple`: Indicates a flag can be set multiple times. Values are turned into an array. (Default: false)
  33. @example
  34. ```
  35. flags: {
  36. unicorn: {
  37. type: 'string',
  38. alias: 'u',
  39. default: ['rainbow', 'cat'],
  40. isMultiple: true,
  41. isRequired: (flags, input) => {
  42. if (flags.otherFlag) {
  43. return true;
  44. }
  45. return false;
  46. }
  47. }
  48. }
  49. ```
  50. */
  51. readonly flags?: Flags;
  52. /**
  53. Description to show above the help text. Default: The package.json `"description"` property.
  54. Set it to `false` to disable it altogether.
  55. */
  56. readonly description?: string | false;
  57. /**
  58. The help text you want shown.
  59. The input is reindented and starting/ending newlines are trimmed which means you can use a [template literal](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/template_strings) without having to care about using the correct amount of indent.
  60. The description will be shown above your help text automatically.
  61. Set it to `false` to disable it altogether.
  62. */
  63. readonly help?: string | false;
  64. /**
  65. Set a custom version output. Default: The package.json `"version"` property.
  66. Set it to `false` to disable it altogether.
  67. */
  68. readonly version?: string | false;
  69. /**
  70. Automatically show the help text when the `--help` flag is present. Useful to set this value to `false` when a CLI manages child CLIs with their own help text.
  71. This option is only considered when there is only one argument in `process.argv`.
  72. */
  73. readonly autoHelp?: boolean;
  74. /**
  75. Automatically show the version text when the `--version` flag is present. Useful to set this value to `false` when a CLI manages child CLIs with their own version text.
  76. This option is only considered when there is only one argument in `process.argv`.
  77. */
  78. readonly autoVersion?: boolean;
  79. /**
  80. `package.json` as an `Object`. Default: Closest `package.json` upwards.
  81. _You most likely don't need this option._
  82. */
  83. readonly pkg?: {[key: string]: unknown};
  84. /**
  85. Custom arguments object.
  86. @default process.argv.slice(2)
  87. */
  88. readonly argv?: readonly string[];
  89. /**
  90. Infer the argument type.
  91. By default, the argument `5` in `$ foo 5` becomes a string. Enabling this would infer it as a number.
  92. @default false
  93. */
  94. readonly inferType?: boolean;
  95. /**
  96. Value of `boolean` flags not defined in `argv`. If set to `undefined` the flags not defined in `argv` will be excluded from the result. The `default` value set in `boolean` flags take precedence over `booleanDefault`.
  97. __Caution: Explicitly specifying undefined for `booleanDefault` has different meaning from omitting key itself.__
  98. @example
  99. ```
  100. import meow = require('meow');
  101. const cli = meow(`
  102. Usage
  103. $ foo
  104. Options
  105. --rainbow, -r Include a rainbow
  106. --unicorn, -u Include a unicorn
  107. --no-sparkles Exclude sparkles
  108. Examples
  109. $ foo
  110. 🌈 unicorns✨🌈
  111. `, {
  112. booleanDefault: undefined,
  113. flags: {
  114. rainbow: {
  115. type: 'boolean',
  116. default: true,
  117. alias: 'r'
  118. },
  119. unicorn: {
  120. type: 'boolean',
  121. default: false,
  122. alias: 'u'
  123. },
  124. cake: {
  125. type: 'boolean',
  126. alias: 'c'
  127. },
  128. sparkles: {
  129. type: 'boolean',
  130. default: true
  131. }
  132. }
  133. });
  134. //{
  135. // flags: {
  136. // rainbow: true,
  137. // unicorn: false,
  138. // sparkles: true
  139. // },
  140. // unnormalizedFlags: {
  141. // rainbow: true,
  142. // r: true,
  143. // unicorn: false,
  144. // u: false,
  145. // sparkles: true
  146. // },
  147. // …
  148. //}
  149. ```
  150. */
  151. readonly booleanDefault?: boolean | null | undefined;
  152. /**
  153. Whether to use [hard-rejection](https://github.com/sindresorhus/hard-rejection) or not. Disabling this can be useful if you need to handle `process.on('unhandledRejection')` yourself.
  154. @default true
  155. */
  156. readonly hardRejection?: boolean;
  157. }
  158. type TypedFlag<Flag extends AnyFlag> =
  159. Flag extends {type: 'number'}
  160. ? number
  161. : Flag extends {type: 'string'}
  162. ? string
  163. : Flag extends {type: 'boolean'}
  164. ? boolean
  165. : unknown;
  166. type PossiblyOptionalFlag<Flag extends AnyFlag, FlagType> =
  167. Flag extends {isRequired: true}
  168. ? FlagType
  169. : Flag extends {default: any}
  170. ? FlagType
  171. : FlagType | undefined;
  172. type TypedFlags<Flags extends AnyFlags> = {
  173. [F in keyof Flags]: Flags[F] extends {isMultiple: true}
  174. ? PossiblyOptionalFlag<Flags[F], Array<TypedFlag<Flags[F]>>>
  175. : PossiblyOptionalFlag<Flags[F], TypedFlag<Flags[F]>>
  176. };
  177. interface Result<Flags extends AnyFlags> {
  178. /**
  179. Non-flag arguments.
  180. */
  181. input: string[];
  182. /**
  183. Flags converted to camelCase excluding aliases.
  184. */
  185. flags: TypedFlags<Flags> & {[name: string]: unknown};
  186. /**
  187. Flags converted camelCase including aliases.
  188. */
  189. unnormalizedFlags: TypedFlags<Flags> & {[name: string]: unknown};
  190. /**
  191. The `package.json` object.
  192. */
  193. pkg: PackageJson;
  194. /**
  195. The help text used with `--help`.
  196. */
  197. help: string;
  198. /**
  199. Show the help text and exit with code.
  200. @param exitCode - The exit code to use. Default: `2`.
  201. */
  202. showHelp: (exitCode?: number) => void;
  203. /**
  204. Show the version text and exit.
  205. */
  206. showVersion: () => void;
  207. }
  208. }
  209. /**
  210. @param helpMessage - Shortcut for the `help` option.
  211. @example
  212. ```
  213. #!/usr/bin/env node
  214. 'use strict';
  215. import meow = require('meow');
  216. import foo = require('.');
  217. const cli = meow(`
  218. Usage
  219. $ foo <input>
  220. Options
  221. --rainbow, -r Include a rainbow
  222. Examples
  223. $ foo unicorns --rainbow
  224. 🌈 unicorns 🌈
  225. `, {
  226. flags: {
  227. rainbow: {
  228. type: 'boolean',
  229. alias: 'r'
  230. }
  231. }
  232. });
  233. //{
  234. // input: ['unicorns'],
  235. // flags: {rainbow: true},
  236. // ...
  237. //}
  238. foo(cli.input[0], cli.flags);
  239. ```
  240. */
  241. declare function meow<Flags extends meow.AnyFlags>(helpMessage: string, options?: meow.Options<Flags>): meow.Result<Flags>;
  242. declare function meow<Flags extends meow.AnyFlags>(options?: meow.Options<Flags>): meow.Result<Flags>;
  243. export = meow;