Then create the options object containing all your command line flags: const options = yargs. After installing it, include the module in your index.js : ~$const yargs = require("yargs") ģ. yargs will simplify our process of parsing arguments and help us organize command-line flags.Ģ. In our CLI, we will be receiving the language name and the sentence to be translated as arguments and then we will parse it.Īlthough Node.js offers built-in functionality for handling command-line arguments, we are going to use an npm package called yargs □☠ which is specifically made for building CLI s. The most basic task that any CLI does is handling command-line arguments. Our basic CLI is ready and now we move to add further functionality. Test the CLI by typing the specified keyword in the terminal. The -g the flag tells npm to install the package globally on the system. Navigate to the root directory of the project and then run ~$npm install -g. People may call our CLI from anywhere in the system so let’s install it globally. The name defined here is not permanent and can be changed whenever you like. Be free to name it whatever you like, although I would suggest keeping the name short and semantic so that it’s quick to type and easy to remember. This is the keyword that people will type in the terminal for using your CLI. The key, mycli, is the keyword for calling the CLI. ![]() The addition should look something like this:
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |