Search⌘ K

Comparing Cobra to Alternatives

Understand the strengths of Cobra for building structured command-line programs in Go by comparing it with Go's built-in flag package and other third-party parsing libraries. Learn how to install and use Cobra, examine its application generator, and evaluate alternatives to make informed choices for your CLI projects.

We will compare Cobra with some alternatives ways to parse command-line programs including the built-in ways Go provides as well as other third-party command-line parsing solutions.

Installing Cobra

First, we need to install Cobra. The official instructions recommend using the following command:

go get -u github.com/spf13/cobra/cobra

Unfortunately, this hangs indefinitely for me using Go 1.13.1. However, this command works:

go get -u github.com/spf13/cobra/cobra/...

Try the above commands in the following terminal.

Terminal 1
Terminal
Loading...

Cobra as a library

Once Cobra is installed, we can import it as a library into our application:

import "github.com/spf13/cobra"

At this point, we can start using ...