yargs 17.0.0-candidate.7 → 17.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -111,63 +111,77 @@ export class CommandInstance {
111
111
  hasDefaultCommand() {
112
112
  return !!this.defaultCommand;
113
113
  }
114
- runCommand(command, yargs, parsed, commandIndex, helpOnly) {
114
+ runCommand(command, yargs, parsed, commandIndex, helpOnly, helpOrVersionSet) {
115
115
  const commandHandler = this.handlers[command] ||
116
116
  this.handlers[this.aliasMap[command]] ||
117
117
  this.defaultCommand;
118
- const currentContext = yargs.getContext();
118
+ const currentContext = yargs.getInternalMethods().getContext();
119
119
  const parentCommands = currentContext.commands.slice();
120
+ const isDefaultCommand = !command;
120
121
  if (command) {
121
122
  currentContext.commands.push(command);
122
123
  currentContext.fullCommands.push(commandHandler.original);
123
124
  }
124
- const builderResult = this.applyBuilderUpdateUsageAndParse(command, commandHandler, yargs, parsed.aliases, parentCommands, commandIndex, helpOnly);
125
+ const builderResult = this.applyBuilderUpdateUsageAndParse(isDefaultCommand, commandHandler, yargs, parsed.aliases, parentCommands, commandIndex, helpOnly, helpOrVersionSet);
125
126
  if (isPromise(builderResult)) {
126
127
  return builderResult.then(result => {
127
- return this.applyMiddlewareAndGetResult(command, commandHandler, result.innerArgv, currentContext, helpOnly, result.aliases, yargs);
128
+ return this.applyMiddlewareAndGetResult(isDefaultCommand, commandHandler, result.innerArgv, currentContext, helpOnly, result.aliases, yargs);
128
129
  });
129
130
  }
130
131
  else {
131
- return this.applyMiddlewareAndGetResult(command, commandHandler, builderResult.innerArgv, currentContext, helpOnly, builderResult.aliases, yargs);
132
+ return this.applyMiddlewareAndGetResult(isDefaultCommand, commandHandler, builderResult.innerArgv, currentContext, helpOnly, builderResult.aliases, yargs);
132
133
  }
133
134
  }
134
- applyBuilderUpdateUsageAndParse(command, commandHandler, yargs, aliases, parentCommands, commandIndex, helpOnly) {
135
+ applyBuilderUpdateUsageAndParse(isDefaultCommand, commandHandler, yargs, aliases, parentCommands, commandIndex, helpOnly, helpOrVersionSet) {
135
136
  const builder = commandHandler.builder;
136
137
  let innerYargs = yargs;
137
138
  if (isCommandBuilderCallback(builder)) {
138
- const builderOutput = builder(yargs.reset(aliases));
139
+ const builderOutput = builder(yargs.getInternalMethods().reset(aliases), helpOrVersionSet);
139
140
  if (isPromise(builderOutput)) {
140
141
  return builderOutput.then(output => {
141
142
  innerYargs = isYargsInstance(output) ? output : yargs;
142
- return this.parseAndUpdateUsage(command, commandHandler, innerYargs, parentCommands, commandIndex, helpOnly);
143
+ return this.parseAndUpdateUsage(isDefaultCommand, commandHandler, innerYargs, parentCommands, commandIndex, helpOnly);
143
144
  });
144
145
  }
145
146
  }
146
147
  else if (isCommandBuilderOptionDefinitions(builder)) {
147
- innerYargs = yargs.reset(aliases);
148
+ innerYargs = yargs.getInternalMethods().reset(aliases);
148
149
  Object.keys(commandHandler.builder).forEach(key => {
149
150
  innerYargs.option(key, builder[key]);
150
151
  });
151
152
  }
152
- return this.parseAndUpdateUsage(command, commandHandler, innerYargs, parentCommands, commandIndex, helpOnly);
153
+ return this.parseAndUpdateUsage(isDefaultCommand, commandHandler, innerYargs, parentCommands, commandIndex, helpOnly);
153
154
  }
154
- parseAndUpdateUsage(command, commandHandler, innerYargs, parentCommands, commandIndex, helpOnly) {
155
- if (!command)
156
- innerYargs.getUsageInstance().unfreeze();
155
+ parseAndUpdateUsage(isDefaultCommand, commandHandler, innerYargs, parentCommands, commandIndex, helpOnly) {
156
+ if (isDefaultCommand)
157
+ innerYargs.getInternalMethods().getUsageInstance().unfreeze();
157
158
  if (this.shouldUpdateUsage(innerYargs)) {
158
159
  innerYargs
160
+ .getInternalMethods()
159
161
  .getUsageInstance()
160
162
  .usage(this.usageFromParentCommandsCommandHandler(parentCommands, commandHandler), commandHandler.description);
161
163
  }
162
- const innerArgv = innerYargs._parseArgs(null, undefined, true, commandIndex, helpOnly);
163
- return {
164
- aliases: innerYargs.parsed.aliases,
165
- innerArgv: innerArgv,
166
- };
164
+ const innerArgv = innerYargs
165
+ .getInternalMethods()
166
+ .runYargsParserAndExecuteCommands(null, undefined, true, commandIndex, helpOnly);
167
+ if (isPromise(innerArgv)) {
168
+ return innerArgv.then(argv => {
169
+ return {
170
+ aliases: innerYargs.parsed.aliases,
171
+ innerArgv: argv,
172
+ };
173
+ });
174
+ }
175
+ else {
176
+ return {
177
+ aliases: innerYargs.parsed.aliases,
178
+ innerArgv: innerArgv,
179
+ };
180
+ }
167
181
  }
168
182
  shouldUpdateUsage(yargs) {
169
- return (!yargs.getUsageInstance().getUsageDisabled() &&
170
- yargs.getUsageInstance().getUsage().length === 0);
183
+ return (!yargs.getInternalMethods().getUsageInstance().getUsageDisabled() &&
184
+ yargs.getInternalMethods().getUsageInstance().getUsage().length === 0);
171
185
  }
172
186
  usageFromParentCommandsCommandHandler(parentCommands, commandHandler) {
173
187
  const c = DEFAULT_MARKER.test(commandHandler.original)
@@ -179,29 +193,33 @@ export class CommandInstance {
179
193
  pc.push(c);
180
194
  return `$0 ${pc.join(' ')}`;
181
195
  }
182
- applyMiddlewareAndGetResult(command, commandHandler, innerArgv, currentContext, helpOnly, aliases, yargs) {
196
+ applyMiddlewareAndGetResult(isDefaultCommand, commandHandler, innerArgv, currentContext, helpOnly, aliases, yargs) {
183
197
  let positionalMap = {};
184
- if (!yargs._hasOutput()) {
185
- positionalMap = this.populatePositionals(commandHandler, innerArgv, currentContext, yargs);
186
- }
187
198
  if (helpOnly)
188
199
  return innerArgv;
200
+ if (!yargs.getInternalMethods().getHasOutput()) {
201
+ positionalMap = this.populatePositionals(commandHandler, innerArgv, currentContext, yargs);
202
+ }
189
203
  const middlewares = this.globalMiddleware
190
204
  .getMiddleware()
191
205
  .slice(0)
192
206
  .concat(commandHandler.middlewares);
193
207
  innerArgv = applyMiddleware(innerArgv, yargs, middlewares, true);
194
- if (!yargs._hasOutput()) {
195
- const validation = yargs._runValidation(aliases, positionalMap, yargs.parsed.error, !command);
208
+ if (!yargs.getInternalMethods().getHasOutput()) {
209
+ const validation = yargs
210
+ .getInternalMethods()
211
+ .runValidation(aliases, positionalMap, yargs.parsed.error, isDefaultCommand);
196
212
  innerArgv = maybeAsyncResult(innerArgv, result => {
197
213
  validation(result);
198
214
  return result;
199
215
  });
200
216
  }
201
- if (commandHandler.handler && !yargs._hasOutput()) {
202
- yargs._setHasOutput();
217
+ if (commandHandler.handler && !yargs.getInternalMethods().getHasOutput()) {
218
+ yargs.getInternalMethods().setHasOutput();
203
219
  const populateDoubleDash = !!yargs.getOptions().configuration['populate--'];
204
- yargs._postProcess(innerArgv, populateDoubleDash, false, false);
220
+ yargs
221
+ .getInternalMethods()
222
+ .postProcess(innerArgv, populateDoubleDash, false, false);
205
223
  innerArgv = applyMiddleware(innerArgv, yargs, middlewares, false);
206
224
  innerArgv = maybeAsyncResult(innerArgv, result => {
207
225
  const handlerResult = commandHandler.handler(result);
@@ -212,18 +230,21 @@ export class CommandInstance {
212
230
  return result;
213
231
  }
214
232
  });
215
- yargs.getUsageInstance().cacheHelpMessage();
216
- if (isPromise(innerArgv) && !yargs._hasParseCallback()) {
233
+ if (!isDefaultCommand) {
234
+ yargs.getInternalMethods().getUsageInstance().cacheHelpMessage();
235
+ }
236
+ if (isPromise(innerArgv) &&
237
+ !yargs.getInternalMethods().hasParseCallback()) {
217
238
  innerArgv.catch(error => {
218
239
  try {
219
- yargs.getUsageInstance().fail(null, error);
240
+ yargs.getInternalMethods().getUsageInstance().fail(null, error);
220
241
  }
221
242
  catch (_err) {
222
243
  }
223
244
  });
224
245
  }
225
246
  }
226
- if (command) {
247
+ if (!isDefaultCommand) {
227
248
  currentContext.commands.pop();
228
249
  currentContext.fullCommands.pop();
229
250
  }
@@ -310,7 +331,10 @@ export class CommandInstance {
310
331
  configuration: config,
311
332
  }));
312
333
  if (parsed.error) {
313
- yargs.getUsageInstance().fail(parsed.error.message, parsed.error);
334
+ yargs
335
+ .getInternalMethods()
336
+ .getUsageInstance()
337
+ .fail(parsed.error.message, parsed.error);
314
338
  }
315
339
  else {
316
340
  const positionalKeys = Object.keys(positionalMap);
@@ -327,24 +351,27 @@ export class CommandInstance {
327
351
  }
328
352
  }
329
353
  runDefaultBuilderOn(yargs) {
330
- assertNotStrictEqual(this.defaultCommand, undefined, this.shim);
354
+ if (!this.defaultCommand)
355
+ return;
331
356
  if (this.shouldUpdateUsage(yargs)) {
332
357
  const commandString = DEFAULT_MARKER.test(this.defaultCommand.original)
333
358
  ? this.defaultCommand.original
334
359
  : this.defaultCommand.original.replace(/^[^[\]<>]*/, '$0 ');
335
360
  yargs
361
+ .getInternalMethods()
336
362
  .getUsageInstance()
337
363
  .usage(commandString, this.defaultCommand.description);
338
364
  }
339
365
  const builder = this.defaultCommand.builder;
340
366
  if (isCommandBuilderCallback(builder)) {
341
- builder(yargs);
367
+ return builder(yargs, true);
342
368
  }
343
369
  else if (!isCommandBuilderDefinition(builder)) {
344
370
  Object.keys(builder).forEach(key => {
345
371
  yargs.option(key, builder[key]);
346
372
  });
347
373
  }
374
+ return undefined;
348
375
  }
349
376
  moduleName(obj) {
350
377
  const mod = whichModule(obj);
@@ -396,12 +423,7 @@ export function isCommandBuilderDefinition(builder) {
396
423
  typeof builder.handler === 'function');
397
424
  }
398
425
  function isCommandAndAliases(cmd) {
399
- if (cmd.every(c => typeof c === 'string')) {
400
- return true;
401
- }
402
- else {
403
- return false;
404
- }
426
+ return cmd.every(c => typeof c === 'string');
405
427
  }
406
428
  export function isCommandBuilderCallback(builder) {
407
429
  return typeof builder === 'function';
@@ -5,7 +5,7 @@ export const completionShTemplate = `###-begin-{{app_name}}-completions-###
5
5
  # Installation: {{app_path}} {{completion_command}} >> ~/.bashrc
6
6
  # or {{app_path}} {{completion_command}} >> ~/.bash_profile on OSX.
7
7
  #
8
- _yargs_completions()
8
+ _{{app_name}}_yargs_completions()
9
9
  {
10
10
  local cur_word args type_list
11
11
 
@@ -24,7 +24,7 @@ _yargs_completions()
24
24
 
25
25
  return 0
26
26
  }
27
- complete -o default -F _yargs_completions {{app_name}}
27
+ complete -o default -F _{{app_name}}_yargs_completions {{app_name}}
28
28
  ###-end-{{app_name}}-completions-###
29
29
  `;
30
30
  export const completionZshTemplate = `#compdef {{app_name}}
@@ -13,7 +13,9 @@ export class Completion {
13
13
  this.completionKey = 'get-yargs-completions';
14
14
  this.aliases = null;
15
15
  this.customCompletionFunction = null;
16
- this.zshShell = (_c = (((_a = this.shim.getEnv('SHELL')) === null || _a === void 0 ? void 0 : _a.includes('zsh')) || ((_b = this.shim.getEnv('ZSH_NAME')) === null || _b === void 0 ? void 0 : _b.includes('zsh')))) !== null && _c !== void 0 ? _c : false;
16
+ this.zshShell =
17
+ (_c = (((_a = this.shim.getEnv('SHELL')) === null || _a === void 0 ? void 0 : _a.includes('zsh')) ||
18
+ ((_b = this.shim.getEnv('ZSH_NAME')) === null || _b === void 0 ? void 0 : _b.includes('zsh')))) !== null && _c !== void 0 ? _c : false;
17
19
  }
18
20
  defaultCompletion(args, argv, current, done) {
19
21
  const handlers = this.command.getCommandHandlers();
@@ -21,8 +23,8 @@ export class Completion {
21
23
  if (handlers[args[i]] && handlers[args[i]].builder) {
22
24
  const builder = handlers[args[i]].builder;
23
25
  if (isCommandBuilderCallback(builder)) {
24
- const y = this.yargs.reset();
25
- builder(y);
26
+ const y = this.yargs.getInternalMethods().reset();
27
+ builder(y, true);
26
28
  return y.argv;
27
29
  }
28
30
  }
@@ -33,7 +35,8 @@ export class Completion {
33
35
  done(null, completions);
34
36
  }
35
37
  commandCompletions(completions, args, current) {
36
- const parentCommands = this.yargs.getContext().commands;
38
+ const parentCommands = this.yargs.getInternalMethods().getContext()
39
+ .commands;
37
40
  if (!current.match(/^-/) &&
38
41
  parentCommands[parentCommands.length - 1] !== current) {
39
42
  this.usage.getCommands().forEach(usageCommand => {
@@ -30,7 +30,7 @@ export class GlobalMiddleware {
30
30
  addCoerceMiddleware(callback, option) {
31
31
  const aliases = this.yargs.getAliases();
32
32
  this.globalMiddleware = this.globalMiddleware.filter(m => {
33
- const toCheck = [...(aliases[option] ? aliases[option] : []), option];
33
+ const toCheck = [...(aliases[option] || []), option];
34
34
  if (!m.option)
35
35
  return true;
36
36
  else
@@ -4,8 +4,8 @@ import setBlocking from './utils/set-blocking.js';
4
4
  function isBoolean(fail) {
5
5
  return typeof fail === 'boolean';
6
6
  }
7
- export function usage(yargs, y18n, shim) {
8
- const __ = y18n.__;
7
+ export function usage(yargs, shim) {
8
+ const __ = shim.y18n.__;
9
9
  const self = {};
10
10
  const fails = [];
11
11
  self.failFn = function failFn(f) {
@@ -24,7 +24,7 @@ export function usage(yargs, y18n, shim) {
24
24
  };
25
25
  let failureOutput = false;
26
26
  self.fail = function fail(msg, err) {
27
- const logger = yargs._getLoggerInstance();
27
+ const logger = yargs.getInternalMethods().getLoggerInstance();
28
28
  if (fails.length) {
29
29
  for (let i = fails.length - 1; i >= 0; --i) {
30
30
  const fail = fails[i];
@@ -60,7 +60,7 @@ export function usage(yargs, y18n, shim) {
60
60
  if (yargs.getExitProcess()) {
61
61
  return yargs.exit(1);
62
62
  }
63
- else if (yargs._hasParseCallback()) {
63
+ else if (yargs.getInternalMethods().hasParseCallback()) {
64
64
  return yargs.exit(1, err);
65
65
  }
66
66
  else {
@@ -191,11 +191,12 @@ export function usage(yargs, y18n, shim) {
191
191
  }
192
192
  if (commands.length > 1 || (commands.length === 1 && !commands[0][2])) {
193
193
  ui.div(__('Commands:'));
194
- const context = yargs.getContext();
194
+ const context = yargs.getInternalMethods().getContext();
195
195
  const parentCommands = context.commands.length
196
196
  ? `${context.commands.join(' ')} `
197
197
  : '';
198
- if (yargs.getParserConfiguration()['sort-commands'] === true) {
198
+ if (yargs.getInternalMethods().getParserConfiguration()['sort-commands'] ===
199
+ true) {
199
200
  commands = commands.sort((a, b) => a[0].localeCompare(b[0]));
200
201
  }
201
202
  commands.forEach(command => {
@@ -443,7 +444,7 @@ export function usage(yargs, y18n, shim) {
443
444
  yargs.parsed.argv[yargs.getOptions().showHiddenOpt]);
444
445
  }
445
446
  self.showHelp = (level) => {
446
- const logger = yargs._getLoggerInstance();
447
+ const logger = yargs.getInternalMethods().getLoggerInstance();
447
448
  if (!level)
448
449
  level = 'error';
449
450
  const emit = typeof level === 'function' ? level : logger[level];
@@ -503,7 +504,7 @@ export function usage(yargs, y18n, shim) {
503
504
  version = ver;
504
505
  };
505
506
  self.showVersion = level => {
506
- const logger = yargs._getLoggerInstance();
507
+ const logger = yargs.getInternalMethods().getLoggerInstance();
507
508
  if (!level)
508
509
  level = 'error';
509
510
  const emit = typeof level === 'function' ? level : logger[level];
@@ -4,14 +4,9 @@ export function maybeAsyncResult(getResult, resultHandler, errorHandler = (err)
4
4
  }) {
5
5
  try {
6
6
  const result = isFunction(getResult) ? getResult() : getResult;
7
- if (isPromise(result)) {
8
- return result.then((result) => {
9
- return resultHandler(result);
10
- });
11
- }
12
- else {
13
- return resultHandler(result);
14
- }
7
+ return isPromise(result)
8
+ ? result.then((result) => resultHandler(result))
9
+ : resultHandler(result);
15
10
  }
16
11
  catch (err) {
17
12
  return errorHandler(err);
@@ -3,14 +3,14 @@ import { assertNotStrictEqual, } from './typings/common-types.js';
3
3
  import { levenshtein as distance } from './utils/levenshtein.js';
4
4
  import { objFilter } from './utils/obj-filter.js';
5
5
  const specialKeys = ['$0', '--', '_'];
6
- export function validation(yargs, usage, y18n, shim) {
7
- const __ = y18n.__;
8
- const __n = y18n.__n;
6
+ export function validation(yargs, usage, shim) {
7
+ const __ = shim.y18n.__;
8
+ const __n = shim.y18n.__n;
9
9
  const self = {};
10
10
  self.nonOptionCount = function nonOptionCount(argv) {
11
11
  const demandedCommands = yargs.getDemandedCommands();
12
12
  const positionalCount = argv._.length + (argv['--'] ? argv['--'].length : 0);
13
- const _s = positionalCount - yargs.getContext().commands.length;
13
+ const _s = positionalCount - yargs.getInternalMethods().getContext().commands.length;
14
14
  if (demandedCommands._ &&
15
15
  (_s < demandedCommands._.min || _s > demandedCommands._.max)) {
16
16
  if (_s < demandedCommands._.min) {
@@ -66,13 +66,16 @@ export function validation(yargs, usage, y18n, shim) {
66
66
  }
67
67
  };
68
68
  self.unknownArguments = function unknownArguments(argv, aliases, positionalMap, isDefaultCommand, checkPositionals = true) {
69
- const commandKeys = yargs.getCommandInstance().getCommands();
69
+ const commandKeys = yargs
70
+ .getInternalMethods()
71
+ .getCommandInstance()
72
+ .getCommands();
70
73
  const unknown = [];
71
- const currentContext = yargs.getContext();
74
+ const currentContext = yargs.getInternalMethods().getContext();
72
75
  Object.keys(argv).forEach(key => {
73
76
  if (specialKeys.indexOf(key) === -1 &&
74
77
  !Object.prototype.hasOwnProperty.call(positionalMap, key) &&
75
- !Object.prototype.hasOwnProperty.call(yargs._getParseContext(), key) &&
78
+ !Object.prototype.hasOwnProperty.call(yargs.getInternalMethods().getParseContext(), key) &&
76
79
  !self.isValidAndSomeAliasIsNotNew(key, aliases)) {
77
80
  unknown.push(key);
78
81
  }
@@ -92,9 +95,12 @@ export function validation(yargs, usage, y18n, shim) {
92
95
  }
93
96
  };
94
97
  self.unknownCommands = function unknownCommands(argv) {
95
- const commandKeys = yargs.getCommandInstance().getCommands();
98
+ const commandKeys = yargs
99
+ .getInternalMethods()
100
+ .getCommandInstance()
101
+ .getCommands();
96
102
  const unknown = [];
97
- const currentContext = yargs.getContext();
103
+ const currentContext = yargs.getInternalMethods().getContext();
98
104
  if (currentContext.commands.length > 0 || commandKeys.length > 0) {
99
105
  argv._.slice(currentContext.commands.length).forEach(key => {
100
106
  if (commandKeys.indexOf('' + key) === -1) {
@@ -115,13 +121,7 @@ export function validation(yargs, usage, y18n, shim) {
115
121
  return false;
116
122
  }
117
123
  const newAliases = yargs.parsed.newAliases;
118
- for (const a of [key, ...aliases[key]]) {
119
- if (!Object.prototype.hasOwnProperty.call(newAliases, a) ||
120
- !newAliases[key]) {
121
- return true;
122
- }
123
- }
124
- return false;
124
+ return [key, ...aliases[key]].some(a => !Object.prototype.hasOwnProperty.call(newAliases, a) || !newAliases[key]);
125
125
  };
126
126
  self.limitedChoices = function limitedChoices(argv) {
127
127
  const options = yargs.getOptions();