Hello everyone. I wonder if anyone here has been using command-line applications (CLI)? If so, why did you choose it over a graphical user interface (GUI)? If I remember correctly, I have written a few articles about the process of creating some applications for myself. Honestly, for me, there are many cases where CLI proves to be much more useful.
Recalling my early days of switching from Windows to Linux, specifically Ubuntu. I have cursed countless times about this damn operating system. The interface is ugly, hard to use, and everything requires typing commands; how can anyone remember all those lines? It’s not like Windows is good. The interface is intuitive and easy to use. You just need to look to know where to click, everything is visible.
True to the saying, “what you hate, fate gives you”. In the end, I became “addicted” to this command-line typing without even noticing. There must be a reason for this addiction; it helps me approach problems faster. Instead of moving the mouse around, I can just type, which is much quicker. Not to mention that many software applications are provided in CLI form, which in turn leads to the creation of management tools, allowing us to easily install our favorite applications with just a single command.
That was also the inspiration for me to tackle problems using CLI. Most of it is to help manage my blog. In the early days of exploring, there were so many new things that I didn’t know. It took a lot of time to learn while doing. Understanding that psychology, today I will compile some libraries that I know from my previous learning process. These libraries not only help you create quality applications but also make them more appealing to users.
Oh! All of these are Node.js libraries. If you are using other platforms like Go, Rust… I’m sure there are similar libraries. You just need to search based on the ideas of the libraries below.
Let’s get started!
Framework
First, we need to talk about the framework; this is where you structure your CLI application. Imagine the framework helps shape, structure the code, and features to fit the nature of the CLI.
Notice that we often see command-line applications using a format like this:
$ mycli image --resize 512:512 /path/to/image
With mycli
being the application name, image
is almost a function, and the flag --resize
is typically used to specify additional attributes. Finally, /path/to/image
points to the data that needs to be processed.
If you are familiar with Node.js, you will see that CLI is quite similar to the node
command, while the following parameters can easily be captured through the process.argv
variable.
$ node index.js image --resize 512:512 /path/to/image
// console.log(process.argv); ['node', 'index.js', 'image', '--resize', '512:512', '/path/to/image']
In other words, just parsing process.argv
is sufficient to classify and call the corresponding functions with the used parameters. At that point, the application is no different from a CLI.
yargs and commander are two foundational libraries to help us parse parameters as mentioned above. They provide very basic functions to serve as a foundation for other libraries.
oclif is one example. This tool helps us create powerful CLI applications by optimizing the workflow. oclif defines everything, from the directory structure to the help commands generated for new features. All you need to do is focus on writing the logic. After a “build” step, oclif produces a complete CLI application, including usage instructions without requiring you to do many additional steps.
Configuration File
In an application, apart from the default configurations or configurations hardcoded in the code, sometimes user configuration is still needed.
For example, you provide the user with the ability to set the input/output paths for files after processing. This option needs to be saved somewhere for future use. The easiest way is to create a text file and store all the information in it. Or more professionally, you can use the cosmiconfig library.
cosmiconfig is a library that automatically fetches configuration files into your application. cosmiconfig supports many formats such as .json, .yaml, .yml… After loading, cosmiconfig puts all the values into a variable that you can use in your application.
Input
Input is an essential part of CLI applications; besides receiving user data through flags like --resize
, there are many other optimized ways.
inquirer is a library that provides many ways to receive user data. For example, displaying a text input box, yes/no questions, or select options… each time a certain command is typed.
Output
Have you ever used a CLI application that shows beautiful “loading” effects during interaction? Or lines of text with various colors to highlight important information? There are many libraries to help us achieve this.
ora is a library that creates beautiful “loading” effects. These effects are often used to signal to users that processing is underway and they need to wait. ora provides many different “spinning” shapes. Additionally, you can combine it with text to create continuous messages on the screen, indicating the progress of the ongoing process.
progress helps you create “downloading” effects using a combination of ASCII characters.
Sometimes you will need to draw a table to display information for output, cli-table3 is very suitable for this. Or if you simply want to enclose content in “boxes”, boxen is a perfect choice.
Additionally, you can combine with chalk to style the text. This is a great library to change colors, add effects like “bold”, “dim”, “italic”… to highlight content. Or if you prefer a “rainbow” effect, gradient-string is unbeatable.
Utils
In addition to the main libraries mentioned above, there are still many other utility libraries that CLI can use.
open helps us open something like images, web addresses, or even applications. This library proves useful when we need the assistance of an external application that the CLI cannot handle.
clipboardy helps read data from the clipboard or issue commands to copy anything you want.
shelljs is used to run any other command-line programs through the shell.
And many other libraries that I cannot list all here.
Conclusion
Above is a compilation of some libraries that assist in creating a CLI application for Node.js. As a CLI enthusiast, I have researched and applied quite a few useful libraries in my applications. What about you? Are you using any additional tools? Please leave a comment for me and everyone to know. Thank you!
Source link
lol