Adding Commands With the Cobra Generator
In this lesson, you'll learn how to use the Cobra generator to generate an application skeleton from scratch.
We'll cover the following...
We will build our simple calculator on top of the skeleton. Next, we will add a couple more options like pre-run and post-run hooks that we skipped over in the previous lesson. Let’s start by understanding what the Cobra generator is, how it works, and when you should use it.
Adding commands to the application
The cobra add
will create new commands for us, and even hook it up to a parent command. By default, the parent is the root command. Our calculator application needs two commands add
and subtract
.
If you want to follow along, there is a terminal window at the bottom of the lesson that is already set up with the Cobra application skeleton. Feel free to type in the commands and check out the results for yourself.
Let’s create the calculator’s add
command using Cobra’s add
command. Don’t be confused. Our calculator command to add two numbers is ...