index.d.ts 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  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 = Record<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. Multiple values are provided by specifying the flag multiple times, for example, `$ foo -u rainbow -u cat`. Space- or comma-separated values are *not* supported.
  34. @example
  35. ```
  36. flags: {
  37. unicorn: {
  38. type: 'string',
  39. alias: 'u',
  40. default: ['rainbow', 'cat'],
  41. isMultiple: true,
  42. isRequired: (flags, input) => {
  43. if (flags.otherFlag) {
  44. return true;
  45. }
  46. return false;
  47. }
  48. }
  49. }
  50. ```
  51. */
  52. readonly flags?: Flags;
  53. /**
  54. Description to show above the help text. Default: The package.json `"description"` property.
  55. Set it to `false` to disable it altogether.
  56. */
  57. readonly description?: string | false;
  58. /**
  59. The help text you want shown.
  60. 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.
  61. The description will be shown above your help text automatically.
  62. Set it to `false` to disable it altogether.
  63. */
  64. readonly help?: string | false;
  65. /**
  66. Set a custom version output. Default: The package.json `"version"` property.
  67. Set it to `false` to disable it altogether.
  68. */
  69. readonly version?: string | false;
  70. /**
  71. 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.
  72. This option is only considered when there is only one argument in `process.argv`.
  73. */
  74. readonly autoHelp?: boolean;
  75. /**
  76. 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.
  77. This option is only considered when there is only one argument in `process.argv`.
  78. */
  79. readonly autoVersion?: boolean;
  80. /**
  81. `package.json` as an `Object`. Default: Closest `package.json` upwards.
  82. _You most likely don't need this option._
  83. */
  84. readonly pkg?: Record<string, unknown>;
  85. /**
  86. Custom arguments object.
  87. @default process.argv.slice(2)
  88. */
  89. readonly argv?: readonly string[];
  90. /**
  91. Infer the argument type.
  92. By default, the argument `5` in `$ foo 5` becomes a string. Enabling this would infer it as a number.
  93. @default false
  94. */
  95. readonly inferType?: boolean;
  96. /**
  97. Value of `boolean` flags not defined in `argv`.
  98. 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`.
  99. _Note: If used in conjunction with `isMultiple`, the default flag value is set to `[]`._
  100. __Caution: Explicitly specifying `undefined` for `booleanDefault` has different meaning from omitting key itself.__
  101. @example
  102. ```
  103. import meow = require('meow');
  104. const cli = meow(`
  105. Usage
  106. $ foo
  107. Options
  108. --rainbow, -r Include a rainbow
  109. --unicorn, -u Include a unicorn
  110. --no-sparkles Exclude sparkles
  111. Examples
  112. $ foo
  113. 🌈 unicorns✨🌈
  114. `, {
  115. booleanDefault: undefined,
  116. flags: {
  117. rainbow: {
  118. type: 'boolean',
  119. default: true,
  120. alias: 'r'
  121. },
  122. unicorn: {
  123. type: 'boolean',
  124. default: false,
  125. alias: 'u'
  126. },
  127. cake: {
  128. type: 'boolean',
  129. alias: 'c'
  130. },
  131. sparkles: {
  132. type: 'boolean',
  133. default: true
  134. }
  135. }
  136. });
  137. //{
  138. // flags: {
  139. // rainbow: true,
  140. // unicorn: false,
  141. // sparkles: true
  142. // },
  143. // unnormalizedFlags: {
  144. // rainbow: true,
  145. // r: true,
  146. // unicorn: false,
  147. // u: false,
  148. // sparkles: true
  149. // },
  150. // …
  151. //}
  152. ```
  153. */
  154. readonly booleanDefault?: boolean | null | undefined;
  155. /**
  156. 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.
  157. @default true
  158. */
  159. readonly hardRejection?: boolean;
  160. /**
  161. Whether to allow unknown flags or not.
  162. @default true
  163. */
  164. readonly allowUnknownFlags?: boolean;
  165. }
  166. type TypedFlag<Flag extends AnyFlag> =
  167. Flag extends {type: 'number'}
  168. ? number
  169. : Flag extends {type: 'string'}
  170. ? string
  171. : Flag extends {type: 'boolean'}
  172. ? boolean
  173. : unknown;
  174. type PossiblyOptionalFlag<Flag extends AnyFlag, FlagType> =
  175. Flag extends {isRequired: true}
  176. ? FlagType
  177. : Flag extends {default: any}
  178. ? FlagType
  179. : FlagType | undefined;
  180. type TypedFlags<Flags extends AnyFlags> = {
  181. [F in keyof Flags]: Flags[F] extends {isMultiple: true}
  182. ? PossiblyOptionalFlag<Flags[F], Array<TypedFlag<Flags[F]>>>
  183. : PossiblyOptionalFlag<Flags[F], TypedFlag<Flags[F]>>
  184. };
  185. interface Result<Flags extends AnyFlags> {
  186. /**
  187. Non-flag arguments.
  188. */
  189. input: string[];
  190. /**
  191. Flags converted to camelCase excluding aliases.
  192. */
  193. flags: TypedFlags<Flags> & Record<string, unknown>;
  194. /**
  195. Flags converted camelCase including aliases.
  196. */
  197. unnormalizedFlags: TypedFlags<Flags> & Record<string, unknown>;
  198. /**
  199. The `package.json` object.
  200. */
  201. pkg: PackageJson;
  202. /**
  203. The help text used with `--help`.
  204. */
  205. help: string;
  206. /**
  207. Show the help text and exit with code.
  208. @param exitCode - The exit code to use. Default: `2`.
  209. */
  210. showHelp: (exitCode?: number) => void;
  211. /**
  212. Show the version text and exit.
  213. */
  214. showVersion: () => void;
  215. }
  216. }
  217. /**
  218. @param helpMessage - Shortcut for the `help` option.
  219. @example
  220. ```
  221. #!/usr/bin/env node
  222. 'use strict';
  223. import meow = require('meow');
  224. import foo = require('.');
  225. const cli = meow(`
  226. Usage
  227. $ foo <input>
  228. Options
  229. --rainbow, -r Include a rainbow
  230. Examples
  231. $ foo unicorns --rainbow
  232. 🌈 unicorns 🌈
  233. `, {
  234. flags: {
  235. rainbow: {
  236. type: 'boolean',
  237. alias: 'r'
  238. }
  239. }
  240. });
  241. //{
  242. // input: ['unicorns'],
  243. // flags: {rainbow: true},
  244. // ...
  245. //}
  246. foo(cli.input[0], cli.flags);
  247. ```
  248. */
  249. declare function meow<Flags extends meow.AnyFlags>(helpMessage: string, options?: meow.Options<Flags>): meow.Result<Flags>;
  250. declare function meow<Flags extends meow.AnyFlags>(options?: meow.Options<Flags>): meow.Result<Flags>;
  251. export = meow;