Skip to content
Myr

Merge branch 'develop' into release/12.x

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

authored by John Gee · committed by John Gee · parents 95d54c3, c7d39ca · Browse files · 5 files+58 added−36 removed
This commit

State

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

Diff stats

5 files+58 added−36 removed

  • other:+58 added−36 removed

5 files+58 added−36 removed

modified:lib/command.js+31 added-25 removed
514 unmodified lines
515 return new Option(flags, description);516 }517518 /**519 * Wrap parseArgs to catch 'commander.invalidArgument'.520 *521 * @param {Option | Argument} target522 * @param {string} value523 * @param {any} previous524 * @param {string} invalidArgumentMessage525 * @api private526 */527528 _callParseArg(target, value, previous, invalidArgumentMessage) {529 try {530 return target.parseArg(value, previous);531 } catch (err) {532 if (err.code === 'commander.invalidArgument') {533 const message = `${invalidArgumentMessage} ${err.message}`;534 this.error(message, { exitCode: err.exitCode, code: err.code });535 }536 throw err;537 }538 }539540 /**541 * Add an option.542 *543 * @param {Option} option544 * @return {Command} `this` command for chaining
26 unmodified lines
571572 // custom processing573 const oldValue = this.getOptionValue(name);574 if (val !== null && option.parseArg) {553 try {554 val = option.parseArg(val, oldValue);555 } catch (err) {556 if (err.code === 'commander.invalidArgument') {557 const message = `${invalidValueMessage} ${err.message}`;558 this.error(message, { exitCode: err.exitCode, code: err.code });559 }560 throw err;561 }575 val = this._callParseArg(option, val, oldValue, invalidValueMessage);576 } else if (val !== null && option.variadic) {577 val = option._concatValue(val, oldValue);578 }579
598 unmodified lines
1178 const myParseArg = (argument, value, previous) => {1179 // Extra processing for nice error message on parsing failure.1180 let parsedValue = value;1181 if (value !== null && argument.parseArg) {1168 try {1169 parsedValue = argument.parseArg(value, previous);1170 } catch (err) {1171 if (err.code === 'commander.invalidArgument') {1172 const message = `error: command-argument value '${value}' is invalid for argument '${argument.name()}'. ${err.message}`;1173 this.error(message, { exitCode: err.exitCode, code: err.code });1174 }1175 throw err;1176 }1182 const invalidValueMessage = `error: command-argument value '${value}' is invalid for argument '${argument.name()}'.`;1183 parsedValue = this._callParseArg(argument, value, previous, invalidValueMessage);1184 }1185 return parsedValue;1186 };1187
646 unmodified lines
1834 this.error(message, { code: 'commander.unknownCommand' });1835 }18361837 /**1831 * Set the program version to `str`.1838 * Get or set the program version.1839 *1833 * This method auto-registers the "-V, --version" flag1834 * which will print the version number when passed.1840 * This method auto-registers the "-V, --version" option which will print the version number.1841 *1836 * You can optionally supply the flags and description to override the defaults.1842 * You can optionally supply the flags and description to override the defaults.1843 *1838 * @param {string} str1844 * @param {string} [str]1845 * @param {string} [flags]1846 * @param {string} [description]1841 * @return {this | string} `this` command for chaining, or version string if no arguments1847 * @return {this | string | undefined} `this` command for chaining, or version string if no arguments1848 */18491850 version(str, flags, description) {1851 if (str === undefined) return this._version;1852 this._version = str;1853 flags = flags || '-V, --version';1854 description = description || 'output the version number';1855 const versionOption = this.createOption(flags, description);1850 this._versionOptionName = versionOption.attributeName();1856 this._versionOptionName = versionOption.attributeName(); // [sic] not defined in constructor, partly legacy, partly only needed at root1857 this.options.push(versionOption);1858 this.on('option:' + versionOption.name(), () => {1859 this._outputConfiguration.writeOut(`${str}\n`);1860 this._exit(0, 'commander.version', str);
358 unmodified lines
modified:typings/index.d.ts+14 added-1 removed
3 unmodified lines
4// Using method rather than property for method-signature-style, to document method overloads separately. Allow either.5/* eslint-disable @typescript-eslint/method-signature-style */6/* eslint-disable @typescript-eslint/no-explicit-any */78// This is a trick to encourage editor to suggest the known literals while still9// allowing any BaseType value.10// References:11// - https://github.com/microsoft/TypeScript/issues/2972912// - https://github.com/sindresorhus/type-fest/blob/main/source/literal-union.d.ts13// - https://github.com/sindresorhus/type-fest/blob/main/source/primitive.d.ts14type LiteralUnion<LiteralType, BaseType extends string | number> = LiteralType | (BaseType & Record<never, never>);1516export class CommanderError extends Error {17 code: string;18 exitCode: number;19 message: string;
259 unmodified lines
279}280281export type AddHelpTextPosition = 'beforeAll' | 'before' | 'after' | 'afterAll';282export type HookEvent = 'preSubcommand' | 'preAction' | 'postAction';275export type OptionValueSource = 'default' | 'config' | 'env' | 'cli' | 'implied';283// The source is a string so author can define their own too.284export type OptionValueSource = LiteralUnion<'default' | 'config' | 'env' | 'cli' | 'implied', string> | undefined;285286export type OptionValues = Record<string, any>;287288export class Command {
13 unmodified lines
302 *303 * You can optionally supply the flags and description to override the defaults.304 */305 version(str: string, flags?: string, description?: string): this;306 /**307 * Get the program version.308 */309 version(): string | undefined;310311 /**312 * Define a command, implemented using an action handler.313 *
589 unmodified lines
modified:typings/index.test-d.ts+1 added
34 unmodified lines
35// version36expectType<commander.Command>(program.version('1.2.3'));37expectType<commander.Command>(program.version('1.2.3', '-r,--revision'));38expectType<commander.Command>(program.version('1.2.3', '-r,--revision', 'show revision information'));39expectType<string | undefined>(program.version());4041// command (and CommandOptions)42expectType<commander.Command>(program.command('action'));43expectType<commander.Command>(program.command('action', { isDefault: true, hidden: true, noHelp: true }));
439 unmodified lines
added:jest.config.js+12 added
modified:package.json-10 removed