I will first take you to the reason and urge I have for building CLI tools and then make your hands dirty by building a CLI-based calculator with Cobra! In case you are well motivated to build CLIs, I would suggest you directly jump to the code valley.
The urge to build CLIs 👨💻
I find it very difficult to install or run an application while switching between various GUI components in the OS, be it Windows/ Mac or Unix. The commands always follow a particular logic, be it any OS for example using sudo
gives us admin privileges, using -v gives us the version of the application, and so on. Moreover, GUI does not have the same level of functionality and granular control as a command-line interface. So the CLI provides greater flexibility of use. A GUI needs to use additional system resources to load the graphical part thus it is going to be slower than using the command line. With a CLI, users have all the control over the file system and operating system, and the tasks become simple. You can create a script that contains a few lines of command and it will do the work for you. Although learning bash has a steep curve because of its ugly syntax, the only datatype is a string but the automation it provides is worth it.
Relating Web with CLI tools 🕸
Having experience in building backend applications for 2 years, I feel the way CLI works is very similar to that of building full-stack web applications. For instance, a simple CLI tool to generate Temperature at a location requires just passing the location as a prop and then doing API calls to fetch the data and display it on the CLI.
Building Simple CLIs 🚀
Initially, I started creating simple CLI tools such as joke generator, fuzzy calculator, code compiler, reminder CLI tools, and some more. The one CLI tool which particularly made me learn a lot of tools was while developing a network CLI tool for Domain migrations within various hosting providers. In the particular CLI, I used Urfave CLI to get started with the CLI structure which made naming labels easier and generating new commands simpler. Urfave also has various classes within it which make overriding functions easier for example creating CLI flags or creating commands. The network CLI had basic functionalities such as when hostname is passed as a parameter it returns name servers, IP address, and CName for that particular host when passed with a particular hostname. A simple but according to me effective CLI tool. Comparing Urfave and Cobra, Cobra uses more template code which makes execution of the programs slower. Although a novice in building CLI applications, I am highly enthusiastic about learning and exploring more and more in this field.
While going through the web I created a repo to learn Go lang and build CLIs I suggest you look into the Code Valley. If you want me to write a blog on how to build a CLI tool with proper explanation let me know in the comments 🐣