DEV Community

Cover image for Urge to build CLIs : Beginners Eye🚀
Aniket Pal
Aniket Pal

Posted on • Edited on

Urge to build CLIs : Beginners Eye🚀

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 🐣

Top comments (7)

Collapse
 
andypiper profile image
Andy Piper

I have not come across Urfave or Cobra before. I guess these are both Go frameworks?

Collapse
 
thomasjunkos profile image
Thomas Junkツ
Collapse
 
aniket762 profile image
Aniket Pal
Collapse
 
schollii profile image
schollii

I have built some fairly large CLIs using python click package. Now I'm transitioning to Go, and have tried cobra. The API is clunky at best: it does not allow you to define callbacks for processing arguments and flags, which forces you to put such code in the Run function of the command, and it's kind of a mess.

Whereas urfave/cli/v2 supports Action callback. There I can put any validation as well as converting the value to something different (eg a value of a flag should affect another value of another setting, cet). I also prefer that flags are defined in the command object, as in urfave/cli, whereas cobra forces you to define flags in an init() function.

It looks like there may be a couple of specialized, advanced use cases supported by cobra but not urfave/cli. However I'm betting I will never need these more than I need the reability of urfave/cli.

Collapse
 
chetan_kumar_43da85c554e6 profile image
Chetan Kumar

any idea how can I traverse through the flags give in cli in urfave.

for eg: ./calculator add --num1 2 --num1 3

I want to know what all the flags fed to the 'add' command.

Collapse
 
marcello_h profile image
Marcelloh

framework is a far to big word for a configuration diggester

Collapse
 
aniket762 profile image
Aniket Pal

lol