API with NestJS #144. Creating CLI applications with the Nest Commander

NestJS

This entry is part 144 of 158 in the API with NestJS

Command Line Applications are very useful for developers. We can interact with them by executing specific commands in the terminal, giving us much control and flexibility. You probably already interact with various CLI applications, such as Git or NPM.

Interestingly, we can also use NestJS to create a Command Line Interface (CLI) application. In this article, we learn what we can do with CLI apps and how to implement one with NestJS and the Nest Commander.

Command Line Interface applications

A straightforward example of a CLI tool is cp, which is used to copy files.

Let’s break it down:

  •  is the command name
  • are arguments. The first one is the file name we want to copy, and the second is the destination.

Many options are configurable through options.

In the above example, is an option. It means we want to copy a folder with all its contents recursively.

In some cases, options can have their own arguments as well. For example, the following command makes a GET request and displays the result in the terminal:

However, we can use the option to store the result of the request in the file. If we do that, we need to provide an additional argument with the name of the output file.

It is worth noticing that options are prefixed with a single dash if they use a shorthand or an abbreviated version of the option. On the other hand, options prefixed with two dashes are usually followed by a whole word or multiple words. For example, we can use instead of when using .

Creating CLI applications with NestJS

To create a CLI application with NestJS, let’s first create a new NestJS project.

The is a CLI tool created by the NestJS team.

We also need to install the Nest Commander library.

First, we need to adjust our method in the file.

main.ts

We can now create our first simple command. To do that, we need to create a class that extends the and has the method.

get-date.command.ts

We mark the method with because it needs to return a promise.

Above, we create the command that prints the current date.

We must also add our class to the array in our module.

app.module.ts

Running our command

To test our command, we must build our NestJS application and run it with Node.js. We can simplify this process by creating an appropriate script in our file.

package.json

We can now run the script and provide the command name we want to run.

Using arguments

Let’s change our code so that the command accepts a date it should format.

get-date.command.ts

Any argument we pass to our command is available through the first argument in the method.

There is a catch, though. Since we use to run our script, we must use the separator before providing the arguments.

 

Using options

Let’s handle an option that signifies that we want to include the time in the output. To do that, we need to use the decorator.

get-date.command.ts

We can provide additional arguments related to our option. For example, let’s allow the users to provide the timezone offset they want to use with the date.

All arguments we pass to our commands are treated as strings. The is a method decorator, and we can use that to parse the argument from a string to a number.

get-date.command.ts

Thanks to that, we can provide the number of minutes we want to add or subtract from the provided date.

Summary

In this article, we’ve gone through how command-line interface applications work and how to control them with arguments and options. We also learned how to implement a CLI application using NestJS and the Nest Commander. With the right approach, command-line interface applications can be straightforward, practical, and valuable tools for developers.

Series Navigation<< API with NestJS #143. Optimizing queries with views using PostgreSQL and KyselyAPI with NestJS #145. Securing applications with Helmet >>
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments