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
Provenance
- Commit
f570b08- Tree
63c1b1b- Author
John Gee <john@ruru.gen.nz>- Committer
John Gee <john@ruru.gen.nz>- Parent 1
95d54c3- Parent 2
c7d39ca
5 files changed+58 added−36 removed
modified:lib/command.js+31 added-25 removed
514 unmodified lines515 return new Option(flags, description);516 }517 518 /**519+ * Wrap parseArgs to catch 'commander.invalidArgument'.520+ *521+ * @param {Option | Argument} target522+ * @param {string} value523+ * @param {any} previous524+ * @param {string} invalidArgumentMessage525+ * @api private526+ */527+528+ _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+ }539+540+ /**541 * Add an option.542 *543 * @param {Option} option544 * @return {Command} `this` command for chaining26 unmodified lines571 572 // 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 lines1178 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 lines1834 this.error(message, { code: 'commander.unknownCommand' });1835 }1836 1837 /**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 */1849 1850 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 linesmodified:typings/index.d.ts+14 added-1 removed
3 unmodified lines4 // 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 */7 8+// 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.ts14+type LiteralUnion<LiteralType, BaseType extends string | number> = LiteralType | (BaseType & Record<never, never>);15+16 export class CommanderError extends Error {17 code: string;18 exitCode: number;19 message: string;259 unmodified lines279 }280 281 export type AddHelpTextPosition = 'beforeAll' | 'before' | 'after' | 'afterAll';282 export type HookEvent = 'preSubcommand' | 'preAction' | 'postAction';275-export type OptionValueSource = 'default' | 'config' | 'env' | 'cli' | 'implied';283+// The source is a string so author can define their own too.284+export type OptionValueSource = LiteralUnion<'default' | 'config' | 'env' | 'cli' | 'implied', string> | undefined;285 286 export type OptionValues = Record<string, any>;287 288 export class Command {13 unmodified lines302 *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;310 311 /**312 * Define a command, implemented using an action handler.313 *589 unmodified linesmodified:typings/index.test-d.ts+1 added
34 unmodified lines35 // version36 expectType<commander.Command>(program.version('1.2.3'));37 expectType<commander.Command>(program.version('1.2.3', '-r,--revision'));38 expectType<commander.Command>(program.version('1.2.3', '-r,--revision', 'show revision information'));39+expectType<string | undefined>(program.version());40 41 // command (and CommandOptions)42 expectType<commander.Command>(program.command('action'));43 expectType<commander.Command>(program.command('action', { isDefault: true, hidden: true, noHelp: true }));439 unmodified linesadded:jest.config.js+12 added
123456789101112const config = { testEnvironment: 'node', collectCoverage: true, transform: { '^.+\\.tsx?$': 'ts-jest' }, testPathIgnorePatterns: [ '/node_modules/' ]};
module.exports = config;modified:package.json-10 removed
71 unmodified lines7273747576777879808182838485767778798071 unmodified lines "tsd": "^0.28.1", "typescript": "^5.0.4" }, "types": "typings/index.d.ts", "jest": { "testEnvironment": "node", "collectCoverage": true, "transform": { "^.+\\.tsx?$": "ts-jest" }, "testPathIgnorePatterns": [ "/node_modules/" ] }, "engines": { "node": ">=16" }, "support": true}