Skip to content
Myr

Refactor to wrap invalid argument (#1977)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Wrap handling command.invalidArgument to simplify calling code

authored by John Gee · committed by GitHub · parent 5740e59 · Browse files · 1 file+25 added−18 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

1 file+25 added−18 removed

  • other:+25 added−18 removed

1 file+25 added−18 removed

modified:lib/command.js+25 added-18 removed
512 unmodified lines
513 return new Option(flags, description);514 }515516 /**517 * Wrap parseArgs to catch 'commander.invalidArgument'.518 *519 * @param {Option | Argument} target520 * @param {string} value521 * @param {any} previous522 * @param {string} invalidArgumentMessage523 * @api private524 */525526 _callParseArg(target, value, previous, invalidArgumentMessage) {527 try {528 return target.parseArg(value, previous);529 } catch (err) {530 if (err.code === 'commander.invalidArgument') {531 const message = `${invalidArgumentMessage} ${err.message}`;532 this.error(message, { exitCode: err.exitCode, code: err.code });533 }534 throw err;535 }536 }537538 /**539 * Add an option.540 *541 * @param {Option} option542 * @return {Command} `this` command for chaining
26 unmodified lines
569570 // custom processing571 const oldValue = this.getOptionValue(name);572 if (val !== null && option.parseArg) {551 try {552 val = option.parseArg(val, oldValue);553 } catch (err) {554 if (err.code === 'commander.invalidArgument') {555 const message = `${invalidValueMessage} ${err.message}`;556 this.error(message, { exitCode: err.exitCode, code: err.code });557 }558 throw err;559 }573 val = this._callParseArg(option, val, oldValue, invalidValueMessage);574 } else if (val !== null && option.variadic) {575 val = option._concatValue(val, oldValue);576 }577
590 unmodified lines
1168 const myParseArg = (argument, value, previous) => {1169 // Extra processing for nice error message on parsing failure.1170 let parsedValue = value;1171 if (value !== null && argument.parseArg) {1158 try {1159 parsedValue = argument.parseArg(value, previous);1160 } catch (err) {1161 if (err.code === 'commander.invalidArgument') {1162 const message = `error: command-argument value '${value}' is invalid for argument '${argument.name()}'. ${err.message}`;1163 this.error(message, { exitCode: err.exitCode, code: err.code });1164 }1165 throw err;1166 }1172 const invalidValueMessage = `error: command-argument value '${value}' is invalid for argument '${argument.name()}'.`;1173 parsedValue = this._callParseArg(argument, value, previous, invalidValueMessage);1174 }1175 return parsedValue;1176 };1177
1032 unmodified lines