Creating A CLI in Go Using Cobra - JetBrains Guide
Creating A CLI in Go Using Cobra - JetBrains Guide
Go Go Download
Blog
Docs
Creating a CLI in Go
Command
Line
Interfaces?
Creating a
CLI in Go
Using Cobra
Using Cobra
Dive into Go CLI Development - A Hands-on
Error Guide with Cobra
Handling in
Cobra Israel Ulelu (Community)
Building a 2024-05-24
Binary
Executable go
Conclusion
Now that you know the basics of command line interfaces and
their underlying principles, let's look at the process of building one
using the Go programming language and Cobra. Cobra is a simple
and powerful open source library released under the Apache
License 2.0 for creating modern command line applications in Go.
With a broad userbase, including notable projects like Kubernetes,
Cosmos SDK, and GitHub CLI, Cobra is highly regarded for its
extensive capabilities. Among its notable features are intelligent
suggestions, command aliases, and support for nested commands,
making it an ideal choice for crafting sophisticated and user-
friendly CLI tools in the Go ecosystem.
Creating a Go Application
Launch your GoLand IDE and initiate a new Go project. Specify
your desired project name and location on your device:
Create a main.go file within the project directory. This file will act
as the central hub for your project's codebase:
Creating a Command
First, create a folder called cmd in the same directory as your
main.go file and then create a root.go file in the newly created
cmd folder.
The root.go file uses the Cobra package to create the root
command and an accessor function to execute the command.
Paste in the code below:
package cmd
import (
"fmt"
"github.com/spf13/cobra"
"os"
)
},
}
func Execute() {
if err := rootCmd.Execute(); err != nil {
fmt.Fprintf(os.Stderr, "Oops. An error while executing Z
os.Exit(1)
}
}
go get -u github.com/spf13/cobra@latest
package main
import "Zero/cmd"
func main() {
cmd.Execute()
}
At this stage, you can test out your command line application by
running go run main.go --help in your GoLand terminal. You
should get the following help result:
Adding Subcommands
Create a zero.go file in the cmd directory to hold the logic for
your mathematical operations. Save it with the following code:
package cmd
import (
"fmt"
"strconv"
)
First, for the addition operation, the add.go file should contain the
following lines of code:
package cmd
import (
"fmt"
"github.com/spf13/cobra"
)
func init() {
rootCmd.AddCommand(addCmd)
}
package cmd
import (
"fmt"
"github.com/spf13/cobra"
)
func init() {
rootCmd.AddCommand(subtractCmd)
}
The subtract command is also added as a subcommand to
rootCmd in the init() function.
You can now run basic addition and subtraction operations using
Zero, your command line application. Launch your GoLand terminal
and execute the following commands:
Then, create a multiply.go file in the cmd folder. The file will
contain the implementation of the multiplication command and
init() function, as shown below:
package cmd
import (
"fmt"
"github.com/spf13/cobra"
)