Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding 'choices' within the option decorator #327

Closed
jmcdo29 opened this issue Mar 18, 2022 Discussed in #326 · 3 comments · Fixed by #363
Closed

Adding 'choices' within the option decorator #327

jmcdo29 opened this issue Mar 18, 2022 Discussed in #326 · 3 comments · Fixed by #363

Comments

@jmcdo29
Copy link
Owner

jmcdo29 commented Mar 18, 2022

Discussed in #326

Originally posted by BenSawary March 18, 2022
I'd like to define a valid list of a commands option argument. Since commander 7, (tj/commander.js#518) it is possible to create an option with choices. Is it possible to add this functionality also in the nest-commander option decorator?

@option({
flags: '--option [option]',
choices: ['a', 'b', 'c'],
defaultValue: 'a'
})
setOption(option: string) {}

@BenSawary
Copy link

Hi Jay, an additional configuration could be dynamic choices. Decorators are used in a static manner, if we want to add the choices dynamically, then we could provide this over a function.

export interface OptionMetadata {
flags: string;
description?: string;
choices?: string[] | () => string[];
defaultValue?: string | boolean;
required?: boolean;
}

An alternative of that approach could be a second Decorator. We could use it in the same way as in the Inquirer API:

@option({
flags: '--option [option]',
defaultValue: 'a'
})
setOption(option: string) {}

@OptionChoicesFor({name: option})
getOptionChoices(): string[] {}

@jmcdo29
Copy link
Owner Author

jmcdo29 commented Mar 22, 2022

I really like the @OptionFor() approach, so I'll probably go with that over using a function inside the decorator, thanks for calling this out as an option.

jmcdo29 added a commit that referenced this issue Apr 19, 2022
Option choices are now supported either as a static string array
or via the `@OptionChoicesFor()` decorator on a class method.
This decorator method approach allows for using a class's injected
providers to give the chocies, which means they could come from a
database or a config file somewhere if the CLI is set up to handle
such a case

closes #327
jmcdo29 added a commit that referenced this issue Apr 19, 2022
Option choices are now supported either as a static string array
or via the `@OptionChoicesFor()` decorator on a class method.
This decorator method approach allows for using a class's injected
providers to give the chocies, which means they could come from a
database or a config file somewhere if the CLI is set up to handle
such a case

closes #327
@jmcdo29
Copy link
Owner Author

jmcdo29 commented Apr 19, 2022

Added support in 2.5.0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants