Skip to content

Commit

Permalink
fix: use the name passed to the options decorator for the name passed…
Browse files Browse the repository at this point in the history
… to the command

There was already a test for this, that I didn't realize was initiall incorrect, so
I was able to verify this does indeed work as expected
  • Loading branch information
jmcdo29 committed Nov 10, 2023
1 parent b5f2273 commit a3b683d
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 4 deletions.
5 changes: 5 additions & 0 deletions .changeset/kind-turkeys-type.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'nest-commander': patch
---

Remap the options to the name passed in the `@Options()` decorator, if provided
2 changes: 1 addition & 1 deletion integration/option-choices/src/option-choices.command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export class OptionsTestCommand extends CommandRunner {
) {
super();
}
async run(_args: never[], options: { choice: string }) {
async run(_args: never[], options: { choices: string }) {
this.logger.log(options);
}

Expand Down
4 changes: 2 additions & 2 deletions integration/option-choices/test/option-choices.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,14 @@ OptionChoiceSuite(
'Send in option "yes"',
async ({ commandInstance, logMock }) => {
await CommandTestFactory.run(commandInstance, ['-c', 'yes']);
equal(logMock.firstCall?.args[0], { choice: 'yes' });
equal(logMock.firstCall?.args[0], { choices: 'yes' });
},
);
OptionChoiceSuite(
'Send in option "no"',
async ({ commandInstance, logMock }) => {
await CommandTestFactory.run(commandInstance, ['-c', 'no']);
equal(logMock.firstCall?.args[0], { choice: 'no' });
equal(logMock.firstCall?.args[0], { choices: 'no' });
},
);
OptionChoiceSuite(
Expand Down
10 changes: 9 additions & 1 deletion packages/nest-commander/src/command-runner.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,8 @@ ${cliPluginError(
newCommand.passThroughOptions(true);
}

const optionNameMap: Record<string, string> = {};

for (const option of command.params) {
const {
flags,
Expand Down Expand Up @@ -191,6 +193,7 @@ ${cliPluginError(
}
commandOption.argParser(handler);
newCommand.addOption(commandOption);
optionNameMap[commandOption.name()] = optionName || commandOption.name();
}
for (const help of command.help ?? []) {
newCommand.addHelpText(
Expand All @@ -202,7 +205,12 @@ ${cliPluginError(
newCommand.action(async () => {
try {
command.instance.run.bind(command.instance);
return await command.instance.run(newCommand.args, newCommand.opts());
const passedOptions = newCommand.opts();
const trueOptions: Record<string, string> = {};
for (const opt in passedOptions) {
trueOptions[optionNameMap[opt]] = passedOptions[opt];
}
return await command.instance.run(newCommand.args, trueOptions);
} catch (err) {
if (err instanceof Error) {
if (err.message.includes('Cannot read properties of undefined')) {
Expand Down

0 comments on commit a3b683d

Please sign in to comment.