Member-only story
In my previous article, you created a NestJS CLI application. It was a very basic starting point that printed a cow saying a static message in the console.
But what if your users demand the ability to display custom messages? That’s what you’re going to add in this article. The new and improved application will display the same message by default, or any string that you pass in with the --message
argument.
Adding a Message Option
The easiest way to add an option is to add a parser function for a parameter and add the @Option
decorator to it.
In the cow-say.command.ts
file, add the following method to your CowSayCommand
class.
@Option({
flags: '--message <message>',
description: 'The message we want to see the cow say',
})
parseMessage(value: string) {
return value;
}
Decorating It
If you’ve used the commander library before, this should look familiar. The <message>
tells commander that if the --message
option is provided, the user doesn't need to provide a parameter after it. If the value was required it would be [message]
instead.
The description
parameter is used when the user displays help for a specific command. The description will be displayed in a column to the…