question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

CommandTestFactory - Passing options (flags) returns wrong values

See original GitHub issue

It might be a lack of documentation, but I can’t understand how CommandTestFactory would want me to pass option flags to my command. Imagine the following command class (simplified for ease of reading):

@Command({
  name: 'generate',
  description: 'Generate a new project',
})
export class GenerateCommand {
  public async run(input: string[], options: GenerateCommandOptions): Promise<void> {
    const { name, description, ...requirements } = await this.promptForMissingOptions('generate', options);

    await this.generate(new ProjectSchema(name, description, requirements));
  }

  @Option({ flags: '-o, --open-api' })
  public openApi(openApi: string): boolean {
    return openApi === 'true';
  }

  @Option({  flags: '-r, --rabbit' })
  public rabbitMqClient(rabbitMqClient: string): boolean {
    return rabbitMqClient === 'true';
  }

  @Option({ flags: '-g, --originator' })
  public originator(originator: string): boolean {
    return originator === 'true';
  }
}

If I test this command with the following:

it(`creates a schema and generates the project`, async () => {
  await CommandTestFactory.run(commandInstance, [
    'generate',
    '--name',
    'Foo',
    '--description',
    'Desc',
    '--open-api',
    'false',
    '--rabbit',
    'true',
    '--originator',
    'false',
  ]);
});

It does not work, my command’s .run() method will receive ['false', 'false', 'false'] as its input parameter, like they are regular arguments, not options and the rabbit option will still be false even though I would assume it should be true with the above.

I’m really not sure what’s going on here, any help would be greatly appreciated 😃

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:6 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
gCardinalcommented, Aug 11, 2021

For anyone stumbling upon this question and struggling with boolean flags like I was, here’s a small example of what works (including negative flags). No need for [] or <>:

@QuestionSet({ name: 'nestjs_questions' })
export class NestjsQuestions extends GenerateQuestions {
  public static NAME = 'nestjs_questions';

  @Question({
    name: 'rabbitMqClient',
    message: 'Do you require a RabbitMQ client to be configured?',
    type: 'confirm',
  })
  public rabbitMqClient(rabbitMqClient: string): boolean {
    return rabbitMqClient === 'true';
  }
}

@Command({
  name: 'nestjs',
  description: 'Generate a new NestJS project',
})
export class GenerateNestjsCommand implements CommandRunner {
  public constructor(
    private readonly inquirer: InquirerService,
  ) {}

  public async run(
    input: string[],
    options: GenerateNestjsCommandOptions,
  ): Promise<void> {
    const responses =
      await this.inquirer.ask<GenerateNestjsCommandOptions>(
        NestjsQuestions.NAME,
        options,
      );
    
    // Stuff
  }

  @Option({
    flags: '-r, --rabbit-mq-client',
    description: "setup Station Casinos' RabbitMQ client module",
  })
  public rabbitMqClient(): boolean {
    return true;
  }

  @Option({
    flags: '--no-rabbit-mq-client',
    description: "do not configure Station Casinos' RabbitMQ client module",
  })
  public noRabbitMqClient(): boolean {
    return false;
  }
}

With this, your command will prompt the user if no flag is given, but will not prompt if -r, --rabbit-mq-client or --no-rabbit-mq-client is given, with the first two flags setting rabbitMqClient to true and the last one to false.

Hope it can help someone 😃

0reactions
jmcdo29commented, Aug 11, 2021

I don’t see this explicitly stated in commander’s docs, but I can see that any option that is hyphenated, that does not have no- (i.e. --no-rabbit) does have either [] or <> wrapping around the option name. I’ll take a look at the PR and see what you’ve got in addition. Thanks for opening one up!

Read more comments on GitHub >

github_iconTop Results From Across the Web

nest-commander/README.md at main - GitHub
The run command is where you can kick all of your logic off from, it will take in whatever parameters did not match...
Read more >
How to unset flags Visited on command line in GoLang for Tests
I want this test to have no command line flags passed. The flags. Visit function should not be able to get true for...
Read more >
Nest Commander | NestJS - A progressive Node.js framework
The run command is where you can kick all of your logic off from, it will take in whatever parameters did not match...
Read more >
A Module for using NestJS to Build Up CLI Apps - Morioh
The run command is where you can kick all of your logic off from, it will take in whatever parameters did not match...
Read more >
Nest-commander NPM
Options, CommandOptions, false, Extra options to pass on down to commander ... If you don't need any filtering done, simply return the value...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found