Skip to content

Commit 90e2985

Browse files
Merge pull request #2426 from goznauk/nest-commander-patch
docs(nest-commander): Update outdated docs to meet latest
2 parents 0dc3c67 + 9f23192 commit 90e2985

File tree

1 file changed

+6
-4
lines changed

1 file changed

+6
-4
lines changed

content/recipes/nest-commander.md

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@ $ npm i nest-commander
1414

1515
#### A Command file
1616

17-
`nest-commander` makes it easy to write new command-line applications with [decorators](https://www.typescriptlang.org/docs/handbook/decorators.html) via the `@Command()` decorator for classes and the `@Option()` decorator for methods of that class. Every command file should implement the `CommandRunner` interface and should be decorated with a `@Command()` decorator.
17+
`nest-commander` makes it easy to write new command-line applications with [decorators](https://www.typescriptlang.org/docs/handbook/decorators.html) via the `@Command()` decorator for classes and the `@Option()` decorator for methods of that class. Every command file should implement the `CommandRunner` abstract class and should be decorated with a `@Command()` decorator.
1818

19-
Every command is seen as an `@Injectable()` by Nest, so your normal Dependency Injection still works as you would expect it to. The only thing to take note of is the interface `CommandRunner`, which should be implemented by each command. The `CommandRunner` interface ensures that all commands have a `run` method that returns a `Promise<void>` and takes in the parameters `string[], Record<string, any>`. The `run` command is where you can kick all of your logic off from, it will take in whatever parameters did not match option flags and pass them in as an array, just in case you are really meaning to work with multiple parameters. As for the options, the `Record<string, any>`, the names of these properties match the `name` property given to the `@Option()` decorators, while their value matches the return of the option handler. If you'd like better type safety, you are welcome to create an interface for your options as well.
19+
Every command is seen as an `@Injectable()` by Nest, so your normal Dependency Injection still works as you would expect it to. The only thing to take note of is the abstract class `CommandRunner`, which should be implemented by each command. The `CommandRunner` abstract class ensures that all commands have a `run` method that returns a `Promise<void>` and takes in the parameters `string[], Record<string, any>`. The `run` command is where you can kick all of your logic off from, it will take in whatever parameters did not match option flags and pass them in as an array, just in case you are really meaning to work with multiple parameters. As for the options, the `Record<string, any>`, the names of these properties match the `name` property given to the `@Option()` decorators, while their value matches the return of the option handler. If you'd like better type safety, you are welcome to create an interface for your options as well.
2020

2121
#### Running the Command
2222

@@ -71,8 +71,10 @@ interface BasicCommandOptions {
7171
}
7272

7373
@Command({ name: 'basic', description: 'A parameter parse' })
74-
export class BasicCommand implements CommandRunner {
75-
constructor(private readonly logService: LogService) {}
74+
export class BasicCommand extends CommandRunner {
75+
constructor(private readonly logService: LogService) {
76+
super()
77+
}
7678

7779
async run(
7880
passedParam: string[],

0 commit comments

Comments
 (0)