Introduction:
Creating a CLI using Cobra in Golang is a great choice, and nesting commands can help organize and structure your CLI effectively. Below, I'll guide you through the process of creating a nested CLI using Cobra.
What is Cobra?
Cobra is a robust and flexible Golang library for building CLI applications. It provides an elegant and easy-to-use interface to define commands, flags, and arguments, making it a popular choice among Golang developers for creating CLI applications.
Prerequisites:
Before we begin, ensure you have Go (Golang) installed and have set up your workspace properly. You can install Cobra by executing the following command:
go get -u github.com/spf13/cobra@latest
Getting Started:
Let's start by creating a new Golang project in your desired workspace. Once your project is set up, we'll initialize a Cobra application by running the following command:
cobra init [your-cli-app-name]
This will create a basic structure for your CLI application, including the necessary files and folders.
Defining the Root Command:
The root command is the entry point of your CLI application. It is the command executed when users run your application without any subcommands. Open the cmd/root.go
file, and let's define the root command using Cobra:
// cmd/root.go
package cmd
import (
"fmt"
"github.com/spf13/cobra"
)
var rootCmd = &cobra.Command{
Use: "[your-cli-app-name]",
Short: "A brief description of your CLI application",
Long: `A longer description that explains your CLI application
in detail. For example, you can mention the various commands
available and what they do.`,
Run: func(cmd *cobra.Command, args []string) {
// This function will be executed when the root command is called
fmt.Println("Welcome to [your-cli-app-name]! Use --help for usage.")
},
}
func Execute() {
if err := rootCmd.Execute(); err != nil {
fmt.Println(err)
os.Exit(1)
}
}
You can try to run it:
$ go run main.go
> Welcome to [your-cli-app-name]! Use --help for usage.
Adding a Nested Command:
Next, let's add a nested command to our CLI application. Nested commands are subcommands that provide additional functionality under the main command. For example, let's create a subcommand called "subcommand" for our application. Open the cmd/subcommand.go
file and define the nested command:
// cmd/subcommand.go
package cmd
import (
"fmt"
"github.com/spf13/cobra"
)
var subCmd = &cobra.Command{
Use: "subcommand",
Short: "A brief description of the subcommand",
Long: `A longer description that explains the subcommand
in detail. For example, you can mention how it complements
the main command and what options it supports.`,
Run: func(cmd *cobra.Command, args []string) {
// This function will be executed when the "subcommand" is called
fmt.Println("Running the subcommand!")
},
}
func init() {
rootCmd.AddCommand(subCmd)
}
Lets try it again:
$ go run main.go subcommand
> Running the subcommand!
Conclusion:
Congratulations! 🎉 You have successfully created a CLI application using Cobra with a nested command. You can continue to expand your application by adding more subcommands, flags, and arguments to meet your specific needs.
In this blog post, we've only scratched the surface of what you can achieve with Cobra. It's a powerful library that allows you to create professional-grade CLIs with ease. Experiment with various options, customize your commands, and explore more features provided by Cobra.
Happy coding! 👨💻
Footnote:
Full code is available on github 🤝
Top comments (3)
Nice artcile.
I love using Cobra, one thing you did was manually add sub command. You can get the optional cobra-cli utility from the same guys, very useful cmdline tool that avoids manual work and automates a lot of the basic tasks like adding subcmds:
That will prep the update and add in a dummy sub-cmd boiler plate ready to roll.
Another thing to note, when you "cobra-cli init" in an existing project Cobra will wipe your main func if you have one so make sure all your code is elsewhere if you do init on an existing project.
Thank you for your feedback on the article!
Yes, that's right.
cobra-cli
is really useful as it automates adding subcommands, saving time and effort.Maybe I will add to the article later.
hi thank you,
but you forgot to call the "os" package.