Skip to content

Class inheritance does not extend all functionality #942

@DanHulton

Description

@DanHulton

Is there an existing issue for this?

  • I have searched the existing issues

Current behavior

If you want to have a "parent command class" for global options, a common logging config, shared functions or constructor behaviour, it does not work as expected. Basic class functionality works, i.e. if you create a protected logger instance, that works, and shared functions work as expected, but trying to create an @Options definition that would be "global" among Commands that inherit this class does not work.

Minimum reproduction code

base-command.ts:

import { CommandRunner } from 'nest-commander';
import { InjectPinoLogger, PinoLogger } from 'nestjs-pino';

export abstract class BaseCommand extends CommandRunner {
  @InjectPinoLogger()
  protected readonly logger: PinoLogger;

  @Option({
    flags: '-c, --common <common>',
    description: 'A common flag',
  })
  parseCommon(val: string) {
    return val;
  }
}

test-command.ts:

import { Command, Option } from 'nest-commander';
import { BaseCommand } from './base-command';

@Command({
  name: 'test',
})
export class TestCommand extends BaseCommand {
  async run(inputs: string[], options: Record<string, any>): Promise<void> {
    this.logger.info(options, 'Options');
  }
}

Expected behavior

When running this code with the --common=something flag set, I would expect to see {'common': 'something'} in the logged options, but instead, the logged object is empty.

Package

  • nest-commander
  • nest-commander-schematics
  • nest-commander-testing

Package version

3.7.1

Node.js version

18.16.0

In which operating systems have you tested?

  • macOS
  • Windows
  • Linux

Other

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions