|
| 1 | +import {PackageJson} from 'type-fest'; |
| 2 | +import {Options as MinimistOptions} from 'minimist-options'; |
| 3 | + |
| 4 | +export interface Options { |
| 5 | + /** |
| 6 | + Define argument flags. |
| 7 | +
|
| 8 | + The key is the flag name and the value is an object with any of: |
| 9 | +
|
| 10 | + - `type`: Type of value. (Possible values: `string` `boolean`) |
| 11 | + - `alias`: Usually used to define a short flag alias. |
| 12 | + - `default`: Default value when the flag is not specified. |
| 13 | +
|
| 14 | + @example |
| 15 | + ``` |
| 16 | + flags: { |
| 17 | + unicorn: { |
| 18 | + type: 'string', |
| 19 | + alias: 'u', |
| 20 | + default: 'rainbow' |
| 21 | + } |
| 22 | + } |
| 23 | + ``` |
| 24 | + */ |
| 25 | + readonly flags?: MinimistOptions; |
| 26 | + |
| 27 | + /** |
| 28 | + Description to show above the help text. Default: The package.json `"description"` property. |
| 29 | +
|
| 30 | + Set it to `false` to disable it altogether. |
| 31 | + */ |
| 32 | + readonly description?: string | false; |
| 33 | + |
| 34 | + /** |
| 35 | + The help text you want shown. |
| 36 | +
|
| 37 | + 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. |
| 38 | +
|
| 39 | + The description will be shown above your help text automatically. |
| 40 | +
|
| 41 | + Set it to `false` to disable it altogether. |
| 42 | + */ |
| 43 | + readonly help?: string | false; |
| 44 | + |
| 45 | + /** |
| 46 | + Set a custom version output. Default: The package.json `"version"` property. |
| 47 | +
|
| 48 | + Set it to `false` to disable it altogether. |
| 49 | + */ |
| 50 | + readonly version?: string | false; |
| 51 | + |
| 52 | + /** |
| 53 | + 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. |
| 54 | + */ |
| 55 | + readonly autoHelp?: boolean; |
| 56 | + |
| 57 | + /** |
| 58 | + 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. |
| 59 | + */ |
| 60 | + readonly autoVersion?: boolean; |
| 61 | + |
| 62 | + /** |
| 63 | + package.json as an `Object`. Default: Closest package.json upwards. |
| 64 | +
|
| 65 | + _You most likely don't need this option._ |
| 66 | + */ |
| 67 | + readonly pkg?: {[key: string]: unknown}; |
| 68 | + |
| 69 | + /** |
| 70 | + Custom arguments object. |
| 71 | +
|
| 72 | + @default process.argv.slice(2) |
| 73 | + */ |
| 74 | + readonly argv?: ReadonlyArray<string>; |
| 75 | + |
| 76 | + /** |
| 77 | + Infer the argument type. |
| 78 | +
|
| 79 | + By default, the argument `5` in `$ foo 5` becomes a string. Enabling this would infer it as a number. |
| 80 | +
|
| 81 | + @default false |
| 82 | + */ |
| 83 | + readonly inferType?: boolean; |
| 84 | + |
| 85 | + /** |
| 86 | + 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`. |
| 87 | +
|
| 88 | + __Caution: Explicitly specifying undefined for `booleanDefault` has different meaning from omitting key itself.__ |
| 89 | +
|
| 90 | + @example |
| 91 | + ``` |
| 92 | + const cli = meow(` |
| 93 | + Usage |
| 94 | + $ foo |
| 95 | +
|
| 96 | + Options |
| 97 | + --rainbow, -r Include a rainbow |
| 98 | + --unicorn, -u Include a unicorn |
| 99 | + --no-sparkles Exclude sparkles |
| 100 | +
|
| 101 | + Examples |
| 102 | + $ foo |
| 103 | + 🌈 unicorns✨🌈 |
| 104 | + `, { |
| 105 | + booleanDefault: undefined, |
| 106 | + flags: { |
| 107 | + rainbow: { |
| 108 | + type: 'boolean', |
| 109 | + default: true, |
| 110 | + alias: 'r' |
| 111 | + }, |
| 112 | + unicorn: { |
| 113 | + type: 'boolean', |
| 114 | + default: false, |
| 115 | + alias: 'u' |
| 116 | + }, |
| 117 | + cake: { |
| 118 | + type: 'boolean', |
| 119 | + alias: 'c' |
| 120 | + }, |
| 121 | + sparkles: { |
| 122 | + type: 'boolean', |
| 123 | + default: true |
| 124 | + } |
| 125 | + } |
| 126 | + }); |
| 127 | +
|
| 128 | + //{ |
| 129 | + // flags: { |
| 130 | + // rainbow: true, |
| 131 | + // unicorn: false, |
| 132 | + // sparkles: true |
| 133 | + // }, |
| 134 | + // unnormalizedFlags: { |
| 135 | + // rainbow: true, |
| 136 | + // r: true, |
| 137 | + // unicorn: false, |
| 138 | + // u: false, |
| 139 | + // sparkles: true |
| 140 | + // }, |
| 141 | + // … |
| 142 | + //} |
| 143 | + ``` |
| 144 | + */ |
| 145 | + readonly booleanDefault?: boolean | null | undefined; |
| 146 | + |
| 147 | + /** |
| 148 | + 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. |
| 149 | +
|
| 150 | + @default true |
| 151 | + */ |
| 152 | + readonly hardRejection?: boolean; |
| 153 | +} |
| 154 | + |
| 155 | +export interface Result { |
| 156 | + /** |
| 157 | + Non-flag arguments. |
| 158 | + */ |
| 159 | + input: string[]; |
| 160 | + |
| 161 | + /** |
| 162 | + Flags converted to camelCase excluding aliases. |
| 163 | + */ |
| 164 | + flags: {[name: string]: unknown}; |
| 165 | + |
| 166 | + /** |
| 167 | + Flags converted camelCase including aliases. |
| 168 | + */ |
| 169 | + unnormalizedFlags: {[name: string]: unknown}; |
| 170 | + |
| 171 | + /** |
| 172 | + The `package.json` object. |
| 173 | + */ |
| 174 | + pkg: PackageJson; |
| 175 | + |
| 176 | + /** |
| 177 | + The help text used with `--help`. |
| 178 | + */ |
| 179 | + help: string; |
| 180 | + |
| 181 | + /** |
| 182 | + Show the help text and exit with code. |
| 183 | +
|
| 184 | + @param exitCode - The exit code to use. Default: `2`. |
| 185 | + */ |
| 186 | + showHelp(exitCode?: number): void; |
| 187 | + |
| 188 | + /** |
| 189 | + Show the version text and exit. |
| 190 | + */ |
| 191 | + showVersion(): void; |
| 192 | +} |
| 193 | + |
| 194 | +/** |
| 195 | +@param helpMessage - Shortcut for the `help` option. |
| 196 | +*/ |
| 197 | +export default function meow(helpMessage: string, options?: Options): Result; |
| 198 | +export default function meow(options?: Options): Result; |
0 commit comments