(yargs: YargsInstance, shim: PlatformShim)
| 12 | } |
| 13 | |
| 14 | export function usage(yargs: YargsInstance, shim: PlatformShim) { |
| 15 | const __ = shim.y18n.__; |
| 16 | const self = {} as UsageInstance; |
| 17 | |
| 18 | // methods for ouputting/building failure message. |
| 19 | const fails: (FailureFunction | boolean)[] = []; |
| 20 | self.failFn = function failFn(f) { |
| 21 | fails.push(f); |
| 22 | }; |
| 23 | let failMessage: string | nil = null; |
| 24 | let globalFailMessage: string | nil = null; |
| 25 | let showHelpOnFail = true; |
| 26 | self.showHelpOnFail = function showHelpOnFailFn( |
| 27 | arg1: boolean | string = true, |
| 28 | arg2?: string |
| 29 | ) { |
| 30 | const [enabled, message] = |
| 31 | typeof arg1 === 'string' ? [true, arg1] : [arg1, arg2]; |
| 32 | |
| 33 | // If global context, set globalFailMessage |
| 34 | // Addresses: https://github.com/yargs/yargs/issues/2085 |
| 35 | if (yargs.getInternalMethods().isGlobalContext()) { |
| 36 | globalFailMessage = message; |
| 37 | } |
| 38 | |
| 39 | failMessage = message; |
| 40 | showHelpOnFail = enabled; |
| 41 | return self; |
| 42 | }; |
| 43 | |
| 44 | let failureOutput = false; |
| 45 | self.fail = function fail(msg, err) { |
| 46 | const logger = yargs.getInternalMethods().getLoggerInstance(); |
| 47 | |
| 48 | if (fails.length) { |
| 49 | for (let i = fails.length - 1; i >= 0; --i) { |
| 50 | const fail = fails[i]; |
| 51 | if (isBoolean(fail)) { |
| 52 | if (err) throw err; |
| 53 | else if (msg) throw Error(msg); |
| 54 | } else { |
| 55 | fail(msg, err, self); |
| 56 | } |
| 57 | } |
| 58 | } else { |
| 59 | if (yargs.getExitProcess()) setBlocking(true); |
| 60 | |
| 61 | // don't output failure message more than once |
| 62 | if (!failureOutput) { |
| 63 | failureOutput = true; |
| 64 | if (showHelpOnFail) { |
| 65 | yargs.showHelp('error'); |
| 66 | logger.error(); |
| 67 | } |
| 68 | if (msg || err) logger.error(msg || err); |
| 69 | const globalOrCommandFailMessage = failMessage || globalFailMessage; |
| 70 | if (globalOrCommandFailMessage) { |
| 71 | if (msg || err) logger.error(''); |
nothing calls this directly
no test coverage detected
searching dependent graphs…