Skip to content
Myr

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

5 files+181 added−20 removed

modified:lib/command.js+31 added-20 removed
72 unmodified lines
73 this._helpConfiguration = {};74 }7576 /**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;101102 return this;103 }104105 /**106 * Define a command.107 *108 * There are two styles of command: pay attention to where to put the description.109 *
26 unmodified lines
136 desc = null;137 }138 opts = opts || {};139 const [, name, args] = nameAndArgs.match(/([^ ]+) *(.*)/);111 const cmd = this.createCommand(name);140141 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;118119 cmd._outputConfiguration = this._outputConfiguration;120147 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;137148 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);153154 if (desc) return this;155 return cmd;156 };
1695 unmodified lines
modified:tests/command.chain.test.js+7 added
added:tests/command.copySettings.test.js+133 added
1const commander = require('../');23// 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).56test('when add subcommand with .command() then calls copyInheritedSettings from parent', () => {7 const program = new commander.Command();89 // 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');1718 expect(copySettingMock).toHaveBeenCalledWith(program);19});2021describe('copyInheritedSettings property tests', () => {22 test('when copyInheritedSettings then copies outputConfiguration(config)', () => {23 const source = new commander.Command();24 const cmd = new commander.Command();2526 source.configureOutput({ foo: 'bar' });27 cmd.copyInheritedSettings(source);28 expect(cmd.configureOutput().foo).toEqual('bar');29 });3031 test('when copyInheritedSettings then copies helpOption(false)', () => {32 const source = new commander.Command();33 const cmd = new commander.Command();34 expect(cmd._hasHelpOption).toBeTruthy();3536 source.helpOption(false);37 cmd.copyInheritedSettings(source);38 expect(cmd._hasHelpOption).toBeFalsy();39 });4041 test('when copyInheritedSettings then copies helpOption(flags, description)', () => {42 const source = new commander.Command();43 const cmd = new commander.Command();4445 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 });5253 test('when copyInheritedSettings then copies addHelpCommand(name, description)', () => {54 const source = new commander.Command();55 const cmd = new commander.Command();5657 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 });6364 test('when copyInheritedSettings then copies configureHelp(config)', () => {65 const source = new commander.Command();66 const cmd = new commander.Command();6768 const configuration = { foo: 'bar', helpWidth: 123, sortSubcommands: true };69 source.configureHelp(configuration);70 cmd.copyInheritedSettings(source);71 expect(cmd.configureHelp()).toEqual(configuration);72 });7374 test('when copyInheritedSettings then copies exitOverride()', () => {75 const source = new commander.Command();76 const cmd = new commander.Command();7778 expect(cmd._exitCallback).toBeFalsy();79 source.exitOverride();80 cmd.copyInheritedSettings(source);81 expect(cmd._exitCallback).toBeTruthy(); // actually a function82 });8384 test('when copyInheritedSettings then copies storeOptionsAsProperties()', () => {85 const source = new commander.Command();86 const cmd = new commander.Command();8788 expect(cmd._storeOptionsAsProperties).toBeFalsy();89 source.storeOptionsAsProperties();90 cmd.copyInheritedSettings(source);91 expect(cmd._storeOptionsAsProperties).toBeTruthy();92 });9394 test('when copyInheritedSettings then copies combineFlagAndOptionalValue()', () => {95 const source = new commander.Command();96 const cmd = new commander.Command();9798 expect(cmd._combineFlagAndOptionalValue).toBeTruthy();99 source.combineFlagAndOptionalValue(false);100 cmd.copyInheritedSettings(source);101 expect(cmd._combineFlagAndOptionalValue).toBeFalsy();102 });103104 test('when copyInheritedSettings then copies allowExcessArguments()', () => {105 const source = new commander.Command();106 const cmd = new commander.Command();107108 expect(cmd._allowExcessArguments).toBeTruthy();109 source.allowExcessArguments(false);110 cmd.copyInheritedSettings(source);111 expect(cmd._allowExcessArguments).toBeFalsy();112 });113114 test('when copyInheritedSettings then copies enablePositionalOptions()', () => {115 const source = new commander.Command();116 const cmd = new commander.Command();117118 expect(cmd._enablePositionalOptions).toBeFalsy();119 source.enablePositionalOptions();120 cmd.copyInheritedSettings(source);121 expect(cmd._enablePositionalOptions).toBeTruthy();122 });123124 test('when copyInheritedSettings then copies showHelpAfterError()', () => {125 const source = new commander.Command();126 const cmd = new commander.Command();127128 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 lines
389 /** Get configuration */390 configureOutput(): OutputConfiguration;391392 /**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;398399 /**400 * Display the help or a custom message after an error occurs.401 */402 showHelpAfterError(displayHelp?: boolean | string): this;403
336 unmodified lines
modified:typings/index.test-d.ts+3 added
283 unmodified lines
284 visibleCommands: () => [] // override method285}));286expectType<commander.HelpConfiguration>(program.configureHelp());287288// copyInheritedSettings289expectType<commander.Command>(program.copyInheritedSettings(new commander.Command()));290291// showHelpAfterError292expectType<commander.Command>(program.showHelpAfterError());293expectType<commander.Command>(program.showHelpAfterError(true));294expectType<commander.Command>(program.showHelpAfterError('See --help'));
106 unmodified lines