Introduction to Cobra
In this lesson, you’ll learn about Cobra, which is the de-facto gold standard for Go command-line programs.
We'll cover the following...
Cobra is both a library for processing command-line arguments and organizing your commands and an application that can generate a structured skeleton for your program. It is used by a vast number of the most prominent Go command-line tools out there like kubectl (the Kubernetes CLI) and docker (the Docker CLI).
The Cobra commander
Cobra is named after the comics villain Cobra commander who is the supreme leader of the Cobra terrorist organization in the G.I Joe comics universe. That’s a great inspiration for a command-line program tool. It also has a nice logo!
The author of Cobra, Steve Francia, works for Google and developed a few other notable Go libraries and programs, in particular Hugo and Viper. Spoiler alert: we will incorporate Viper later into multi-git.
Okay. Let’s see what Cobra is all about.
Why Cobra?
Cobra is mature, battle-tested, and provides a lot of functionality. It is likely familiar to other Go developers, so if you collaborate with other people, they will feel right at home if your command-line program uses Cobra. Here are just some of the features Cobra brings to the table:
- Commands and nested subcommands
- POSIX-compliant flags (including short and long versions)
- Global, local, and cascading flags
- Easy generation of application skeleton
- Intelligent suggestions for typos
- Optional tight integration with viper for 12-factor apps
There are many more features. You may or may not need all these ...