yargs-factory.js 75 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501
  1. var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
  2. if (kind === "m") throw new TypeError("Private method is not writable");
  3. if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
  4. if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
  5. return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
  6. };
  7. var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
  8. if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
  9. if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
  10. return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
  11. };
  12. var _YargsInstance_command, _YargsInstance_cwd, _YargsInstance_context, _YargsInstance_completion, _YargsInstance_completionCommand, _YargsInstance_defaultShowHiddenOpt, _YargsInstance_exitError, _YargsInstance_detectLocale, _YargsInstance_emittedWarnings, _YargsInstance_exitProcess, _YargsInstance_frozens, _YargsInstance_globalMiddleware, _YargsInstance_groups, _YargsInstance_hasOutput, _YargsInstance_helpOpt, _YargsInstance_isGlobalContext, _YargsInstance_logger, _YargsInstance_output, _YargsInstance_options, _YargsInstance_parentRequire, _YargsInstance_parserConfig, _YargsInstance_parseFn, _YargsInstance_parseContext, _YargsInstance_pkgs, _YargsInstance_preservedGroups, _YargsInstance_processArgs, _YargsInstance_recommendCommands, _YargsInstance_shim, _YargsInstance_strict, _YargsInstance_strictCommands, _YargsInstance_strictOptions, _YargsInstance_usage, _YargsInstance_versionOpt, _YargsInstance_validation;
  13. import { command as Command, } from './command.js';
  14. import { assertNotStrictEqual, assertSingleKey, objectKeys, } from './typings/common-types.js';
  15. import { YError } from './yerror.js';
  16. import { usage as Usage } from './usage.js';
  17. import { argsert } from './argsert.js';
  18. import { completion as Completion, } from './completion.js';
  19. import { validation as Validation, } from './validation.js';
  20. import { objFilter } from './utils/obj-filter.js';
  21. import { applyExtends } from './utils/apply-extends.js';
  22. import { applyMiddleware, GlobalMiddleware, } from './middleware.js';
  23. import { isPromise } from './utils/is-promise.js';
  24. import { maybeAsyncResult } from './utils/maybe-async-result.js';
  25. import setBlocking from './utils/set-blocking.js';
  26. export function YargsFactory(_shim) {
  27. return (processArgs = [], cwd = _shim.process.cwd(), parentRequire) => {
  28. const yargs = new YargsInstance(processArgs, cwd, parentRequire, _shim);
  29. Object.defineProperty(yargs, 'argv', {
  30. get: () => {
  31. return yargs.parse();
  32. },
  33. enumerable: true,
  34. });
  35. yargs.help();
  36. yargs.version();
  37. return yargs;
  38. };
  39. }
  40. const kCopyDoubleDash = Symbol('copyDoubleDash');
  41. const kCreateLogger = Symbol('copyDoubleDash');
  42. const kDeleteFromParserHintObject = Symbol('deleteFromParserHintObject');
  43. const kEmitWarning = Symbol('emitWarning');
  44. const kFreeze = Symbol('freeze');
  45. const kGetDollarZero = Symbol('getDollarZero');
  46. const kGetParserConfiguration = Symbol('getParserConfiguration');
  47. const kGuessLocale = Symbol('guessLocale');
  48. const kGuessVersion = Symbol('guessVersion');
  49. const kParsePositionalNumbers = Symbol('parsePositionalNumbers');
  50. const kPkgUp = Symbol('pkgUp');
  51. const kPopulateParserHintArray = Symbol('populateParserHintArray');
  52. const kPopulateParserHintSingleValueDictionary = Symbol('populateParserHintSingleValueDictionary');
  53. const kPopulateParserHintArrayDictionary = Symbol('populateParserHintArrayDictionary');
  54. const kPopulateParserHintDictionary = Symbol('populateParserHintDictionary');
  55. const kSanitizeKey = Symbol('sanitizeKey');
  56. const kSetKey = Symbol('setKey');
  57. const kUnfreeze = Symbol('unfreeze');
  58. const kValidateAsync = Symbol('validateAsync');
  59. const kGetCommandInstance = Symbol('getCommandInstance');
  60. const kGetContext = Symbol('getContext');
  61. const kGetHasOutput = Symbol('getHasOutput');
  62. const kGetLoggerInstance = Symbol('getLoggerInstance');
  63. const kGetParseContext = Symbol('getParseContext');
  64. const kGetUsageInstance = Symbol('getUsageInstance');
  65. const kGetValidationInstance = Symbol('getValidationInstance');
  66. const kHasParseCallback = Symbol('hasParseCallback');
  67. const kIsGlobalContext = Symbol('isGlobalContext');
  68. const kPostProcess = Symbol('postProcess');
  69. const kRebase = Symbol('rebase');
  70. const kReset = Symbol('reset');
  71. const kRunYargsParserAndExecuteCommands = Symbol('runYargsParserAndExecuteCommands');
  72. const kRunValidation = Symbol('runValidation');
  73. const kSetHasOutput = Symbol('setHasOutput');
  74. const kTrackManuallySetKeys = Symbol('kTrackManuallySetKeys');
  75. export class YargsInstance {
  76. constructor(processArgs = [], cwd, parentRequire, shim) {
  77. this.customScriptName = false;
  78. this.parsed = false;
  79. _YargsInstance_command.set(this, void 0);
  80. _YargsInstance_cwd.set(this, void 0);
  81. _YargsInstance_context.set(this, { commands: [], fullCommands: [] });
  82. _YargsInstance_completion.set(this, null);
  83. _YargsInstance_completionCommand.set(this, null);
  84. _YargsInstance_defaultShowHiddenOpt.set(this, 'show-hidden');
  85. _YargsInstance_exitError.set(this, null);
  86. _YargsInstance_detectLocale.set(this, true);
  87. _YargsInstance_emittedWarnings.set(this, {});
  88. _YargsInstance_exitProcess.set(this, true);
  89. _YargsInstance_frozens.set(this, []);
  90. _YargsInstance_globalMiddleware.set(this, void 0);
  91. _YargsInstance_groups.set(this, {});
  92. _YargsInstance_hasOutput.set(this, false);
  93. _YargsInstance_helpOpt.set(this, null);
  94. _YargsInstance_isGlobalContext.set(this, true);
  95. _YargsInstance_logger.set(this, void 0);
  96. _YargsInstance_output.set(this, '');
  97. _YargsInstance_options.set(this, void 0);
  98. _YargsInstance_parentRequire.set(this, void 0);
  99. _YargsInstance_parserConfig.set(this, {});
  100. _YargsInstance_parseFn.set(this, null);
  101. _YargsInstance_parseContext.set(this, null);
  102. _YargsInstance_pkgs.set(this, {});
  103. _YargsInstance_preservedGroups.set(this, {});
  104. _YargsInstance_processArgs.set(this, void 0);
  105. _YargsInstance_recommendCommands.set(this, false);
  106. _YargsInstance_shim.set(this, void 0);
  107. _YargsInstance_strict.set(this, false);
  108. _YargsInstance_strictCommands.set(this, false);
  109. _YargsInstance_strictOptions.set(this, false);
  110. _YargsInstance_usage.set(this, void 0);
  111. _YargsInstance_versionOpt.set(this, null);
  112. _YargsInstance_validation.set(this, void 0);
  113. __classPrivateFieldSet(this, _YargsInstance_shim, shim, "f");
  114. __classPrivateFieldSet(this, _YargsInstance_processArgs, processArgs, "f");
  115. __classPrivateFieldSet(this, _YargsInstance_cwd, cwd, "f");
  116. __classPrivateFieldSet(this, _YargsInstance_parentRequire, parentRequire, "f");
  117. __classPrivateFieldSet(this, _YargsInstance_globalMiddleware, new GlobalMiddleware(this), "f");
  118. this.$0 = this[kGetDollarZero]();
  119. this[kReset]();
  120. __classPrivateFieldSet(this, _YargsInstance_command, __classPrivateFieldGet(this, _YargsInstance_command, "f"), "f");
  121. __classPrivateFieldSet(this, _YargsInstance_usage, __classPrivateFieldGet(this, _YargsInstance_usage, "f"), "f");
  122. __classPrivateFieldSet(this, _YargsInstance_validation, __classPrivateFieldGet(this, _YargsInstance_validation, "f"), "f");
  123. __classPrivateFieldSet(this, _YargsInstance_options, __classPrivateFieldGet(this, _YargsInstance_options, "f"), "f");
  124. __classPrivateFieldGet(this, _YargsInstance_options, "f").showHiddenOpt = __classPrivateFieldGet(this, _YargsInstance_defaultShowHiddenOpt, "f");
  125. __classPrivateFieldSet(this, _YargsInstance_logger, this[kCreateLogger](), "f");
  126. }
  127. addHelpOpt(opt, msg) {
  128. const defaultHelpOpt = 'help';
  129. argsert('[string|boolean] [string]', [opt, msg], arguments.length);
  130. if (__classPrivateFieldGet(this, _YargsInstance_helpOpt, "f")) {
  131. this[kDeleteFromParserHintObject](__classPrivateFieldGet(this, _YargsInstance_helpOpt, "f"));
  132. __classPrivateFieldSet(this, _YargsInstance_helpOpt, null, "f");
  133. }
  134. if (opt === false && msg === undefined)
  135. return this;
  136. __classPrivateFieldSet(this, _YargsInstance_helpOpt, typeof opt === 'string' ? opt : defaultHelpOpt, "f");
  137. this.boolean(__classPrivateFieldGet(this, _YargsInstance_helpOpt, "f"));
  138. this.describe(__classPrivateFieldGet(this, _YargsInstance_helpOpt, "f"), msg || __classPrivateFieldGet(this, _YargsInstance_usage, "f").deferY18nLookup('Show help'));
  139. return this;
  140. }
  141. help(opt, msg) {
  142. return this.addHelpOpt(opt, msg);
  143. }
  144. addShowHiddenOpt(opt, msg) {
  145. argsert('[string|boolean] [string]', [opt, msg], arguments.length);
  146. if (opt === false && msg === undefined)
  147. return this;
  148. const showHiddenOpt = typeof opt === 'string' ? opt : __classPrivateFieldGet(this, _YargsInstance_defaultShowHiddenOpt, "f");
  149. this.boolean(showHiddenOpt);
  150. this.describe(showHiddenOpt, msg || __classPrivateFieldGet(this, _YargsInstance_usage, "f").deferY18nLookup('Show hidden options'));
  151. __classPrivateFieldGet(this, _YargsInstance_options, "f").showHiddenOpt = showHiddenOpt;
  152. return this;
  153. }
  154. showHidden(opt, msg) {
  155. return this.addShowHiddenOpt(opt, msg);
  156. }
  157. alias(key, value) {
  158. argsert('<object|string|array> [string|array]', [key, value], arguments.length);
  159. this[kPopulateParserHintArrayDictionary](this.alias.bind(this), 'alias', key, value);
  160. return this;
  161. }
  162. array(keys) {
  163. argsert('<array|string>', [keys], arguments.length);
  164. this[kPopulateParserHintArray]('array', keys);
  165. this[kTrackManuallySetKeys](keys);
  166. return this;
  167. }
  168. boolean(keys) {
  169. argsert('<array|string>', [keys], arguments.length);
  170. this[kPopulateParserHintArray]('boolean', keys);
  171. this[kTrackManuallySetKeys](keys);
  172. return this;
  173. }
  174. check(f, global) {
  175. argsert('<function> [boolean]', [f, global], arguments.length);
  176. this.middleware((argv, _yargs) => {
  177. return maybeAsyncResult(() => {
  178. return f(argv, _yargs.getOptions());
  179. }, (result) => {
  180. if (!result) {
  181. __classPrivateFieldGet(this, _YargsInstance_usage, "f").fail(__classPrivateFieldGet(this, _YargsInstance_shim, "f").y18n.__('Argument check failed: %s', f.toString()));
  182. }
  183. else if (typeof result === 'string' || result instanceof Error) {
  184. __classPrivateFieldGet(this, _YargsInstance_usage, "f").fail(result.toString(), result);
  185. }
  186. return argv;
  187. }, (err) => {
  188. __classPrivateFieldGet(this, _YargsInstance_usage, "f").fail(err.message ? err.message : err.toString(), err);
  189. return argv;
  190. });
  191. }, false, global);
  192. return this;
  193. }
  194. choices(key, value) {
  195. argsert('<object|string|array> [string|array]', [key, value], arguments.length);
  196. this[kPopulateParserHintArrayDictionary](this.choices.bind(this), 'choices', key, value);
  197. return this;
  198. }
  199. coerce(keys, value) {
  200. argsert('<object|string|array> [function]', [keys, value], arguments.length);
  201. if (Array.isArray(keys)) {
  202. if (!value) {
  203. throw new YError('coerce callback must be provided');
  204. }
  205. for (const key of keys) {
  206. this.coerce(key, value);
  207. }
  208. return this;
  209. }
  210. else if (typeof keys === 'object') {
  211. for (const key of Object.keys(keys)) {
  212. this.coerce(key, keys[key]);
  213. }
  214. return this;
  215. }
  216. if (!value) {
  217. throw new YError('coerce callback must be provided');
  218. }
  219. __classPrivateFieldGet(this, _YargsInstance_options, "f").key[keys] = true;
  220. __classPrivateFieldGet(this, _YargsInstance_globalMiddleware, "f").addCoerceMiddleware((argv, yargs) => {
  221. let aliases;
  222. const shouldCoerce = Object.prototype.hasOwnProperty.call(argv, keys);
  223. if (!shouldCoerce) {
  224. return argv;
  225. }
  226. return maybeAsyncResult(() => {
  227. aliases = yargs.getAliases();
  228. return value(argv[keys]);
  229. }, (result) => {
  230. argv[keys] = result;
  231. const stripAliased = yargs
  232. .getInternalMethods()
  233. .getParserConfiguration()['strip-aliased'];
  234. if (aliases[keys] && stripAliased !== true) {
  235. for (const alias of aliases[keys]) {
  236. argv[alias] = result;
  237. }
  238. }
  239. return argv;
  240. }, (err) => {
  241. throw new YError(err.message);
  242. });
  243. }, keys);
  244. return this;
  245. }
  246. conflicts(key1, key2) {
  247. argsert('<string|object> [string|array]', [key1, key2], arguments.length);
  248. __classPrivateFieldGet(this, _YargsInstance_validation, "f").conflicts(key1, key2);
  249. return this;
  250. }
  251. config(key = 'config', msg, parseFn) {
  252. argsert('[object|string] [string|function] [function]', [key, msg, parseFn], arguments.length);
  253. if (typeof key === 'object' && !Array.isArray(key)) {
  254. key = applyExtends(key, __classPrivateFieldGet(this, _YargsInstance_cwd, "f"), this[kGetParserConfiguration]()['deep-merge-config'] || false, __classPrivateFieldGet(this, _YargsInstance_shim, "f"));
  255. __classPrivateFieldGet(this, _YargsInstance_options, "f").configObjects = (__classPrivateFieldGet(this, _YargsInstance_options, "f").configObjects || []).concat(key);
  256. return this;
  257. }
  258. if (typeof msg === 'function') {
  259. parseFn = msg;
  260. msg = undefined;
  261. }
  262. this.describe(key, msg || __classPrivateFieldGet(this, _YargsInstance_usage, "f").deferY18nLookup('Path to JSON config file'));
  263. (Array.isArray(key) ? key : [key]).forEach(k => {
  264. __classPrivateFieldGet(this, _YargsInstance_options, "f").config[k] = parseFn || true;
  265. });
  266. return this;
  267. }
  268. completion(cmd, desc, fn) {
  269. argsert('[string] [string|boolean|function] [function]', [cmd, desc, fn], arguments.length);
  270. if (typeof desc === 'function') {
  271. fn = desc;
  272. desc = undefined;
  273. }
  274. __classPrivateFieldSet(this, _YargsInstance_completionCommand, cmd || __classPrivateFieldGet(this, _YargsInstance_completionCommand, "f") || 'completion', "f");
  275. if (!desc && desc !== false) {
  276. desc = 'generate completion script';
  277. }
  278. this.command(__classPrivateFieldGet(this, _YargsInstance_completionCommand, "f"), desc);
  279. if (fn)
  280. __classPrivateFieldGet(this, _YargsInstance_completion, "f").registerFunction(fn);
  281. return this;
  282. }
  283. command(cmd, description, builder, handler, middlewares, deprecated) {
  284. argsert('<string|array|object> [string|boolean] [function|object] [function] [array] [boolean|string]', [cmd, description, builder, handler, middlewares, deprecated], arguments.length);
  285. __classPrivateFieldGet(this, _YargsInstance_command, "f").addHandler(cmd, description, builder, handler, middlewares, deprecated);
  286. return this;
  287. }
  288. commands(cmd, description, builder, handler, middlewares, deprecated) {
  289. return this.command(cmd, description, builder, handler, middlewares, deprecated);
  290. }
  291. commandDir(dir, opts) {
  292. argsert('<string> [object]', [dir, opts], arguments.length);
  293. const req = __classPrivateFieldGet(this, _YargsInstance_parentRequire, "f") || __classPrivateFieldGet(this, _YargsInstance_shim, "f").require;
  294. __classPrivateFieldGet(this, _YargsInstance_command, "f").addDirectory(dir, req, __classPrivateFieldGet(this, _YargsInstance_shim, "f").getCallerFile(), opts);
  295. return this;
  296. }
  297. count(keys) {
  298. argsert('<array|string>', [keys], arguments.length);
  299. this[kPopulateParserHintArray]('count', keys);
  300. this[kTrackManuallySetKeys](keys);
  301. return this;
  302. }
  303. default(key, value, defaultDescription) {
  304. argsert('<object|string|array> [*] [string]', [key, value, defaultDescription], arguments.length);
  305. if (defaultDescription) {
  306. assertSingleKey(key, __classPrivateFieldGet(this, _YargsInstance_shim, "f"));
  307. __classPrivateFieldGet(this, _YargsInstance_options, "f").defaultDescription[key] = defaultDescription;
  308. }
  309. if (typeof value === 'function') {
  310. assertSingleKey(key, __classPrivateFieldGet(this, _YargsInstance_shim, "f"));
  311. if (!__classPrivateFieldGet(this, _YargsInstance_options, "f").defaultDescription[key])
  312. __classPrivateFieldGet(this, _YargsInstance_options, "f").defaultDescription[key] =
  313. __classPrivateFieldGet(this, _YargsInstance_usage, "f").functionDescription(value);
  314. value = value.call();
  315. }
  316. this[kPopulateParserHintSingleValueDictionary](this.default.bind(this), 'default', key, value);
  317. return this;
  318. }
  319. defaults(key, value, defaultDescription) {
  320. return this.default(key, value, defaultDescription);
  321. }
  322. demandCommand(min = 1, max, minMsg, maxMsg) {
  323. argsert('[number] [number|string] [string|null|undefined] [string|null|undefined]', [min, max, minMsg, maxMsg], arguments.length);
  324. if (typeof max !== 'number') {
  325. minMsg = max;
  326. max = Infinity;
  327. }
  328. this.global('_', false);
  329. __classPrivateFieldGet(this, _YargsInstance_options, "f").demandedCommands._ = {
  330. min,
  331. max,
  332. minMsg,
  333. maxMsg,
  334. };
  335. return this;
  336. }
  337. demand(keys, max, msg) {
  338. if (Array.isArray(max)) {
  339. max.forEach(key => {
  340. assertNotStrictEqual(msg, true, __classPrivateFieldGet(this, _YargsInstance_shim, "f"));
  341. this.demandOption(key, msg);
  342. });
  343. max = Infinity;
  344. }
  345. else if (typeof max !== 'number') {
  346. msg = max;
  347. max = Infinity;
  348. }
  349. if (typeof keys === 'number') {
  350. assertNotStrictEqual(msg, true, __classPrivateFieldGet(this, _YargsInstance_shim, "f"));
  351. this.demandCommand(keys, max, msg, msg);
  352. }
  353. else if (Array.isArray(keys)) {
  354. keys.forEach(key => {
  355. assertNotStrictEqual(msg, true, __classPrivateFieldGet(this, _YargsInstance_shim, "f"));
  356. this.demandOption(key, msg);
  357. });
  358. }
  359. else {
  360. if (typeof msg === 'string') {
  361. this.demandOption(keys, msg);
  362. }
  363. else if (msg === true || typeof msg === 'undefined') {
  364. this.demandOption(keys);
  365. }
  366. }
  367. return this;
  368. }
  369. demandOption(keys, msg) {
  370. argsert('<object|string|array> [string]', [keys, msg], arguments.length);
  371. this[kPopulateParserHintSingleValueDictionary](this.demandOption.bind(this), 'demandedOptions', keys, msg);
  372. return this;
  373. }
  374. deprecateOption(option, message) {
  375. argsert('<string> [string|boolean]', [option, message], arguments.length);
  376. __classPrivateFieldGet(this, _YargsInstance_options, "f").deprecatedOptions[option] = message;
  377. return this;
  378. }
  379. describe(keys, description) {
  380. argsert('<object|string|array> [string]', [keys, description], arguments.length);
  381. this[kSetKey](keys, true);
  382. __classPrivateFieldGet(this, _YargsInstance_usage, "f").describe(keys, description);
  383. return this;
  384. }
  385. detectLocale(detect) {
  386. argsert('<boolean>', [detect], arguments.length);
  387. __classPrivateFieldSet(this, _YargsInstance_detectLocale, detect, "f");
  388. return this;
  389. }
  390. env(prefix) {
  391. argsert('[string|boolean]', [prefix], arguments.length);
  392. if (prefix === false)
  393. delete __classPrivateFieldGet(this, _YargsInstance_options, "f").envPrefix;
  394. else
  395. __classPrivateFieldGet(this, _YargsInstance_options, "f").envPrefix = prefix || '';
  396. return this;
  397. }
  398. epilogue(msg) {
  399. argsert('<string>', [msg], arguments.length);
  400. __classPrivateFieldGet(this, _YargsInstance_usage, "f").epilog(msg);
  401. return this;
  402. }
  403. epilog(msg) {
  404. return this.epilogue(msg);
  405. }
  406. example(cmd, description) {
  407. argsert('<string|array> [string]', [cmd, description], arguments.length);
  408. if (Array.isArray(cmd)) {
  409. cmd.forEach(exampleParams => this.example(...exampleParams));
  410. }
  411. else {
  412. __classPrivateFieldGet(this, _YargsInstance_usage, "f").example(cmd, description);
  413. }
  414. return this;
  415. }
  416. exit(code, err) {
  417. __classPrivateFieldSet(this, _YargsInstance_hasOutput, true, "f");
  418. __classPrivateFieldSet(this, _YargsInstance_exitError, err, "f");
  419. if (__classPrivateFieldGet(this, _YargsInstance_exitProcess, "f"))
  420. __classPrivateFieldGet(this, _YargsInstance_shim, "f").process.exit(code);
  421. }
  422. exitProcess(enabled = true) {
  423. argsert('[boolean]', [enabled], arguments.length);
  424. __classPrivateFieldSet(this, _YargsInstance_exitProcess, enabled, "f");
  425. return this;
  426. }
  427. fail(f) {
  428. argsert('<function|boolean>', [f], arguments.length);
  429. if (typeof f === 'boolean' && f !== false) {
  430. throw new YError("Invalid first argument. Expected function or boolean 'false'");
  431. }
  432. __classPrivateFieldGet(this, _YargsInstance_usage, "f").failFn(f);
  433. return this;
  434. }
  435. getAliases() {
  436. return this.parsed ? this.parsed.aliases : {};
  437. }
  438. async getCompletion(args, done) {
  439. argsert('<array> [function]', [args, done], arguments.length);
  440. if (!done) {
  441. return new Promise((resolve, reject) => {
  442. __classPrivateFieldGet(this, _YargsInstance_completion, "f").getCompletion(args, (err, completions) => {
  443. if (err)
  444. reject(err);
  445. else
  446. resolve(completions);
  447. });
  448. });
  449. }
  450. else {
  451. return __classPrivateFieldGet(this, _YargsInstance_completion, "f").getCompletion(args, done);
  452. }
  453. }
  454. getDemandedOptions() {
  455. argsert([], 0);
  456. return __classPrivateFieldGet(this, _YargsInstance_options, "f").demandedOptions;
  457. }
  458. getDemandedCommands() {
  459. argsert([], 0);
  460. return __classPrivateFieldGet(this, _YargsInstance_options, "f").demandedCommands;
  461. }
  462. getDeprecatedOptions() {
  463. argsert([], 0);
  464. return __classPrivateFieldGet(this, _YargsInstance_options, "f").deprecatedOptions;
  465. }
  466. getDetectLocale() {
  467. return __classPrivateFieldGet(this, _YargsInstance_detectLocale, "f");
  468. }
  469. getExitProcess() {
  470. return __classPrivateFieldGet(this, _YargsInstance_exitProcess, "f");
  471. }
  472. getGroups() {
  473. return Object.assign({}, __classPrivateFieldGet(this, _YargsInstance_groups, "f"), __classPrivateFieldGet(this, _YargsInstance_preservedGroups, "f"));
  474. }
  475. getHelp() {
  476. __classPrivateFieldSet(this, _YargsInstance_hasOutput, true, "f");
  477. if (!__classPrivateFieldGet(this, _YargsInstance_usage, "f").hasCachedHelpMessage()) {
  478. if (!this.parsed) {
  479. const parse = this[kRunYargsParserAndExecuteCommands](__classPrivateFieldGet(this, _YargsInstance_processArgs, "f"), undefined, undefined, 0, true);
  480. if (isPromise(parse)) {
  481. return parse.then(() => {
  482. return __classPrivateFieldGet(this, _YargsInstance_usage, "f").help();
  483. });
  484. }
  485. }
  486. const builderResponse = __classPrivateFieldGet(this, _YargsInstance_command, "f").runDefaultBuilderOn(this);
  487. if (isPromise(builderResponse)) {
  488. return builderResponse.then(() => {
  489. return __classPrivateFieldGet(this, _YargsInstance_usage, "f").help();
  490. });
  491. }
  492. }
  493. return Promise.resolve(__classPrivateFieldGet(this, _YargsInstance_usage, "f").help());
  494. }
  495. getOptions() {
  496. return __classPrivateFieldGet(this, _YargsInstance_options, "f");
  497. }
  498. getStrict() {
  499. return __classPrivateFieldGet(this, _YargsInstance_strict, "f");
  500. }
  501. getStrictCommands() {
  502. return __classPrivateFieldGet(this, _YargsInstance_strictCommands, "f");
  503. }
  504. getStrictOptions() {
  505. return __classPrivateFieldGet(this, _YargsInstance_strictOptions, "f");
  506. }
  507. global(globals, global) {
  508. argsert('<string|array> [boolean]', [globals, global], arguments.length);
  509. globals = [].concat(globals);
  510. if (global !== false) {
  511. __classPrivateFieldGet(this, _YargsInstance_options, "f").local = __classPrivateFieldGet(this, _YargsInstance_options, "f").local.filter(l => globals.indexOf(l) === -1);
  512. }
  513. else {
  514. globals.forEach(g => {
  515. if (!__classPrivateFieldGet(this, _YargsInstance_options, "f").local.includes(g))
  516. __classPrivateFieldGet(this, _YargsInstance_options, "f").local.push(g);
  517. });
  518. }
  519. return this;
  520. }
  521. group(opts, groupName) {
  522. argsert('<string|array> <string>', [opts, groupName], arguments.length);
  523. const existing = __classPrivateFieldGet(this, _YargsInstance_preservedGroups, "f")[groupName] || __classPrivateFieldGet(this, _YargsInstance_groups, "f")[groupName];
  524. if (__classPrivateFieldGet(this, _YargsInstance_preservedGroups, "f")[groupName]) {
  525. delete __classPrivateFieldGet(this, _YargsInstance_preservedGroups, "f")[groupName];
  526. }
  527. const seen = {};
  528. __classPrivateFieldGet(this, _YargsInstance_groups, "f")[groupName] = (existing || []).concat(opts).filter(key => {
  529. if (seen[key])
  530. return false;
  531. return (seen[key] = true);
  532. });
  533. return this;
  534. }
  535. hide(key) {
  536. argsert('<string>', [key], arguments.length);
  537. __classPrivateFieldGet(this, _YargsInstance_options, "f").hiddenOptions.push(key);
  538. return this;
  539. }
  540. implies(key, value) {
  541. argsert('<string|object> [number|string|array]', [key, value], arguments.length);
  542. __classPrivateFieldGet(this, _YargsInstance_validation, "f").implies(key, value);
  543. return this;
  544. }
  545. locale(locale) {
  546. argsert('[string]', [locale], arguments.length);
  547. if (locale === undefined) {
  548. this[kGuessLocale]();
  549. return __classPrivateFieldGet(this, _YargsInstance_shim, "f").y18n.getLocale();
  550. }
  551. __classPrivateFieldSet(this, _YargsInstance_detectLocale, false, "f");
  552. __classPrivateFieldGet(this, _YargsInstance_shim, "f").y18n.setLocale(locale);
  553. return this;
  554. }
  555. middleware(callback, applyBeforeValidation, global) {
  556. return __classPrivateFieldGet(this, _YargsInstance_globalMiddleware, "f").addMiddleware(callback, !!applyBeforeValidation, global);
  557. }
  558. nargs(key, value) {
  559. argsert('<string|object|array> [number]', [key, value], arguments.length);
  560. this[kPopulateParserHintSingleValueDictionary](this.nargs.bind(this), 'narg', key, value);
  561. return this;
  562. }
  563. normalize(keys) {
  564. argsert('<array|string>', [keys], arguments.length);
  565. this[kPopulateParserHintArray]('normalize', keys);
  566. return this;
  567. }
  568. number(keys) {
  569. argsert('<array|string>', [keys], arguments.length);
  570. this[kPopulateParserHintArray]('number', keys);
  571. this[kTrackManuallySetKeys](keys);
  572. return this;
  573. }
  574. option(key, opt) {
  575. argsert('<string|object> [object]', [key, opt], arguments.length);
  576. if (typeof key === 'object') {
  577. Object.keys(key).forEach(k => {
  578. this.options(k, key[k]);
  579. });
  580. }
  581. else {
  582. if (typeof opt !== 'object') {
  583. opt = {};
  584. }
  585. this[kTrackManuallySetKeys](key);
  586. if (__classPrivateFieldGet(this, _YargsInstance_versionOpt, "f") && (key === 'version' || (opt === null || opt === void 0 ? void 0 : opt.alias) === 'version')) {
  587. this[kEmitWarning]([
  588. '"version" is a reserved word.',
  589. 'Please do one of the following:',
  590. '- Disable version with `yargs.version(false)` if using "version" as an option',
  591. '- Use the built-in `yargs.version` method instead (if applicable)',
  592. '- Use a different option key',
  593. 'https://yargs.js.org/docs/#api-reference-version',
  594. ].join('\n'), undefined, 'versionWarning');
  595. }
  596. __classPrivateFieldGet(this, _YargsInstance_options, "f").key[key] = true;
  597. if (opt.alias)
  598. this.alias(key, opt.alias);
  599. const deprecate = opt.deprecate || opt.deprecated;
  600. if (deprecate) {
  601. this.deprecateOption(key, deprecate);
  602. }
  603. const demand = opt.demand || opt.required || opt.require;
  604. if (demand) {
  605. this.demand(key, demand);
  606. }
  607. if (opt.demandOption) {
  608. this.demandOption(key, typeof opt.demandOption === 'string' ? opt.demandOption : undefined);
  609. }
  610. if (opt.conflicts) {
  611. this.conflicts(key, opt.conflicts);
  612. }
  613. if ('default' in opt) {
  614. this.default(key, opt.default);
  615. }
  616. if (opt.implies !== undefined) {
  617. this.implies(key, opt.implies);
  618. }
  619. if (opt.nargs !== undefined) {
  620. this.nargs(key, opt.nargs);
  621. }
  622. if (opt.config) {
  623. this.config(key, opt.configParser);
  624. }
  625. if (opt.normalize) {
  626. this.normalize(key);
  627. }
  628. if (opt.choices) {
  629. this.choices(key, opt.choices);
  630. }
  631. if (opt.coerce) {
  632. this.coerce(key, opt.coerce);
  633. }
  634. if (opt.group) {
  635. this.group(key, opt.group);
  636. }
  637. if (opt.boolean || opt.type === 'boolean') {
  638. this.boolean(key);
  639. if (opt.alias)
  640. this.boolean(opt.alias);
  641. }
  642. if (opt.array || opt.type === 'array') {
  643. this.array(key);
  644. if (opt.alias)
  645. this.array(opt.alias);
  646. }
  647. if (opt.number || opt.type === 'number') {
  648. this.number(key);
  649. if (opt.alias)
  650. this.number(opt.alias);
  651. }
  652. if (opt.string || opt.type === 'string') {
  653. this.string(key);
  654. if (opt.alias)
  655. this.string(opt.alias);
  656. }
  657. if (opt.count || opt.type === 'count') {
  658. this.count(key);
  659. }
  660. if (typeof opt.global === 'boolean') {
  661. this.global(key, opt.global);
  662. }
  663. if (opt.defaultDescription) {
  664. __classPrivateFieldGet(this, _YargsInstance_options, "f").defaultDescription[key] = opt.defaultDescription;
  665. }
  666. if (opt.skipValidation) {
  667. this.skipValidation(key);
  668. }
  669. const desc = opt.describe || opt.description || opt.desc;
  670. const descriptions = __classPrivateFieldGet(this, _YargsInstance_usage, "f").getDescriptions();
  671. if (!Object.prototype.hasOwnProperty.call(descriptions, key) ||
  672. typeof desc === 'string') {
  673. this.describe(key, desc);
  674. }
  675. if (opt.hidden) {
  676. this.hide(key);
  677. }
  678. if (opt.requiresArg) {
  679. this.requiresArg(key);
  680. }
  681. }
  682. return this;
  683. }
  684. options(key, opt) {
  685. return this.option(key, opt);
  686. }
  687. parse(args, shortCircuit, _parseFn) {
  688. argsert('[string|array] [function|boolean|object] [function]', [args, shortCircuit, _parseFn], arguments.length);
  689. this[kFreeze]();
  690. if (typeof args === 'undefined') {
  691. args = __classPrivateFieldGet(this, _YargsInstance_processArgs, "f");
  692. }
  693. if (typeof shortCircuit === 'object') {
  694. __classPrivateFieldSet(this, _YargsInstance_parseContext, shortCircuit, "f");
  695. shortCircuit = _parseFn;
  696. }
  697. if (typeof shortCircuit === 'function') {
  698. __classPrivateFieldSet(this, _YargsInstance_parseFn, shortCircuit, "f");
  699. shortCircuit = false;
  700. }
  701. if (!shortCircuit)
  702. __classPrivateFieldSet(this, _YargsInstance_processArgs, args, "f");
  703. if (__classPrivateFieldGet(this, _YargsInstance_parseFn, "f"))
  704. __classPrivateFieldSet(this, _YargsInstance_exitProcess, false, "f");
  705. const parsed = this[kRunYargsParserAndExecuteCommands](args, !!shortCircuit);
  706. const tmpParsed = this.parsed;
  707. __classPrivateFieldGet(this, _YargsInstance_completion, "f").setParsed(this.parsed);
  708. if (isPromise(parsed)) {
  709. return parsed
  710. .then(argv => {
  711. if (__classPrivateFieldGet(this, _YargsInstance_parseFn, "f"))
  712. __classPrivateFieldGet(this, _YargsInstance_parseFn, "f").call(this, __classPrivateFieldGet(this, _YargsInstance_exitError, "f"), argv, __classPrivateFieldGet(this, _YargsInstance_output, "f"));
  713. return argv;
  714. })
  715. .catch(err => {
  716. if (__classPrivateFieldGet(this, _YargsInstance_parseFn, "f")) {
  717. __classPrivateFieldGet(this, _YargsInstance_parseFn, "f")(err, this.parsed.argv, __classPrivateFieldGet(this, _YargsInstance_output, "f"));
  718. }
  719. throw err;
  720. })
  721. .finally(() => {
  722. this[kUnfreeze]();
  723. this.parsed = tmpParsed;
  724. });
  725. }
  726. else {
  727. if (__classPrivateFieldGet(this, _YargsInstance_parseFn, "f"))
  728. __classPrivateFieldGet(this, _YargsInstance_parseFn, "f").call(this, __classPrivateFieldGet(this, _YargsInstance_exitError, "f"), parsed, __classPrivateFieldGet(this, _YargsInstance_output, "f"));
  729. this[kUnfreeze]();
  730. this.parsed = tmpParsed;
  731. }
  732. return parsed;
  733. }
  734. parseAsync(args, shortCircuit, _parseFn) {
  735. const maybePromise = this.parse(args, shortCircuit, _parseFn);
  736. return !isPromise(maybePromise)
  737. ? Promise.resolve(maybePromise)
  738. : maybePromise;
  739. }
  740. parseSync(args, shortCircuit, _parseFn) {
  741. const maybePromise = this.parse(args, shortCircuit, _parseFn);
  742. if (isPromise(maybePromise)) {
  743. throw new YError('.parseSync() must not be used with asynchronous builders, handlers, or middleware');
  744. }
  745. return maybePromise;
  746. }
  747. parserConfiguration(config) {
  748. argsert('<object>', [config], arguments.length);
  749. __classPrivateFieldSet(this, _YargsInstance_parserConfig, config, "f");
  750. return this;
  751. }
  752. pkgConf(key, rootPath) {
  753. argsert('<string> [string]', [key, rootPath], arguments.length);
  754. let conf = null;
  755. const obj = this[kPkgUp](rootPath || __classPrivateFieldGet(this, _YargsInstance_cwd, "f"));
  756. if (obj[key] && typeof obj[key] === 'object') {
  757. conf = applyExtends(obj[key], rootPath || __classPrivateFieldGet(this, _YargsInstance_cwd, "f"), this[kGetParserConfiguration]()['deep-merge-config'] || false, __classPrivateFieldGet(this, _YargsInstance_shim, "f"));
  758. __classPrivateFieldGet(this, _YargsInstance_options, "f").configObjects = (__classPrivateFieldGet(this, _YargsInstance_options, "f").configObjects || []).concat(conf);
  759. }
  760. return this;
  761. }
  762. positional(key, opts) {
  763. argsert('<string> <object>', [key, opts], arguments.length);
  764. const supportedOpts = [
  765. 'default',
  766. 'defaultDescription',
  767. 'implies',
  768. 'normalize',
  769. 'choices',
  770. 'conflicts',
  771. 'coerce',
  772. 'type',
  773. 'describe',
  774. 'desc',
  775. 'description',
  776. 'alias',
  777. ];
  778. opts = objFilter(opts, (k, v) => {
  779. if (k === 'type' && !['string', 'number', 'boolean'].includes(v))
  780. return false;
  781. return supportedOpts.includes(k);
  782. });
  783. const fullCommand = __classPrivateFieldGet(this, _YargsInstance_context, "f").fullCommands[__classPrivateFieldGet(this, _YargsInstance_context, "f").fullCommands.length - 1];
  784. const parseOptions = fullCommand
  785. ? __classPrivateFieldGet(this, _YargsInstance_command, "f").cmdToParseOptions(fullCommand)
  786. : {
  787. array: [],
  788. alias: {},
  789. default: {},
  790. demand: {},
  791. };
  792. objectKeys(parseOptions).forEach(pk => {
  793. const parseOption = parseOptions[pk];
  794. if (Array.isArray(parseOption)) {
  795. if (parseOption.indexOf(key) !== -1)
  796. opts[pk] = true;
  797. }
  798. else {
  799. if (parseOption[key] && !(pk in opts))
  800. opts[pk] = parseOption[key];
  801. }
  802. });
  803. this.group(key, __classPrivateFieldGet(this, _YargsInstance_usage, "f").getPositionalGroupName());
  804. return this.option(key, opts);
  805. }
  806. recommendCommands(recommend = true) {
  807. argsert('[boolean]', [recommend], arguments.length);
  808. __classPrivateFieldSet(this, _YargsInstance_recommendCommands, recommend, "f");
  809. return this;
  810. }
  811. required(keys, max, msg) {
  812. return this.demand(keys, max, msg);
  813. }
  814. require(keys, max, msg) {
  815. return this.demand(keys, max, msg);
  816. }
  817. requiresArg(keys) {
  818. argsert('<array|string|object> [number]', [keys], arguments.length);
  819. if (typeof keys === 'string' && __classPrivateFieldGet(this, _YargsInstance_options, "f").narg[keys]) {
  820. return this;
  821. }
  822. else {
  823. this[kPopulateParserHintSingleValueDictionary](this.requiresArg.bind(this), 'narg', keys, NaN);
  824. }
  825. return this;
  826. }
  827. showCompletionScript($0, cmd) {
  828. argsert('[string] [string]', [$0, cmd], arguments.length);
  829. $0 = $0 || this.$0;
  830. __classPrivateFieldGet(this, _YargsInstance_logger, "f").log(__classPrivateFieldGet(this, _YargsInstance_completion, "f").generateCompletionScript($0, cmd || __classPrivateFieldGet(this, _YargsInstance_completionCommand, "f") || 'completion'));
  831. return this;
  832. }
  833. showHelp(level) {
  834. argsert('[string|function]', [level], arguments.length);
  835. __classPrivateFieldSet(this, _YargsInstance_hasOutput, true, "f");
  836. if (!__classPrivateFieldGet(this, _YargsInstance_usage, "f").hasCachedHelpMessage()) {
  837. if (!this.parsed) {
  838. const parse = this[kRunYargsParserAndExecuteCommands](__classPrivateFieldGet(this, _YargsInstance_processArgs, "f"), undefined, undefined, 0, true);
  839. if (isPromise(parse)) {
  840. parse.then(() => {
  841. __classPrivateFieldGet(this, _YargsInstance_usage, "f").showHelp(level);
  842. });
  843. return this;
  844. }
  845. }
  846. const builderResponse = __classPrivateFieldGet(this, _YargsInstance_command, "f").runDefaultBuilderOn(this);
  847. if (isPromise(builderResponse)) {
  848. builderResponse.then(() => {
  849. __classPrivateFieldGet(this, _YargsInstance_usage, "f").showHelp(level);
  850. });
  851. return this;
  852. }
  853. }
  854. __classPrivateFieldGet(this, _YargsInstance_usage, "f").showHelp(level);
  855. return this;
  856. }
  857. scriptName(scriptName) {
  858. this.customScriptName = true;
  859. this.$0 = scriptName;
  860. return this;
  861. }
  862. showHelpOnFail(enabled, message) {
  863. argsert('[boolean|string] [string]', [enabled, message], arguments.length);
  864. __classPrivateFieldGet(this, _YargsInstance_usage, "f").showHelpOnFail(enabled, message);
  865. return this;
  866. }
  867. showVersion(level) {
  868. argsert('[string|function]', [level], arguments.length);
  869. __classPrivateFieldGet(this, _YargsInstance_usage, "f").showVersion(level);
  870. return this;
  871. }
  872. skipValidation(keys) {
  873. argsert('<array|string>', [keys], arguments.length);
  874. this[kPopulateParserHintArray]('skipValidation', keys);
  875. return this;
  876. }
  877. strict(enabled) {
  878. argsert('[boolean]', [enabled], arguments.length);
  879. __classPrivateFieldSet(this, _YargsInstance_strict, enabled !== false, "f");
  880. return this;
  881. }
  882. strictCommands(enabled) {
  883. argsert('[boolean]', [enabled], arguments.length);
  884. __classPrivateFieldSet(this, _YargsInstance_strictCommands, enabled !== false, "f");
  885. return this;
  886. }
  887. strictOptions(enabled) {
  888. argsert('[boolean]', [enabled], arguments.length);
  889. __classPrivateFieldSet(this, _YargsInstance_strictOptions, enabled !== false, "f");
  890. return this;
  891. }
  892. string(keys) {
  893. argsert('<array|string>', [keys], arguments.length);
  894. this[kPopulateParserHintArray]('string', keys);
  895. this[kTrackManuallySetKeys](keys);
  896. return this;
  897. }
  898. terminalWidth() {
  899. argsert([], 0);
  900. return __classPrivateFieldGet(this, _YargsInstance_shim, "f").process.stdColumns;
  901. }
  902. updateLocale(obj) {
  903. return this.updateStrings(obj);
  904. }
  905. updateStrings(obj) {
  906. argsert('<object>', [obj], arguments.length);
  907. __classPrivateFieldSet(this, _YargsInstance_detectLocale, false, "f");
  908. __classPrivateFieldGet(this, _YargsInstance_shim, "f").y18n.updateLocale(obj);
  909. return this;
  910. }
  911. usage(msg, description, builder, handler) {
  912. argsert('<string|null|undefined> [string|boolean] [function|object] [function]', [msg, description, builder, handler], arguments.length);
  913. if (description !== undefined) {
  914. assertNotStrictEqual(msg, null, __classPrivateFieldGet(this, _YargsInstance_shim, "f"));
  915. if ((msg || '').match(/^\$0( |$)/)) {
  916. return this.command(msg, description, builder, handler);
  917. }
  918. else {
  919. throw new YError('.usage() description must start with $0 if being used as alias for .command()');
  920. }
  921. }
  922. else {
  923. __classPrivateFieldGet(this, _YargsInstance_usage, "f").usage(msg);
  924. return this;
  925. }
  926. }
  927. version(opt, msg, ver) {
  928. const defaultVersionOpt = 'version';
  929. argsert('[boolean|string] [string] [string]', [opt, msg, ver], arguments.length);
  930. if (__classPrivateFieldGet(this, _YargsInstance_versionOpt, "f")) {
  931. this[kDeleteFromParserHintObject](__classPrivateFieldGet(this, _YargsInstance_versionOpt, "f"));
  932. __classPrivateFieldGet(this, _YargsInstance_usage, "f").version(undefined);
  933. __classPrivateFieldSet(this, _YargsInstance_versionOpt, null, "f");
  934. }
  935. if (arguments.length === 0) {
  936. ver = this[kGuessVersion]();
  937. opt = defaultVersionOpt;
  938. }
  939. else if (arguments.length === 1) {
  940. if (opt === false) {
  941. return this;
  942. }
  943. ver = opt;
  944. opt = defaultVersionOpt;
  945. }
  946. else if (arguments.length === 2) {
  947. ver = msg;
  948. msg = undefined;
  949. }
  950. __classPrivateFieldSet(this, _YargsInstance_versionOpt, typeof opt === 'string' ? opt : defaultVersionOpt, "f");
  951. msg = msg || __classPrivateFieldGet(this, _YargsInstance_usage, "f").deferY18nLookup('Show version number');
  952. __classPrivateFieldGet(this, _YargsInstance_usage, "f").version(ver || undefined);
  953. this.boolean(__classPrivateFieldGet(this, _YargsInstance_versionOpt, "f"));
  954. this.describe(__classPrivateFieldGet(this, _YargsInstance_versionOpt, "f"), msg);
  955. return this;
  956. }
  957. wrap(cols) {
  958. argsert('<number|null|undefined>', [cols], arguments.length);
  959. __classPrivateFieldGet(this, _YargsInstance_usage, "f").wrap(cols);
  960. return this;
  961. }
  962. [(_YargsInstance_command = new WeakMap(), _YargsInstance_cwd = new WeakMap(), _YargsInstance_context = new WeakMap(), _YargsInstance_completion = new WeakMap(), _YargsInstance_completionCommand = new WeakMap(), _YargsInstance_defaultShowHiddenOpt = new WeakMap(), _YargsInstance_exitError = new WeakMap(), _YargsInstance_detectLocale = new WeakMap(), _YargsInstance_emittedWarnings = new WeakMap(), _YargsInstance_exitProcess = new WeakMap(), _YargsInstance_frozens = new WeakMap(), _YargsInstance_globalMiddleware = new WeakMap(), _YargsInstance_groups = new WeakMap(), _YargsInstance_hasOutput = new WeakMap(), _YargsInstance_helpOpt = new WeakMap(), _YargsInstance_isGlobalContext = new WeakMap(), _YargsInstance_logger = new WeakMap(), _YargsInstance_output = new WeakMap(), _YargsInstance_options = new WeakMap(), _YargsInstance_parentRequire = new WeakMap(), _YargsInstance_parserConfig = new WeakMap(), _YargsInstance_parseFn = new WeakMap(), _YargsInstance_parseContext = new WeakMap(), _YargsInstance_pkgs = new WeakMap(), _YargsInstance_preservedGroups = new WeakMap(), _YargsInstance_processArgs = new WeakMap(), _YargsInstance_recommendCommands = new WeakMap(), _YargsInstance_shim = new WeakMap(), _YargsInstance_strict = new WeakMap(), _YargsInstance_strictCommands = new WeakMap(), _YargsInstance_strictOptions = new WeakMap(), _YargsInstance_usage = new WeakMap(), _YargsInstance_versionOpt = new WeakMap(), _YargsInstance_validation = new WeakMap(), kCopyDoubleDash)](argv) {
  963. if (!argv._ || !argv['--'])
  964. return argv;
  965. argv._.push.apply(argv._, argv['--']);
  966. try {
  967. delete argv['--'];
  968. }
  969. catch (_err) { }
  970. return argv;
  971. }
  972. [kCreateLogger]() {
  973. return {
  974. log: (...args) => {
  975. if (!this[kHasParseCallback]())
  976. console.log(...args);
  977. __classPrivateFieldSet(this, _YargsInstance_hasOutput, true, "f");
  978. if (__classPrivateFieldGet(this, _YargsInstance_output, "f").length)
  979. __classPrivateFieldSet(this, _YargsInstance_output, __classPrivateFieldGet(this, _YargsInstance_output, "f") + '\n', "f");
  980. __classPrivateFieldSet(this, _YargsInstance_output, __classPrivateFieldGet(this, _YargsInstance_output, "f") + args.join(' '), "f");
  981. },
  982. error: (...args) => {
  983. if (!this[kHasParseCallback]())
  984. console.error(...args);
  985. __classPrivateFieldSet(this, _YargsInstance_hasOutput, true, "f");
  986. if (__classPrivateFieldGet(this, _YargsInstance_output, "f").length)
  987. __classPrivateFieldSet(this, _YargsInstance_output, __classPrivateFieldGet(this, _YargsInstance_output, "f") + '\n', "f");
  988. __classPrivateFieldSet(this, _YargsInstance_output, __classPrivateFieldGet(this, _YargsInstance_output, "f") + args.join(' '), "f");
  989. },
  990. };
  991. }
  992. [kDeleteFromParserHintObject](optionKey) {
  993. objectKeys(__classPrivateFieldGet(this, _YargsInstance_options, "f")).forEach((hintKey) => {
  994. if (((key) => key === 'configObjects')(hintKey))
  995. return;
  996. const hint = __classPrivateFieldGet(this, _YargsInstance_options, "f")[hintKey];
  997. if (Array.isArray(hint)) {
  998. if (hint.includes(optionKey))
  999. hint.splice(hint.indexOf(optionKey), 1);
  1000. }
  1001. else if (typeof hint === 'object') {
  1002. delete hint[optionKey];
  1003. }
  1004. });
  1005. delete __classPrivateFieldGet(this, _YargsInstance_usage, "f").getDescriptions()[optionKey];
  1006. }
  1007. [kEmitWarning](warning, type, deduplicationId) {
  1008. if (!__classPrivateFieldGet(this, _YargsInstance_emittedWarnings, "f")[deduplicationId]) {
  1009. __classPrivateFieldGet(this, _YargsInstance_shim, "f").process.emitWarning(warning, type);
  1010. __classPrivateFieldGet(this, _YargsInstance_emittedWarnings, "f")[deduplicationId] = true;
  1011. }
  1012. }
  1013. [kFreeze]() {
  1014. __classPrivateFieldGet(this, _YargsInstance_frozens, "f").push({
  1015. options: __classPrivateFieldGet(this, _YargsInstance_options, "f"),
  1016. configObjects: __classPrivateFieldGet(this, _YargsInstance_options, "f").configObjects.slice(0),
  1017. exitProcess: __classPrivateFieldGet(this, _YargsInstance_exitProcess, "f"),
  1018. groups: __classPrivateFieldGet(this, _YargsInstance_groups, "f"),
  1019. strict: __classPrivateFieldGet(this, _YargsInstance_strict, "f"),
  1020. strictCommands: __classPrivateFieldGet(this, _YargsInstance_strictCommands, "f"),
  1021. strictOptions: __classPrivateFieldGet(this, _YargsInstance_strictOptions, "f"),
  1022. completionCommand: __classPrivateFieldGet(this, _YargsInstance_completionCommand, "f"),
  1023. output: __classPrivateFieldGet(this, _YargsInstance_output, "f"),
  1024. exitError: __classPrivateFieldGet(this, _YargsInstance_exitError, "f"),
  1025. hasOutput: __classPrivateFieldGet(this, _YargsInstance_hasOutput, "f"),
  1026. parsed: this.parsed,
  1027. parseFn: __classPrivateFieldGet(this, _YargsInstance_parseFn, "f"),
  1028. parseContext: __classPrivateFieldGet(this, _YargsInstance_parseContext, "f"),
  1029. });
  1030. __classPrivateFieldGet(this, _YargsInstance_usage, "f").freeze();
  1031. __classPrivateFieldGet(this, _YargsInstance_validation, "f").freeze();
  1032. __classPrivateFieldGet(this, _YargsInstance_command, "f").freeze();
  1033. __classPrivateFieldGet(this, _YargsInstance_globalMiddleware, "f").freeze();
  1034. }
  1035. [kGetDollarZero]() {
  1036. let $0 = '';
  1037. let default$0;
  1038. if (/\b(node|iojs|electron)(\.exe)?$/.test(__classPrivateFieldGet(this, _YargsInstance_shim, "f").process.argv()[0])) {
  1039. default$0 = __classPrivateFieldGet(this, _YargsInstance_shim, "f").process.argv().slice(1, 2);
  1040. }
  1041. else {
  1042. default$0 = __classPrivateFieldGet(this, _YargsInstance_shim, "f").process.argv().slice(0, 1);
  1043. }
  1044. $0 = default$0
  1045. .map(x => {
  1046. const b = this[kRebase](__classPrivateFieldGet(this, _YargsInstance_cwd, "f"), x);
  1047. return x.match(/^(\/|([a-zA-Z]:)?\\)/) && b.length < x.length ? b : x;
  1048. })
  1049. .join(' ')
  1050. .trim();
  1051. if (__classPrivateFieldGet(this, _YargsInstance_shim, "f").getEnv('_') &&
  1052. __classPrivateFieldGet(this, _YargsInstance_shim, "f").getProcessArgvBin() === __classPrivateFieldGet(this, _YargsInstance_shim, "f").getEnv('_')) {
  1053. $0 = __classPrivateFieldGet(this, _YargsInstance_shim, "f")
  1054. .getEnv('_')
  1055. .replace(`${__classPrivateFieldGet(this, _YargsInstance_shim, "f").path.dirname(__classPrivateFieldGet(this, _YargsInstance_shim, "f").process.execPath())}/`, '');
  1056. }
  1057. return $0;
  1058. }
  1059. [kGetParserConfiguration]() {
  1060. return __classPrivateFieldGet(this, _YargsInstance_parserConfig, "f");
  1061. }
  1062. [kGuessLocale]() {
  1063. if (!__classPrivateFieldGet(this, _YargsInstance_detectLocale, "f"))
  1064. return;
  1065. const locale = __classPrivateFieldGet(this, _YargsInstance_shim, "f").getEnv('LC_ALL') ||
  1066. __classPrivateFieldGet(this, _YargsInstance_shim, "f").getEnv('LC_MESSAGES') ||
  1067. __classPrivateFieldGet(this, _YargsInstance_shim, "f").getEnv('LANG') ||
  1068. __classPrivateFieldGet(this, _YargsInstance_shim, "f").getEnv('LANGUAGE') ||
  1069. 'en_US';
  1070. this.locale(locale.replace(/[.:].*/, ''));
  1071. }
  1072. [kGuessVersion]() {
  1073. const obj = this[kPkgUp]();
  1074. return obj.version || 'unknown';
  1075. }
  1076. [kParsePositionalNumbers](argv) {
  1077. const args = argv['--'] ? argv['--'] : argv._;
  1078. for (let i = 0, arg; (arg = args[i]) !== undefined; i++) {
  1079. if (__classPrivateFieldGet(this, _YargsInstance_shim, "f").Parser.looksLikeNumber(arg) &&
  1080. Number.isSafeInteger(Math.floor(parseFloat(`${arg}`)))) {
  1081. args[i] = Number(arg);
  1082. }
  1083. }
  1084. return argv;
  1085. }
  1086. [kPkgUp](rootPath) {
  1087. const npath = rootPath || '*';
  1088. if (__classPrivateFieldGet(this, _YargsInstance_pkgs, "f")[npath])
  1089. return __classPrivateFieldGet(this, _YargsInstance_pkgs, "f")[npath];
  1090. let obj = {};
  1091. try {
  1092. let startDir = rootPath || __classPrivateFieldGet(this, _YargsInstance_shim, "f").mainFilename;
  1093. if (!rootPath && __classPrivateFieldGet(this, _YargsInstance_shim, "f").path.extname(startDir)) {
  1094. startDir = __classPrivateFieldGet(this, _YargsInstance_shim, "f").path.dirname(startDir);
  1095. }
  1096. const pkgJsonPath = __classPrivateFieldGet(this, _YargsInstance_shim, "f").findUp(startDir, (dir, names) => {
  1097. if (names.includes('package.json')) {
  1098. return 'package.json';
  1099. }
  1100. else {
  1101. return undefined;
  1102. }
  1103. });
  1104. assertNotStrictEqual(pkgJsonPath, undefined, __classPrivateFieldGet(this, _YargsInstance_shim, "f"));
  1105. obj = JSON.parse(__classPrivateFieldGet(this, _YargsInstance_shim, "f").readFileSync(pkgJsonPath, 'utf8'));
  1106. }
  1107. catch (_noop) { }
  1108. __classPrivateFieldGet(this, _YargsInstance_pkgs, "f")[npath] = obj || {};
  1109. return __classPrivateFieldGet(this, _YargsInstance_pkgs, "f")[npath];
  1110. }
  1111. [kPopulateParserHintArray](type, keys) {
  1112. keys = [].concat(keys);
  1113. keys.forEach(key => {
  1114. key = this[kSanitizeKey](key);
  1115. __classPrivateFieldGet(this, _YargsInstance_options, "f")[type].push(key);
  1116. });
  1117. }
  1118. [kPopulateParserHintSingleValueDictionary](builder, type, key, value) {
  1119. this[kPopulateParserHintDictionary](builder, type, key, value, (type, key, value) => {
  1120. __classPrivateFieldGet(this, _YargsInstance_options, "f")[type][key] = value;
  1121. });
  1122. }
  1123. [kPopulateParserHintArrayDictionary](builder, type, key, value) {
  1124. this[kPopulateParserHintDictionary](builder, type, key, value, (type, key, value) => {
  1125. __classPrivateFieldGet(this, _YargsInstance_options, "f")[type][key] = (__classPrivateFieldGet(this, _YargsInstance_options, "f")[type][key] || []).concat(value);
  1126. });
  1127. }
  1128. [kPopulateParserHintDictionary](builder, type, key, value, singleKeyHandler) {
  1129. if (Array.isArray(key)) {
  1130. key.forEach(k => {
  1131. builder(k, value);
  1132. });
  1133. }
  1134. else if (((key) => typeof key === 'object')(key)) {
  1135. for (const k of objectKeys(key)) {
  1136. builder(k, key[k]);
  1137. }
  1138. }
  1139. else {
  1140. singleKeyHandler(type, this[kSanitizeKey](key), value);
  1141. }
  1142. }
  1143. [kSanitizeKey](key) {
  1144. if (key === '__proto__')
  1145. return '___proto___';
  1146. return key;
  1147. }
  1148. [kSetKey](key, set) {
  1149. this[kPopulateParserHintSingleValueDictionary](this[kSetKey].bind(this), 'key', key, set);
  1150. return this;
  1151. }
  1152. [kUnfreeze]() {
  1153. var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m;
  1154. const frozen = __classPrivateFieldGet(this, _YargsInstance_frozens, "f").pop();
  1155. assertNotStrictEqual(frozen, undefined, __classPrivateFieldGet(this, _YargsInstance_shim, "f"));
  1156. let configObjects;
  1157. (_a = this, _b = this, _c = this, _d = this, _e = this, _f = this, _g = this, _h = this, _j = this, _k = this, _l = this, _m = this, {
  1158. options: ({ set value(_o) { __classPrivateFieldSet(_a, _YargsInstance_options, _o, "f"); } }).value,
  1159. configObjects,
  1160. exitProcess: ({ set value(_o) { __classPrivateFieldSet(_b, _YargsInstance_exitProcess, _o, "f"); } }).value,
  1161. groups: ({ set value(_o) { __classPrivateFieldSet(_c, _YargsInstance_groups, _o, "f"); } }).value,
  1162. output: ({ set value(_o) { __classPrivateFieldSet(_d, _YargsInstance_output, _o, "f"); } }).value,
  1163. exitError: ({ set value(_o) { __classPrivateFieldSet(_e, _YargsInstance_exitError, _o, "f"); } }).value,
  1164. hasOutput: ({ set value(_o) { __classPrivateFieldSet(_f, _YargsInstance_hasOutput, _o, "f"); } }).value,
  1165. parsed: this.parsed,
  1166. strict: ({ set value(_o) { __classPrivateFieldSet(_g, _YargsInstance_strict, _o, "f"); } }).value,
  1167. strictCommands: ({ set value(_o) { __classPrivateFieldSet(_h, _YargsInstance_strictCommands, _o, "f"); } }).value,
  1168. strictOptions: ({ set value(_o) { __classPrivateFieldSet(_j, _YargsInstance_strictOptions, _o, "f"); } }).value,
  1169. completionCommand: ({ set value(_o) { __classPrivateFieldSet(_k, _YargsInstance_completionCommand, _o, "f"); } }).value,
  1170. parseFn: ({ set value(_o) { __classPrivateFieldSet(_l, _YargsInstance_parseFn, _o, "f"); } }).value,
  1171. parseContext: ({ set value(_o) { __classPrivateFieldSet(_m, _YargsInstance_parseContext, _o, "f"); } }).value,
  1172. } = frozen);
  1173. __classPrivateFieldGet(this, _YargsInstance_options, "f").configObjects = configObjects;
  1174. __classPrivateFieldGet(this, _YargsInstance_usage, "f").unfreeze();
  1175. __classPrivateFieldGet(this, _YargsInstance_validation, "f").unfreeze();
  1176. __classPrivateFieldGet(this, _YargsInstance_command, "f").unfreeze();
  1177. __classPrivateFieldGet(this, _YargsInstance_globalMiddleware, "f").unfreeze();
  1178. }
  1179. [kValidateAsync](validation, argv) {
  1180. return maybeAsyncResult(argv, result => {
  1181. validation(result);
  1182. return result;
  1183. });
  1184. }
  1185. getInternalMethods() {
  1186. return {
  1187. getCommandInstance: this[kGetCommandInstance].bind(this),
  1188. getContext: this[kGetContext].bind(this),
  1189. getHasOutput: this[kGetHasOutput].bind(this),
  1190. getLoggerInstance: this[kGetLoggerInstance].bind(this),
  1191. getParseContext: this[kGetParseContext].bind(this),
  1192. getParserConfiguration: this[kGetParserConfiguration].bind(this),
  1193. getUsageInstance: this[kGetUsageInstance].bind(this),
  1194. getValidationInstance: this[kGetValidationInstance].bind(this),
  1195. hasParseCallback: this[kHasParseCallback].bind(this),
  1196. isGlobalContext: this[kIsGlobalContext].bind(this),
  1197. postProcess: this[kPostProcess].bind(this),
  1198. reset: this[kReset].bind(this),
  1199. runValidation: this[kRunValidation].bind(this),
  1200. runYargsParserAndExecuteCommands: this[kRunYargsParserAndExecuteCommands].bind(this),
  1201. setHasOutput: this[kSetHasOutput].bind(this),
  1202. };
  1203. }
  1204. [kGetCommandInstance]() {
  1205. return __classPrivateFieldGet(this, _YargsInstance_command, "f");
  1206. }
  1207. [kGetContext]() {
  1208. return __classPrivateFieldGet(this, _YargsInstance_context, "f");
  1209. }
  1210. [kGetHasOutput]() {
  1211. return __classPrivateFieldGet(this, _YargsInstance_hasOutput, "f");
  1212. }
  1213. [kGetLoggerInstance]() {
  1214. return __classPrivateFieldGet(this, _YargsInstance_logger, "f");
  1215. }
  1216. [kGetParseContext]() {
  1217. return __classPrivateFieldGet(this, _YargsInstance_parseContext, "f") || {};
  1218. }
  1219. [kGetUsageInstance]() {
  1220. return __classPrivateFieldGet(this, _YargsInstance_usage, "f");
  1221. }
  1222. [kGetValidationInstance]() {
  1223. return __classPrivateFieldGet(this, _YargsInstance_validation, "f");
  1224. }
  1225. [kHasParseCallback]() {
  1226. return !!__classPrivateFieldGet(this, _YargsInstance_parseFn, "f");
  1227. }
  1228. [kIsGlobalContext]() {
  1229. return __classPrivateFieldGet(this, _YargsInstance_isGlobalContext, "f");
  1230. }
  1231. [kPostProcess](argv, populateDoubleDash, calledFromCommand, runGlobalMiddleware) {
  1232. if (calledFromCommand)
  1233. return argv;
  1234. if (isPromise(argv))
  1235. return argv;
  1236. if (!populateDoubleDash) {
  1237. argv = this[kCopyDoubleDash](argv);
  1238. }
  1239. const parsePositionalNumbers = this[kGetParserConfiguration]()['parse-positional-numbers'] ||
  1240. this[kGetParserConfiguration]()['parse-positional-numbers'] === undefined;
  1241. if (parsePositionalNumbers) {
  1242. argv = this[kParsePositionalNumbers](argv);
  1243. }
  1244. if (runGlobalMiddleware) {
  1245. argv = applyMiddleware(argv, this, __classPrivateFieldGet(this, _YargsInstance_globalMiddleware, "f").getMiddleware(), false);
  1246. }
  1247. return argv;
  1248. }
  1249. [kReset](aliases = {}) {
  1250. __classPrivateFieldSet(this, _YargsInstance_options, __classPrivateFieldGet(this, _YargsInstance_options, "f") || {}, "f");
  1251. const tmpOptions = {};
  1252. tmpOptions.local = __classPrivateFieldGet(this, _YargsInstance_options, "f").local || [];
  1253. tmpOptions.configObjects = __classPrivateFieldGet(this, _YargsInstance_options, "f").configObjects || [];
  1254. const localLookup = {};
  1255. tmpOptions.local.forEach(l => {
  1256. localLookup[l] = true;
  1257. (aliases[l] || []).forEach(a => {
  1258. localLookup[a] = true;
  1259. });
  1260. });
  1261. Object.assign(__classPrivateFieldGet(this, _YargsInstance_preservedGroups, "f"), Object.keys(__classPrivateFieldGet(this, _YargsInstance_groups, "f")).reduce((acc, groupName) => {
  1262. const keys = __classPrivateFieldGet(this, _YargsInstance_groups, "f")[groupName].filter(key => !(key in localLookup));
  1263. if (keys.length > 0) {
  1264. acc[groupName] = keys;
  1265. }
  1266. return acc;
  1267. }, {}));
  1268. __classPrivateFieldSet(this, _YargsInstance_groups, {}, "f");
  1269. const arrayOptions = [
  1270. 'array',
  1271. 'boolean',
  1272. 'string',
  1273. 'skipValidation',
  1274. 'count',
  1275. 'normalize',
  1276. 'number',
  1277. 'hiddenOptions',
  1278. ];
  1279. const objectOptions = [
  1280. 'narg',
  1281. 'key',
  1282. 'alias',
  1283. 'default',
  1284. 'defaultDescription',
  1285. 'config',
  1286. 'choices',
  1287. 'demandedOptions',
  1288. 'demandedCommands',
  1289. 'deprecatedOptions',
  1290. ];
  1291. arrayOptions.forEach(k => {
  1292. tmpOptions[k] = (__classPrivateFieldGet(this, _YargsInstance_options, "f")[k] || []).filter((k) => !localLookup[k]);
  1293. });
  1294. objectOptions.forEach((k) => {
  1295. tmpOptions[k] = objFilter(__classPrivateFieldGet(this, _YargsInstance_options, "f")[k], k => !localLookup[k]);
  1296. });
  1297. tmpOptions.envPrefix = __classPrivateFieldGet(this, _YargsInstance_options, "f").envPrefix;
  1298. __classPrivateFieldSet(this, _YargsInstance_options, tmpOptions, "f");
  1299. __classPrivateFieldSet(this, _YargsInstance_usage, __classPrivateFieldGet(this, _YargsInstance_usage, "f")
  1300. ? __classPrivateFieldGet(this, _YargsInstance_usage, "f").reset(localLookup)
  1301. : Usage(this, __classPrivateFieldGet(this, _YargsInstance_shim, "f")), "f");
  1302. __classPrivateFieldSet(this, _YargsInstance_validation, __classPrivateFieldGet(this, _YargsInstance_validation, "f")
  1303. ? __classPrivateFieldGet(this, _YargsInstance_validation, "f").reset(localLookup)
  1304. : Validation(this, __classPrivateFieldGet(this, _YargsInstance_usage, "f"), __classPrivateFieldGet(this, _YargsInstance_shim, "f")), "f");
  1305. __classPrivateFieldSet(this, _YargsInstance_command, __classPrivateFieldGet(this, _YargsInstance_command, "f")
  1306. ? __classPrivateFieldGet(this, _YargsInstance_command, "f").reset()
  1307. : Command(__classPrivateFieldGet(this, _YargsInstance_usage, "f"), __classPrivateFieldGet(this, _YargsInstance_validation, "f"), __classPrivateFieldGet(this, _YargsInstance_globalMiddleware, "f"), __classPrivateFieldGet(this, _YargsInstance_shim, "f")), "f");
  1308. if (!__classPrivateFieldGet(this, _YargsInstance_completion, "f"))
  1309. __classPrivateFieldSet(this, _YargsInstance_completion, Completion(this, __classPrivateFieldGet(this, _YargsInstance_usage, "f"), __classPrivateFieldGet(this, _YargsInstance_command, "f"), __classPrivateFieldGet(this, _YargsInstance_shim, "f")), "f");
  1310. __classPrivateFieldGet(this, _YargsInstance_globalMiddleware, "f").reset();
  1311. __classPrivateFieldSet(this, _YargsInstance_completionCommand, null, "f");
  1312. __classPrivateFieldSet(this, _YargsInstance_output, '', "f");
  1313. __classPrivateFieldSet(this, _YargsInstance_exitError, null, "f");
  1314. __classPrivateFieldSet(this, _YargsInstance_hasOutput, false, "f");
  1315. this.parsed = false;
  1316. return this;
  1317. }
  1318. [kRebase](base, dir) {
  1319. return __classPrivateFieldGet(this, _YargsInstance_shim, "f").path.relative(base, dir);
  1320. }
  1321. [kRunYargsParserAndExecuteCommands](args, shortCircuit, calledFromCommand, commandIndex = 0, helpOnly = false) {
  1322. let skipValidation = !!calledFromCommand || helpOnly;
  1323. args = args || __classPrivateFieldGet(this, _YargsInstance_processArgs, "f");
  1324. __classPrivateFieldGet(this, _YargsInstance_options, "f").__ = __classPrivateFieldGet(this, _YargsInstance_shim, "f").y18n.__;
  1325. __classPrivateFieldGet(this, _YargsInstance_options, "f").configuration = this[kGetParserConfiguration]();
  1326. const populateDoubleDash = !!__classPrivateFieldGet(this, _YargsInstance_options, "f").configuration['populate--'];
  1327. const config = Object.assign({}, __classPrivateFieldGet(this, _YargsInstance_options, "f").configuration, {
  1328. 'populate--': true,
  1329. });
  1330. const parsed = __classPrivateFieldGet(this, _YargsInstance_shim, "f").Parser.detailed(args, Object.assign({}, __classPrivateFieldGet(this, _YargsInstance_options, "f"), {
  1331. configuration: { 'parse-positional-numbers': false, ...config },
  1332. }));
  1333. const argv = Object.assign(parsed.argv, __classPrivateFieldGet(this, _YargsInstance_parseContext, "f"));
  1334. let argvPromise = undefined;
  1335. const aliases = parsed.aliases;
  1336. let helpOptSet = false;
  1337. let versionOptSet = false;
  1338. Object.keys(argv).forEach(key => {
  1339. if (key === __classPrivateFieldGet(this, _YargsInstance_helpOpt, "f") && argv[key]) {
  1340. helpOptSet = true;
  1341. }
  1342. else if (key === __classPrivateFieldGet(this, _YargsInstance_versionOpt, "f") && argv[key]) {
  1343. versionOptSet = true;
  1344. }
  1345. });
  1346. argv.$0 = this.$0;
  1347. this.parsed = parsed;
  1348. if (commandIndex === 0) {
  1349. __classPrivateFieldGet(this, _YargsInstance_usage, "f").clearCachedHelpMessage();
  1350. }
  1351. try {
  1352. this[kGuessLocale]();
  1353. if (shortCircuit) {
  1354. return this[kPostProcess](argv, populateDoubleDash, !!calledFromCommand, false);
  1355. }
  1356. if (__classPrivateFieldGet(this, _YargsInstance_helpOpt, "f")) {
  1357. const helpCmds = [__classPrivateFieldGet(this, _YargsInstance_helpOpt, "f")]
  1358. .concat(aliases[__classPrivateFieldGet(this, _YargsInstance_helpOpt, "f")] || [])
  1359. .filter(k => k.length > 1);
  1360. if (helpCmds.includes('' + argv._[argv._.length - 1])) {
  1361. argv._.pop();
  1362. helpOptSet = true;
  1363. }
  1364. }
  1365. __classPrivateFieldSet(this, _YargsInstance_isGlobalContext, false, "f");
  1366. const handlerKeys = __classPrivateFieldGet(this, _YargsInstance_command, "f").getCommands();
  1367. const requestCompletions = __classPrivateFieldGet(this, _YargsInstance_completion, "f").completionKey in argv;
  1368. const skipRecommendation = helpOptSet || requestCompletions || helpOnly;
  1369. if (argv._.length) {
  1370. if (handlerKeys.length) {
  1371. let firstUnknownCommand;
  1372. for (let i = commandIndex || 0, cmd; argv._[i] !== undefined; i++) {
  1373. cmd = String(argv._[i]);
  1374. if (handlerKeys.includes(cmd) && cmd !== __classPrivateFieldGet(this, _YargsInstance_completionCommand, "f")) {
  1375. const innerArgv = __classPrivateFieldGet(this, _YargsInstance_command, "f").runCommand(cmd, this, parsed, i + 1, helpOnly, helpOptSet || versionOptSet || helpOnly);
  1376. return this[kPostProcess](innerArgv, populateDoubleDash, !!calledFromCommand, false);
  1377. }
  1378. else if (!firstUnknownCommand &&
  1379. cmd !== __classPrivateFieldGet(this, _YargsInstance_completionCommand, "f")) {
  1380. firstUnknownCommand = cmd;
  1381. break;
  1382. }
  1383. }
  1384. if (!__classPrivateFieldGet(this, _YargsInstance_command, "f").hasDefaultCommand() &&
  1385. __classPrivateFieldGet(this, _YargsInstance_recommendCommands, "f") &&
  1386. firstUnknownCommand &&
  1387. !skipRecommendation) {
  1388. __classPrivateFieldGet(this, _YargsInstance_validation, "f").recommendCommands(firstUnknownCommand, handlerKeys);
  1389. }
  1390. }
  1391. if (__classPrivateFieldGet(this, _YargsInstance_completionCommand, "f") &&
  1392. argv._.includes(__classPrivateFieldGet(this, _YargsInstance_completionCommand, "f")) &&
  1393. !requestCompletions) {
  1394. if (__classPrivateFieldGet(this, _YargsInstance_exitProcess, "f"))
  1395. setBlocking(true);
  1396. this.showCompletionScript();
  1397. this.exit(0);
  1398. }
  1399. }
  1400. if (__classPrivateFieldGet(this, _YargsInstance_command, "f").hasDefaultCommand() && !skipRecommendation) {
  1401. const innerArgv = __classPrivateFieldGet(this, _YargsInstance_command, "f").runCommand(null, this, parsed, 0, helpOnly, helpOptSet || versionOptSet || helpOnly);
  1402. return this[kPostProcess](innerArgv, populateDoubleDash, !!calledFromCommand, false);
  1403. }
  1404. if (requestCompletions) {
  1405. if (__classPrivateFieldGet(this, _YargsInstance_exitProcess, "f"))
  1406. setBlocking(true);
  1407. args = [].concat(args);
  1408. const completionArgs = args.slice(args.indexOf(`--${__classPrivateFieldGet(this, _YargsInstance_completion, "f").completionKey}`) + 1);
  1409. __classPrivateFieldGet(this, _YargsInstance_completion, "f").getCompletion(completionArgs, (err, completions) => {
  1410. if (err)
  1411. throw new YError(err.message);
  1412. (completions || []).forEach(completion => {
  1413. __classPrivateFieldGet(this, _YargsInstance_logger, "f").log(completion);
  1414. });
  1415. this.exit(0);
  1416. });
  1417. return this[kPostProcess](argv, !populateDoubleDash, !!calledFromCommand, false);
  1418. }
  1419. if (!__classPrivateFieldGet(this, _YargsInstance_hasOutput, "f")) {
  1420. if (helpOptSet) {
  1421. if (__classPrivateFieldGet(this, _YargsInstance_exitProcess, "f"))
  1422. setBlocking(true);
  1423. skipValidation = true;
  1424. this.showHelp('log');
  1425. this.exit(0);
  1426. }
  1427. else if (versionOptSet) {
  1428. if (__classPrivateFieldGet(this, _YargsInstance_exitProcess, "f"))
  1429. setBlocking(true);
  1430. skipValidation = true;
  1431. __classPrivateFieldGet(this, _YargsInstance_usage, "f").showVersion('log');
  1432. this.exit(0);
  1433. }
  1434. }
  1435. if (!skipValidation && __classPrivateFieldGet(this, _YargsInstance_options, "f").skipValidation.length > 0) {
  1436. skipValidation = Object.keys(argv).some(key => __classPrivateFieldGet(this, _YargsInstance_options, "f").skipValidation.indexOf(key) >= 0 && argv[key] === true);
  1437. }
  1438. if (!skipValidation) {
  1439. if (parsed.error)
  1440. throw new YError(parsed.error.message);
  1441. if (!requestCompletions) {
  1442. const validation = this[kRunValidation](aliases, {}, parsed.error);
  1443. if (!calledFromCommand) {
  1444. argvPromise = applyMiddleware(argv, this, __classPrivateFieldGet(this, _YargsInstance_globalMiddleware, "f").getMiddleware(), true);
  1445. }
  1446. argvPromise = this[kValidateAsync](validation, argvPromise !== null && argvPromise !== void 0 ? argvPromise : argv);
  1447. if (isPromise(argvPromise) && !calledFromCommand) {
  1448. argvPromise = argvPromise.then(() => {
  1449. return applyMiddleware(argv, this, __classPrivateFieldGet(this, _YargsInstance_globalMiddleware, "f").getMiddleware(), false);
  1450. });
  1451. }
  1452. }
  1453. }
  1454. }
  1455. catch (err) {
  1456. if (err instanceof YError)
  1457. __classPrivateFieldGet(this, _YargsInstance_usage, "f").fail(err.message, err);
  1458. else
  1459. throw err;
  1460. }
  1461. return this[kPostProcess](argvPromise !== null && argvPromise !== void 0 ? argvPromise : argv, populateDoubleDash, !!calledFromCommand, true);
  1462. }
  1463. [kRunValidation](aliases, positionalMap, parseErrors, isDefaultCommand) {
  1464. const demandedOptions = { ...this.getDemandedOptions() };
  1465. return (argv) => {
  1466. if (parseErrors)
  1467. throw new YError(parseErrors.message);
  1468. __classPrivateFieldGet(this, _YargsInstance_validation, "f").nonOptionCount(argv);
  1469. __classPrivateFieldGet(this, _YargsInstance_validation, "f").requiredArguments(argv, demandedOptions);
  1470. let failedStrictCommands = false;
  1471. if (__classPrivateFieldGet(this, _YargsInstance_strictCommands, "f")) {
  1472. failedStrictCommands = __classPrivateFieldGet(this, _YargsInstance_validation, "f").unknownCommands(argv);
  1473. }
  1474. if (__classPrivateFieldGet(this, _YargsInstance_strict, "f") && !failedStrictCommands) {
  1475. __classPrivateFieldGet(this, _YargsInstance_validation, "f").unknownArguments(argv, aliases, positionalMap, !!isDefaultCommand);
  1476. }
  1477. else if (__classPrivateFieldGet(this, _YargsInstance_strictOptions, "f")) {
  1478. __classPrivateFieldGet(this, _YargsInstance_validation, "f").unknownArguments(argv, aliases, {}, false, false);
  1479. }
  1480. __classPrivateFieldGet(this, _YargsInstance_validation, "f").limitedChoices(argv);
  1481. __classPrivateFieldGet(this, _YargsInstance_validation, "f").implications(argv);
  1482. __classPrivateFieldGet(this, _YargsInstance_validation, "f").conflicting(argv);
  1483. };
  1484. }
  1485. [kSetHasOutput]() {
  1486. __classPrivateFieldSet(this, _YargsInstance_hasOutput, true, "f");
  1487. }
  1488. [kTrackManuallySetKeys](keys) {
  1489. if (typeof keys === 'string') {
  1490. __classPrivateFieldGet(this, _YargsInstance_options, "f").key[keys] = true;
  1491. }
  1492. else {
  1493. for (const k of keys) {
  1494. __classPrivateFieldGet(this, _YargsInstance_options, "f").key[k] = true;
  1495. }
  1496. }
  1497. }
  1498. }
  1499. export function isYargsInstance(y) {
  1500. return !!y && typeof y.getInternalMethods === 'function';
  1501. }