yargs-factory.js 76 KB

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