Working with Cobra - Flags
This lesson wraps up the topic of working with Cobra as a library.
We'll cover the following...
In this lesson, you will see how flags complement arguments on the command line, create the main.go file of our calculator demo application, and then take a little quiz.
Understanding flags
Commands and arguments are very powerful, but good command-line interfaces need flags too. Flags modify the way commands operate on their arguments.
There are two types of flags: persistent and non-persistent. Persistent flags are flags that are available to a command and all its sub-commands. In particular it is common to define persistent flags on the root command that will be available for all commands. For example, --verbose
or --help
are good examples for global flags that can apply to any command.
Local flags, on the other hand, apply only to the command they are attached to. Let’s add flags to our commands. We can do it in the init()
function of each command.
Since our program deals with adding and subtracting integers, there’s the possibility of overflowing or ...