spf13/cobra

CLI
import "github.com/spf13/cobra"

A library for creating powerful modern CLI applications with subcommands, flags, and shell completion.

Example

package main

import (
    "fmt"
    "github.com/spf13/cobra"
)

var rootCmd = &cobra.Command{
    Use:   "app",
    Short: "My CLI app",
    Run: func(cmd *cobra.Command, args []string) {
        fmt.Println("Hello from CLI!")
    },
}

func main() { rootCmd.Execute() }

Key Types & Functions

CommandExecuteFlagsPersistentFlagsAddCommand

About spf13/cobra

The spf13/cobra package (imported as github.com/spf13/cobra) belongs to the CLI category of Go packages. A library for creating powerful modern CLI applications with subcommands, flags, and shell completion.

Go's standard library is one of the language's greatest strengths, providing production-ready implementations for networking, cryptography, encoding, I/O, and more. The spf13/cobra package follows Go's philosophy of simplicity and composability — small, focused packages that combine through interfaces like io.Reader and io.Writer.

When using spf13/cobra in production, follow Go best practices: handle errors explicitly, use context for cancellation and timeouts, prefer composition over inheritance, and write table-driven tests. The Go documentation at pkg.go.dev provides comprehensive API references and examples for every exported type and function.

Related Packages