Add copyInheritedSettings (#1557)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
* Factor out copySettings so can be used in programs using addCommand. * Fill out tests for properties for copySettings * Add TypeScript * Rename
authored by John Gee · committed by GitHub · parent 80054ba · Browse files · 5 files·+181 added−20 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+181 added−20 removed
- tests:+140 added−0 removed
- other:+41 added−20 removed
Provenance
- Commit
5517d25- Tree
ec4c5d6- Author
John Gee <john@ruru.gen.nz>- Committer
GitHub <noreply@github.com>- Parent
80054ba
5 files changed+181 added−20 removed
modified:lib/command.js+31 added-20 removed
72 unmodified lines73 this._helpConfiguration = {};74 }75 76 /**77+ * Copy settings that are useful to have in common across root command and subcommands.78+ *79+ * (Used internally when adding a command using `.command()` so subcommands inherit parent settings.)80+ *81+ * @param {Command} sourceCommand82+ * @return {Command} returns `this` for executable command83+ */84+ copyInheritedSettings(sourceCommand) {85+ this._outputConfiguration = sourceCommand._outputConfiguration;86+ this._hasHelpOption = sourceCommand._hasHelpOption;87+ this._helpFlags = sourceCommand._helpFlags;88+ this._helpDescription = sourceCommand._helpDescription;89+ this._helpShortFlag = sourceCommand._helpShortFlag;90+ this._helpLongFlag = sourceCommand._helpLongFlag;91+ this._helpCommandName = sourceCommand._helpCommandName;92+ this._helpCommandnameAndArgs = sourceCommand._helpCommandnameAndArgs;93+ this._helpCommandDescription = sourceCommand._helpCommandDescription;94+ this._helpConfiguration = sourceCommand._helpConfiguration;95+ this._exitCallback = sourceCommand._exitCallback;96+ this._storeOptionsAsProperties = sourceCommand._storeOptionsAsProperties;97+ this._combineFlagAndOptionalValue = sourceCommand._combineFlagAndOptionalValue;98+ this._allowExcessArguments = sourceCommand._allowExcessArguments;99+ this._enablePositionalOptions = sourceCommand._enablePositionalOptions;100+ this._showHelpAfterError = sourceCommand._showHelpAfterError;101+102+ return this;103+ }104+105+ /**106 * Define a command.107 *108 * There are two styles of command: pay attention to where to put the description.109 *26 unmodified lines136 desc = null;137 }138 opts = opts || {};139 const [, name, args] = nameAndArgs.match(/([^ ]+) *(.*)/);111- const cmd = this.createCommand(name);140 141+ const cmd = this.createCommand(name);142 if (desc) {143 cmd.description(desc);144 cmd._executableHandler = true;145 }146 if (opts.isDefault) this._defaultCommandName = cmd._name;118-119- cmd._outputConfiguration = this._outputConfiguration;120-147 cmd._hidden = !!(opts.noHelp || opts.hidden); // noHelp is deprecated old name for hidden122- cmd._hasHelpOption = this._hasHelpOption;123- cmd._helpFlags = this._helpFlags;124- cmd._helpDescription = this._helpDescription;125- cmd._helpShortFlag = this._helpShortFlag;126- cmd._helpLongFlag = this._helpLongFlag;127- cmd._helpCommandName = this._helpCommandName;128- cmd._helpCommandnameAndArgs = this._helpCommandnameAndArgs;129- cmd._helpCommandDescription = this._helpCommandDescription;130- cmd._helpConfiguration = this._helpConfiguration;131- cmd._exitCallback = this._exitCallback;132- cmd._storeOptionsAsProperties = this._storeOptionsAsProperties;133- cmd._combineFlagAndOptionalValue = this._combineFlagAndOptionalValue;134- cmd._allowExcessArguments = this._allowExcessArguments;135- cmd._enablePositionalOptions = this._enablePositionalOptions;136- cmd._showHelpAfterError = this._showHelpAfterError;137-148 cmd._executableFile = opts.executableFile || null; // Custom name for executable file, set missing to null to match constructor149 if (args) cmd.arguments(args);150 this.commands.push(cmd);151 cmd.parent = this;152+ cmd.copyInheritedSettings(this);153 154 if (desc) return this;155 return cmd;156 };1695 unmodified linesmodified:tests/command.chain.test.js+7 added
181 unmodified linesExpand all182183184185186187188189190191192193181 unmodified linesExpand all const program = new Command(); const result = program.showHelpAfterError(); expect(result).toBe(program); });
test('when call .copyInheritedSettings() then returns this', () => { const program = new Command(); const cmd = new Command(); const result = cmd.copyInheritedSettings(program); expect(result).toBe(cmd); });});added:tests/command.copySettings.test.js+133 added
1+const commander = require('../');2+3+// Tests some private properties as simpler than pure tests of observable behaviours.4+// Testing before and after values in some cases, to ensure value actually changes (when copied).5+6+test('when add subcommand with .command() then calls copyInheritedSettings from parent', () => {7+ const program = new commander.Command();8+9+ // This is a bit intrusive, but check expectation that copyInheritedSettings is called internally.10+ const copySettingMock = jest.fn();11+ program.createCommand = (name) => {12+ const cmd = new commander.Command(name);13+ cmd.copyInheritedSettings = copySettingMock;14+ return cmd;15+ };16+ program.command('sub');17+18+ expect(copySettingMock).toHaveBeenCalledWith(program);19+});20+21+describe('copyInheritedSettings property tests', () => {22+ test('when copyInheritedSettings then copies outputConfiguration(config)', () => {23+ const source = new commander.Command();24+ const cmd = new commander.Command();25+26+ source.configureOutput({ foo: 'bar' });27+ cmd.copyInheritedSettings(source);28+ expect(cmd.configureOutput().foo).toEqual('bar');29+ });30+31+ test('when copyInheritedSettings then copies helpOption(false)', () => {32+ const source = new commander.Command();33+ const cmd = new commander.Command();34+ expect(cmd._hasHelpOption).toBeTruthy();35+36+ source.helpOption(false);37+ cmd.copyInheritedSettings(source);38+ expect(cmd._hasHelpOption).toBeFalsy();39+ });40+41+ test('when copyInheritedSettings then copies helpOption(flags, description)', () => {42+ const source = new commander.Command();43+ const cmd = new commander.Command();44+45+ source.helpOption('-Z, --zz', 'ddd');46+ cmd.copyInheritedSettings(source);47+ expect(cmd._helpFlags).toBe('-Z, --zz');48+ expect(cmd._helpDescription).toBe('ddd');49+ expect(cmd._helpShortFlag).toBe('-Z');50+ expect(cmd._helpLongFlag).toBe('--zz');51+ });52+53+ test('when copyInheritedSettings then copies addHelpCommand(name, description)', () => {54+ const source = new commander.Command();55+ const cmd = new commander.Command();56+57+ source.addHelpCommand('HELP [cmd]', 'ddd');58+ cmd.copyInheritedSettings(source);59+ expect(cmd._helpCommandName).toBe('HELP');60+ expect(cmd._helpCommandnameAndArgs).toBe('HELP [cmd]');61+ expect(cmd._helpCommandDescription).toBe('ddd');62+ });63+64+ test('when copyInheritedSettings then copies configureHelp(config)', () => {65+ const source = new commander.Command();66+ const cmd = new commander.Command();67+68+ const configuration = { foo: 'bar', helpWidth: 123, sortSubcommands: true };69+ source.configureHelp(configuration);70+ cmd.copyInheritedSettings(source);71+ expect(cmd.configureHelp()).toEqual(configuration);72+ });73+74+ test('when copyInheritedSettings then copies exitOverride()', () => {75+ const source = new commander.Command();76+ const cmd = new commander.Command();77+78+ expect(cmd._exitCallback).toBeFalsy();79+ source.exitOverride();80+ cmd.copyInheritedSettings(source);81+ expect(cmd._exitCallback).toBeTruthy(); // actually a function82+ });83+84+ test('when copyInheritedSettings then copies storeOptionsAsProperties()', () => {85+ const source = new commander.Command();86+ const cmd = new commander.Command();87+88+ expect(cmd._storeOptionsAsProperties).toBeFalsy();89+ source.storeOptionsAsProperties();90+ cmd.copyInheritedSettings(source);91+ expect(cmd._storeOptionsAsProperties).toBeTruthy();92+ });93+94+ test('when copyInheritedSettings then copies combineFlagAndOptionalValue()', () => {95+ const source = new commander.Command();96+ const cmd = new commander.Command();97+98+ expect(cmd._combineFlagAndOptionalValue).toBeTruthy();99+ source.combineFlagAndOptionalValue(false);100+ cmd.copyInheritedSettings(source);101+ expect(cmd._combineFlagAndOptionalValue).toBeFalsy();102+ });103+104+ test('when copyInheritedSettings then copies allowExcessArguments()', () => {105+ const source = new commander.Command();106+ const cmd = new commander.Command();107+108+ expect(cmd._allowExcessArguments).toBeTruthy();109+ source.allowExcessArguments(false);110+ cmd.copyInheritedSettings(source);111+ expect(cmd._allowExcessArguments).toBeFalsy();112+ });113+114+ test('when copyInheritedSettings then copies enablePositionalOptions()', () => {115+ const source = new commander.Command();116+ const cmd = new commander.Command();117+118+ expect(cmd._enablePositionalOptions).toBeFalsy();119+ source.enablePositionalOptions();120+ cmd.copyInheritedSettings(source);121+ expect(cmd._enablePositionalOptions).toBeTruthy();122+ });123+124+ test('when copyInheritedSettings then copies showHelpAfterError()', () => {125+ const source = new commander.Command();126+ const cmd = new commander.Command();127+128+ expect(cmd._showHelpAfterError).toBeFalsy();129+ source.showHelpAfterError();130+ cmd.copyInheritedSettings(source);131+ expect(cmd._showHelpAfterError).toBeTruthy();132+ });133+});modified:typings/index.d.ts+7 added
388 unmodified lines389 /** Get configuration */390 configureOutput(): OutputConfiguration;391 392 /**393+ * Copy settings that are useful to have in common across root command and subcommands.394+ *395+ * (Used internally when adding a command using `.command()` so subcommands inherit parent settings.)396+ */397+ copyInheritedSettings(sourceCommand: Command): this;398+399+ /**400 * Display the help or a custom message after an error occurs.401 */402 showHelpAfterError(displayHelp?: boolean | string): this;403 336 unmodified linesmodified:typings/index.test-d.ts+3 added
283 unmodified lines284 visibleCommands: () => [] // override method285 }));286 expectType<commander.HelpConfiguration>(program.configureHelp());287 288+// copyInheritedSettings289+expectType<commander.Command>(program.copyInheritedSettings(new commander.Command()));290+291 // showHelpAfterError292 expectType<commander.Command>(program.showHelpAfterError());293 expectType<commander.Command>(program.showHelpAfterError(true));294 expectType<commander.Command>(program.showHelpAfterError('See --help'));106 unmodified lines