Skip to content

Cobra

A Commander for modern Go CLI interactions

Overview

Cobra is a library for creating powerful modern CLI applications in Go. It's used by many popular Go projects including Kubernetes, Hugo, and GitHub CLI.

Features

  • Simple subcommands
  • POSIX-compliant flags
  • Intelligent suggestions
  • Automatic help generation
  • Command aliases
  • Nested subcommands
  • Shell completion

Getting Started

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

Basic Example

go
package cmd

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

var rootCmd = &cobra.Command{
  Use:   "mycli",
  Short: "My CLI application",
  Long:  `A longer description of my CLI application.`,
}

func Execute() {
  if err := rootCmd.Execute(); err != nil {
    fmt.Println(err)
    os.Exit(1)
  }
}

func init() {
  rootCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
}

PAPER-CODE Integration

PAPER-CODE provides:

  • Cobra project templates
  • CLI structure patterns
  • Testing configurations
  • Build and distribution setup

Resources