Using CLI Applications to Increase Efficiency in Work
Tags: cli, story
Published: 2024-03-13 16:07:33

## The Problem

As a programmer, I believe that everyone has thought about leveraging lines of code to automate tasks. For example, a JavaScript code snippet written by a programmer to calculate the total amount of money spent on an e-commerce platform, instead of manually adding up each order in a never-ending list.  

Automation brings many benefits, the most obvious of which are time savings and reduced errors in repetitive processes. Moreover, if the code snippet I wrote can be shared with others, it's truly a win-win situation.  

I also create such code snippets, but instead of calculating order values, they focus on solving common work-related issues. For instance, a code snippet that synchronizes data between two production servers and the development environment of this blog. Because sometimes it's necessary to test new features on real data for safety before deploying them to the "production" environment.  

I usually store these snippets in a convenient location, such as the note-taking application of my operating system, so that they can be synchronized across all my workspaces. If I encounter a problem in the future, I can simply open the application, copy the code, and paste it to run. It's brilliant!  

That's just a simple example of how to automate repetitive tasks. A few weeks ago, when I was focused on optimizing images for my blog, the problem became more complicated. Copying and pasting, then modifying the content for each task took more time. I realized that such code snippets are only suitable for low-frequency tasks, such as once a day.  

At this point, you may already be imagining how to solve the problem for yourself. I know there are many ways to do it, but personally, I chose to create a CLI application to handle everything.  

## What is a CLI?

A Command-Line Interface (CLI) is a software mechanism that allows you to interact with your operating system through the keyboard. It is the opposite of a Graphical User Interface (GUI).  

The strength of GUI lies in its intuitive navigation, such as clicking icons and images to use software applications. However, GUI is not efficient for system administration tasks, especially in virtual or remote environments.  

With CLI, you can enter text commands to configure, navigate, or run programs on any server or computer system. CLI focuses on functionality and speed of use.  

CLI is a broad topic, but in this article, I only want to discuss the application of CLI in software applications. In simple terms, CLI applications are command-line applications that we type daily in the Terminal. Commands like `cd`, `ls`, `pwd`, etc. can also be considered as software applications that help us perform specific functions.  

## When to Create a CLI Application?

Let's go back to the image optimization problem that I wrote an article about, [Optimizing Image Display with Blur Placeholder and Lazyload](https://2coffee.dev/en/articles/optimize-image-display-with-blur-placeholder-and-lazyload). There are many steps involved in creating an image suitable for the blog post, but in general, there are two main actions:  

First, reformat the image to `webp`, adjust the image quality, and crop it to the desired size.  

Second, upload all the newly created images to R2 and retrieve all the links to them.  

If we apply the "copy-paste" method as mentioned earlier, it becomes cumbersome. Therefore, creating a CLI application in this case is reasonable to centralize management and save typing time.  

Imagine, my application is called `img`, and when I want to create all the optimized images, I just need to run:  

```bash
$ img all path/to/file
```

Where `path/to/file` is the path to the original image. After running, the newly created images will be saved in `path/to/new/file`. At this point, I add another command `upload` for the action of uploading images to R2:  

```bash
$ img upload path/to/new/file
```

Perfect, everything works. But it's not over yet. One advantage of CLI is that it supports parameters (args) and flags. Based on the flags, we can create more options for richer processing.  

For example, if I want to upload the images to R2 immediately after they are created instead of running an additional `upload` command, I can add code for the `--upload` flag or its shorthand `-u`:  

```bash
$ img all path/to/file --upload
# or
$ img all path/to/file -u
```

Of course, the above is just a hypothesis. A CLI application can do much more depending on each person's approach. So, feel free to customize commands and flags according to your preferences, but make sure they are clear, easy to remember, and easy to use.  

## How to Create a Simple CLI Application

CLI applications are not limited to any specific programming language. If you are using Golang, search for libraries that support creating CLI applications in Go. Similarly, in Node.js, oclif can help with this. Besides oclif, there are many other libraries that can help you create CLI applications. Take the time to explore them and choose one with a friendly and easy-to-use syntax. Find more libraries at [Command-line utilities | awesome-nodejs Public | Github](https://github.com/sindresorhus/awesome-nodejs?tab=readme-ov-file#command-line-utilities).  

oclif is relatively simple to get started with. Just follow the instructions in [Introduction | oclif docs](https://oclif.io/docs/introduction/), and you will immediately have your own "command."

```bash
$ npx oclif generate mynewcli
? npm package name (mynewcli): mynewcli
$ cd mynewcli
$ ./bin/dev.js hello world
hello world! (./src/commands/hello/world.ts)
```

oclif supports automatic generation of new commands (CLI Generator), flag/argument parsing, testing, autocomplete, and many other features. For more information, refer to [Features | oclif docs](https://oclif.io/docs/features).  

## Conclusion

CLI is a software mechanism that allows you to interact with the operating system. While CLI has a wide scope, in this article, we focused on using CLI applications to create software for our own use.  

Previously, I used to store commonly used code snippets and simply "copy-paste" them when needed. Creating a CLI application simplifies this process, saving time and effort.  

[oclif](https://github.com/oclif/oclif) is a library that helps create CLI applications using Node.js. If you are using a different programming language, search for a suitable library.