MCPcopy Index your code
hub / github.com/yargs/yargs / usage

Function usage

lib/usage.ts:14–753  ·  view source on GitHub ↗
(yargs: YargsInstance, shim: PlatformShim)

Source from the content-addressed store, hash-verified

12}
13
14export 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('');

Callers

nothing calls this directly

Calls 15

isBooleanFunction · 0.85
setBlockingFunction · 0.85
windowWidthFunction · 0.85
normalizeAliasesFunction · 0.85
maxWidthFunction · 0.85
addUngroupedKeysFunction · 0.85
isLongSwitchFunction · 0.85
addIndentationFunction · 0.85
deprecatedExtraFunction · 0.85
defaultStringFunction · 0.85
getTextFunction · 0.85
getIndentationFunction · 0.85

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…