Starting a CLI project

Patrick Rachford
2 min readAug 20, 2023

--

Learn to get started with the Cobra CLI

When trying to install the Cobra CLI project to create a command line application, I ran into a few issues.
For starters, running this command :

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

Resulted in the following error:

go: can only use path@version syntax with ‘go get’ and ‘go install’ in module-aware mode

This made me aware, I was not using the correct package I wanted.
What I wanted was the cobra-cli.

With this, you can generate your own CLI.

1. Install the cobra-cli package:

This step will install the Cobra command line interface, which is a library for creating powerful modern CLI applications.

go install github.com/spf13/cobra-cli@latest

2. Initialize a new Go module:

If you’re starting a new project, you need to set it up as a Go module. Here, we’re naming it cobra-cli.

go mod init github.com/${GITHUB_USERNAME}/${PROJECT_NAME}

3. Initialize a new Cobra application:

Using the cobra-cli tool you just installed, this step will scaffold a new Cobra application for you.

cobra-cli init

4. Compile and run a Go program:

This step compiles the code to ensure everything is set up correctly and runs the command.

go run main.go

Results: You should see the following output.

A longer description that spans multiple lines and likely contains
examples and usage of using your application. For example:
Cobra is a CLI library for Go that empowers applications.
This application is a tool to generate the needed files
to quickly create a Cobra application.

That is it. Now you can add your custom logic to start creating a CLI backed by Cobra.

--

--